//! Method, error and parameter types for the {{classname}} endpoint. #![allow( unused_imports, )] {{>partial_header}} 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 {{classname}}<'api> { auth: &'api Auth } pub fn new(auth: &Auth) -> {{classname}} { {{classname}} { auth } } {{#operations}}{{#operation}}{{#contents}} /// Errors for the [{{summary}}]({{classname}}::{{operationId}}_async()) endpoint. #[derive(Debug, thiserror::Error)] pub enum {{classname}}{{operationIdCamelCase}}Error { #[error(transparent)] AdapterError(#[from] AdapterError), #[error(transparent)] SerdeJson(#[from] serde_json::Error), #[error(transparent)] SerdeUrl(#[from] serde_urlencoded::ser::Error), // -- endpoint errors {{#responses}}{{^vendorExtensions.x-is-default}} #[error("{{message}}")] Status{{code}}{{#dataType}}({{{.}}}){{/dataType}}, {{/vendorExtensions.x-is-default}}{{/responses}} #[error("Status code: {}", code)] Generic { code: u16 }, } {{/contents}}{{/operation}} {{#operation}}{{#contents}}{{#hasQueryParams}} /// Query parameters for the [{{summary}}]({{classname}}::{{operationId}}_async()) endpoint. #[derive(Default, Serialize)] pub struct {{classname}}{{operationIdCamelCase}}Params{{#vendorExtensions.x-codegen-has-string-params}}<'req>{{/vendorExtensions.x-codegen-has-string-params}} { {{#queryParams}} {{#description}}/// {{{.}}}{{/description}} {{paramName}}: {{^required}}Option<{{#isString}}&'req str{{/isString}}{{#isUuid}}&'req str{{/isUuid}}{{^isString}}{{^isUuid}}{{{dataType}}}{{/isUuid}}{{/isString}}>{{/required}}{{#required}}{{#isString}}&'req str{{/isString}}{{#isUuid}}&'req str{{/isUuid}}{{^isString}}{{^isUuid}}{{{dataType}}}{{/isUuid}}{{/isString}}{{/required}}{{#hasMore}}, {{/hasMore}} {{/queryParams}} } impl{{#vendorExtensions.x-codegen-has-string-params}}<'req>{{/vendorExtensions.x-codegen-has-string-params}} {{classname}}{{operationIdCamelCase}}Params{{#vendorExtensions.x-codegen-has-string-params}}<'req>{{/vendorExtensions.x-codegen-has-string-params}} { pub fn new() -> Self { Self::default() } {{#queryParams}} {{#description}}/// {{{.}}}{{/description}} pub fn {{paramName}}(self, {{paramName}}: {{#isString}}&'req str{{/isString}}{{#isUuid}}&'req str{{/isUuid}}{{^isString}}{{^isUuid}}{{{dataType}}}{{/isUuid}}{{/isString}}) -> Self { Self { {{#queryParams}} {{#equals paramName ../paramName}}{{paramName}}: {{^required}}Some({{/required}}{{paramName}}{{#isString}}{{/isString}}{{#isUuid}}{{/isUuid}}{{^required}}){{/required}},{{else}}{{paramName}}: self.{{paramName}}, {{/equals}} {{/queryParams}} } } {{/queryParams}} } {{#vendorExtensions.x-codegen-impl-per-page}} impl<'enc> From<&'enc PerPage> for {{classname}}{{operationIdCamelCase}}Params{{#vendorExtensions.x-codegen-has-string-params}}<'enc>{{/vendorExtensions.x-codegen-has-string-params}} { fn from(per_page: &'enc PerPage) -> Self { Self { per_page: Some(per_page.per_page), page: Some(per_page.page), ..Default::default() } } } {{/vendorExtensions.x-codegen-impl-per-page}} {{/hasQueryParams}} {{/contents}}{{/operation}} impl<'api> {{classname}}<'api> { {{#operation}}{{#contents}} /// --- /// {{#summary}} /// # {{.}} {{/summary}}{{#unescapedNotes}}/// /// {{{.}}} {{/unescapedNotes}}{{#externalDocs.url}}/// /// [GitHub API docs for {{operationId}}]({{.}}){{/externalDocs.url}} {{#vendorExtensions.x-codegen-has-previews}} /// {{/vendorExtensions.x-codegen-has-previews}} {{#vendorExtensions.x-github.previews}} /// The `{{operationId}}_async` endpoint is enabled with the `{{name}}` cargo feature. {{/vendorExtensions.x-github.previews}} /// /// --- {{#vendorExtensions.x-github.previews}}#[cfg(feature = "{{name}}")] {{/vendorExtensions.x-github.previews}}pub async fn {{operationId}}_async(&self{{#hasParams}}, {{/hasParams}}{{#pathParams}}{{paramName}}: {{#nullable}}Option<{{#isString}}&str{{/isString}}{{#isUuid}}&str{{/isUuid}}{{^isString}}{{^isUuid}}{{{dataType}}}{{/isUuid}}{{/isString}}>{{/nullable}}{{^nullable}}{{#isString}}&str{{/isString}}{{#isUuid}}&str{{/isUuid}}{{^isString}}{{^isUuid}}{{{dataType}}}{{/isUuid}}{{/isString}}{{/nullable}}{{#hasMore}}, {{/hasMore}}{{/pathParams}}{{#hasQueryParams}}{{#hasPathParams}}, {{/hasPathParams}}query_params: {{#vendorExtensions.x-codegen-has-optional-query-params}}Option<{{/vendorExtensions.x-codegen-has-optional-query-params}}impl Into<{{classname}}{{operationIdCamelCase}}Params{{#vendorExtensions.x-codegen-has-string-params}}<'api>{{/vendorExtensions.x-codegen-has-string-params}}>{{#vendorExtensions.x-codegen-has-optional-query-params}}>{{/vendorExtensions.x-codegen-has-optional-query-params}}{{#hasBodyParam}}, {{/hasBodyParam}}{{/hasQueryParams}}{{#hasBodyParam}}{{^hasQueryParams}}{{#hasPathParams}}, {{/hasPathParams}}{{/hasQueryParams}}body: {{#bodyParam}}{{{dataType}}}{{/bodyParam}}{{/hasBodyParam}}) -> Result<{{#responses}}{{#vendorExtensions.x-is-default}}{{{dataType}}}{{/vendorExtensions.x-is-default}}{{/responses}}{{#vendorExtensions.x-codegen-response-empty-default}}(){{/vendorExtensions.x-codegen-response-empty-default}}, {{classname}}{{operationIdCamelCase}}Error> { let {{#hasQueryParams}}mut {{/hasQueryParams}}request_uri = format!("{}{{path}}", super::GITHUB_BASE_API_URL{{#hasPathParams}}, {{/hasPathParams}}{{#pathParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/pathParams}}); {{#hasQueryParams}} {{#vendorExtensions.x-codegen-has-optional-query-params}} if let Some(params) = query_params { request_uri.push_str("?"); request_uri.push_str(&serde_urlencoded::to_string(params.into())?); } {{/vendorExtensions.x-codegen-has-optional-query-params}} {{^vendorExtensions.x-codegen-has-optional-query-params}} request_uri.push_str("?"); request_uri.push_str(&serde_urlencoded::to_string(query_params.into())?); {{/vendorExtensions.x-codegen-has-optional-query-params}} {{/hasQueryParams}} let req = GitHubRequest { uri: request_uri, body: {{#hasBodyParam}}Some({{#bodyParam}}{{#vendorExtensions.x-is-map-container}}HashMap{{/vendorExtensions.x-is-map-container}}{{^vendorExtensions.x-is-map-container}}{{{dataType}}}{{/vendorExtensions.x-is-map-container}}{{/bodyParam}}::from_json(body)?){{/hasBodyParam}}{{^hasBodyParam}}None{{/hasBodyParam}}, method: "{{httpMethod}}", headers: vec![{{#vendorExtensions.x-github.previews}}("Accept", "application/vnd.github.{{name}}-preview+json"), {{/vendorExtensions.x-github.previews}}] }; 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() { {{#responses}}{{#vendorExtensions}}{{^x-is-default}} {{code}} => {{#dataType}}Err({{classname}}{{operationIdCamelCase}}Error::Status{{code}}(crate::adapters::to_json_async(github_response).await?)){{/dataType}}{{^dataType}}Err({{classname}}{{operationIdCamelCase}}Error::Status{{code}}){{/dataType}}, {{/x-is-default}}{{/vendorExtensions}}{{/responses}} code => Err({{classname}}{{operationIdCamelCase}}Error::Generic { code }), } } } /// --- /// {{#summary}} /// # {{.}} {{/summary}}{{#unescapedNotes}}/// /// {{{.}}} {{/unescapedNotes}}{{#externalDocs.url}}/// /// [GitHub API docs for {{operationId}}]({{.}}){{/externalDocs.url}} {{#vendorExtensions.x-codegen-has-previews}} /// {{/vendorExtensions.x-codegen-has-previews}} {{#vendorExtensions.x-github.previews}} /// The `{{operationId}}` endpoint is enabled with the `{{name}}` cargo feature. {{/vendorExtensions.x-github.previews}} /// /// --- #[cfg(not(target_arch = "wasm32"))] {{#vendorExtensions.x-github.previews}}#[cfg(feature = "{{name}}")] {{/vendorExtensions.x-github.previews}}pub fn {{operationId}}(&self{{#hasParams}}, {{/hasParams}}{{#pathParams}}{{paramName}}: {{#nullable}}Option<{{#isString}}&str{{/isString}}{{#isUuid}}&str{{/isUuid}}{{^isString}}{{^isUuid}}{{{dataType}}}{{/isUuid}}{{/isString}}>{{/nullable}}{{^nullable}}{{#isString}}&str{{/isString}}{{#isUuid}}&str{{/isUuid}}{{^isString}}{{^isUuid}}{{{dataType}}}{{/isUuid}}{{/isString}}{{/nullable}}{{#hasMore}}, {{/hasMore}}{{/pathParams}}{{#hasQueryParams}}{{#hasPathParams}}, {{/hasPathParams}}query_params: {{#vendorExtensions.x-codegen-has-optional-query-params}}Option<{{/vendorExtensions.x-codegen-has-optional-query-params}}impl Into<{{classname}}{{operationIdCamelCase}}Params{{#vendorExtensions.x-codegen-has-string-params}}<'api>{{/vendorExtensions.x-codegen-has-string-params}}>{{#vendorExtensions.x-codegen-has-optional-query-params}}>{{/vendorExtensions.x-codegen-has-optional-query-params}}{{#hasBodyParam}}, {{/hasBodyParam}}{{/hasQueryParams}}{{#hasBodyParam}}{{^hasQueryParams}}{{#hasPathParams}}, {{/hasPathParams}}{{/hasQueryParams}}body: {{#bodyParam}}{{{dataType}}}{{/bodyParam}}{{/hasBodyParam}}) -> Result<{{#responses}}{{#vendorExtensions.x-is-default}}{{{dataType}}}{{/vendorExtensions.x-is-default}}{{/responses}}{{#vendorExtensions.x-codegen-response-empty-default}}(){{/vendorExtensions.x-codegen-response-empty-default}}, {{classname}}{{operationIdCamelCase}}Error> { let {{#hasQueryParams}}mut {{/hasQueryParams}}request_uri = format!("{}{{path}}", super::GITHUB_BASE_API_URL{{#hasPathParams}}, {{/hasPathParams}}{{#pathParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/pathParams}}); {{#hasQueryParams}} {{#vendorExtensions.x-codegen-has-optional-query-params}} if let Some(params) = query_params { request_uri.push_str("?"); let qp: {{classname}}{{operationIdCamelCase}}Params = params.into(); request_uri.push_str(&serde_urlencoded::to_string(qp)?); } {{/vendorExtensions.x-codegen-has-optional-query-params}} {{^vendorExtensions.x-codegen-has-optional-query-params}} request_uri.push_str("?"); let qp: {{classname}}{{operationIdCamelCase}}Params = query_params.into(); request_uri.push_str(&serde_urlencoded::to_string(qp)?); {{/vendorExtensions.x-codegen-has-optional-query-params}} {{/hasQueryParams}} let req = GitHubRequest { uri: request_uri, body: {{#hasBodyParam}}Some({{#bodyParam}}{{#vendorExtensions.x-is-map-container}}HashMap{{/vendorExtensions.x-is-map-container}}{{^vendorExtensions.x-is-map-container}}{{{dataType}}}{{/vendorExtensions.x-is-map-container}}{{/bodyParam}}::from_json(body)?){{/hasBodyParam}}{{^hasBodyParam}}None{{/hasBodyParam}}, method: "{{httpMethod}}", headers: vec![{{#vendorExtensions.x-github.previews}}("Accept", "application/vnd.github.{{name}}-preview+json"), {{/vendorExtensions.x-github.previews}}] }; 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() { {{#responses}}{{#vendorExtensions}}{{^x-is-default}} {{code}} => {{#dataType}}Err({{classname}}{{operationIdCamelCase}}Error::Status{{code}}(crate::adapters::to_json(github_response)?)){{/dataType}}{{^dataType}}Err({{classname}}{{operationIdCamelCase}}Error::Status{{code}}){{/dataType}}, {{/x-is-default}}{{/vendorExtensions}}{{/responses}} code => Err({{classname}}{{operationIdCamelCase}}Error::Generic { code }), } } } {{/contents}}{{/operation}}{{/operations}} }