1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
//! Method, error and parameter types for the Gitignore endpoint.
#![allow(
    unused_imports,
)]
/* 
 * GitHub v3 REST API
 *
 * GitHub's v3 REST API.
 *
 * OpenAPI spec version: 1.1.4
 * 
 * Generated by: https://github.com/swagger-api/swagger-codegen.git
 */

use serde::Deserialize;

use crate::adapters::{AdapterError, FromJson, GitHubRequest, GitHubRequestBuilder, GitHubResponseExt};
use crate::auth::Auth;
use crate::models::*;

use super::PerPage;

use std::collections::HashMap;
use serde_json::value::Value;

pub struct Gitignore<'api> {
    auth: &'api Auth
}

pub fn new(auth: &Auth) -> Gitignore {
    Gitignore { auth }
}

/// Errors for the [Get all gitignore templates](Gitignore::get_all_templates_async()) endpoint.
#[derive(Debug, thiserror::Error)]
pub enum GitignoreGetAllTemplatesError {
    #[error(transparent)]
    AdapterError(#[from] AdapterError),
    #[error(transparent)]
    SerdeJson(#[from] serde_json::Error),
    #[error(transparent)]
    SerdeUrl(#[from] serde_urlencoded::ser::Error),


    // -- endpoint errors

    #[error("Not modified")]
    Status304,
    #[error("Status code: {}", code)]
    Generic { code: u16 },
}

/// Errors for the [Get a gitignore template](Gitignore::get_template_async()) endpoint.
#[derive(Debug, thiserror::Error)]
pub enum GitignoreGetTemplateError {
    #[error(transparent)]
    AdapterError(#[from] AdapterError),
    #[error(transparent)]
    SerdeJson(#[from] serde_json::Error),
    #[error(transparent)]
    SerdeUrl(#[from] serde_urlencoded::ser::Error),


    // -- endpoint errors

    #[error("Not modified")]
    Status304,
    #[error("Status code: {}", code)]
    Generic { code: u16 },
}



impl<'api> Gitignore<'api> {
    /// ---
    ///
    /// # Get all gitignore templates
    ///
    /// List all templates available to pass as an option when [creating a repository](https://docs.github.com/rest/reference/repos#create-a-repository-for-the-authenticated-user).
    /// 
    /// [GitHub API docs for get_all_templates](https://docs.github.com/rest/reference/gitignore#get-all-gitignore-templates)
    ///
    /// ---
    pub async fn get_all_templates_async(&self) -> Result<Vec<String>, GitignoreGetAllTemplatesError> {

        let request_uri = format!("{}/gitignore/templates", super::GITHUB_BASE_API_URL);


        let req = GitHubRequest {
            uri: request_uri,
            body: None,
            method: "GET",
            headers: vec![]
        };

        let request = GitHubRequestBuilder::build(req, self.auth)?;

        // --

        let github_response = crate::adapters::fetch_async(request).await?;

        // --

        if github_response.is_success() {
            Ok(crate::adapters::to_json_async(github_response).await?)
        } else {
            match github_response.status_code() {
                304 => Err(GitignoreGetAllTemplatesError::Status304),
                code => Err(GitignoreGetAllTemplatesError::Generic { code }),
            }
        }
    }

    /// ---
    ///
    /// # Get all gitignore templates
    ///
    /// List all templates available to pass as an option when [creating a repository](https://docs.github.com/rest/reference/repos#create-a-repository-for-the-authenticated-user).
    /// 
    /// [GitHub API docs for get_all_templates](https://docs.github.com/rest/reference/gitignore#get-all-gitignore-templates)
    ///
    /// ---
    #[cfg(not(target_arch = "wasm32"))]
    pub fn get_all_templates(&self) -> Result<Vec<String>, GitignoreGetAllTemplatesError> {

        let request_uri = format!("{}/gitignore/templates", super::GITHUB_BASE_API_URL);


        let req = GitHubRequest {
            uri: request_uri,
            body: None,
            method: "GET",
            headers: vec![]
        };

        let request = GitHubRequestBuilder::build(req, self.auth)?;

        // --

        let github_response = crate::adapters::fetch(request)?;

        // --

        if github_response.is_success() {
            Ok(crate::adapters::to_json(github_response)?)
        } else {
            match github_response.status_code() {
                304 => Err(GitignoreGetAllTemplatesError::Status304),
                code => Err(GitignoreGetAllTemplatesError::Generic { code }),
            }
        }
    }

    /// ---
    ///
    /// # Get a gitignore template
    ///
    /// The API also allows fetching the source of a single template.
    /// Use the raw [media type](https://docs.github.com/rest/overview/media-types/) to get the raw contents.
    /// 
    /// [GitHub API docs for get_template](https://docs.github.com/rest/reference/gitignore#get-a-gitignore-template)
    ///
    /// ---
    pub async fn get_template_async(&self, name: &str) -> Result<GitignoreTemplate, GitignoreGetTemplateError> {

        let request_uri = format!("{}/gitignore/templates/{}", super::GITHUB_BASE_API_URL, name);


        let req = GitHubRequest {
            uri: request_uri,
            body: None,
            method: "GET",
            headers: vec![]
        };

        let request = GitHubRequestBuilder::build(req, self.auth)?;

        // --

        let github_response = crate::adapters::fetch_async(request).await?;

        // --

        if github_response.is_success() {
            Ok(crate::adapters::to_json_async(github_response).await?)
        } else {
            match github_response.status_code() {
                304 => Err(GitignoreGetTemplateError::Status304),
                code => Err(GitignoreGetTemplateError::Generic { code }),
            }
        }
    }

    /// ---
    ///
    /// # Get a gitignore template
    ///
    /// The API also allows fetching the source of a single template.
    /// Use the raw [media type](https://docs.github.com/rest/overview/media-types/) to get the raw contents.
    /// 
    /// [GitHub API docs for get_template](https://docs.github.com/rest/reference/gitignore#get-a-gitignore-template)
    ///
    /// ---
    #[cfg(not(target_arch = "wasm32"))]
    pub fn get_template(&self, name: &str) -> Result<GitignoreTemplate, GitignoreGetTemplateError> {

        let request_uri = format!("{}/gitignore/templates/{}", super::GITHUB_BASE_API_URL, name);


        let req = GitHubRequest {
            uri: request_uri,
            body: None,
            method: "GET",
            headers: vec![]
        };

        let request = GitHubRequestBuilder::build(req, self.auth)?;

        // --

        let github_response = crate::adapters::fetch(request)?;

        // --

        if github_response.is_success() {
            Ok(crate::adapters::to_json(github_response)?)
        } else {
            match github_response.status_code() {
                304 => Err(GitignoreGetTemplateError::Status304),
                code => Err(GitignoreGetTemplateError::Generic { code }),
            }
        }
    }

}