Riven/srcgen/endpoints.rs.dt

199 lines
7.0 KiB
Plaintext
Raw Normal View History

2021-01-16 22:16:56 +00:00
{{
const spec = require('./.spec.json');
const dotUtils = require('./dotUtils.js');
}}{{= dotUtils.preamble() }}
// http://www.mingweisamuel.com/riotapi-schema/tool/
// Version {{= spec.info.version }}
//! Automatically generated endpoint handles.
use crate::models::*;
use std::future::Future;
use std::vec::Vec;
2021-05-22 00:31:52 +00:00
use reqwest::Method;
2021-01-16 22:16:56 +00:00
use crate::Result;
use crate::consts::Region;
use crate::riot_api::RiotApi;
{{
const endpointGroups = {};
for (let path of Object.entries(spec.paths)) {
let ep = path[1]['x-endpoint'];
endpointGroups[ep] = endpointGroups[ep] || [];
endpointGroups[ep].push(path);
}
}}
impl RiotApi {
{{
for (const endpointName of Object.keys(endpointGroups)) {
const method = dotUtils.changeCase.snakeCase(endpointName);
const type = dotUtils.changeCase.pascalCase(endpointName);
}}
/// Returns a handle for accessing [{{= type }}](crate::endpoints::{{= type }}) endpoints.
/// # Riot Developer API Reference
/// <a href="https://developer.riotgames.com/apis#{{= endpointName }}" target="_blank">`{{= endpointName }}`</a>
///
/// Note: this method is automatically generated.
#[inline]
pub fn {{= method }}(&self) -> {{= type }} {
{{= type }} { base: self }
}
{{
}
}}
}
{{
for (let [ endpointName, endpointMethods ] of Object.entries(endpointGroups)) {
let endpoint = dotUtils.changeCase.pascalCase(endpointName);
const endpoint_snake_case = dotUtils.changeCase.snakeCase(endpointName);
}}
/// {{= endpoint }} endpoints handle, accessed by calling [`{{= endpoint_snake_case }}()`](crate::RiotApi::{{= endpoint_snake_case }}) on a [`RiotApi`](crate::RiotApi) instance.
/// # Riot Developer API Reference
/// <a href="https://developer.riotgames.com/apis#{{= endpointName }}" target="_blank">`{{= endpointName }}`</a>
///
/// Note: this struct is automatically generated.
2021-05-22 00:31:52 +00:00
#[repr(transparent)]
2021-01-16 22:16:56 +00:00
pub struct {{= endpoint }}<'a> {
base: &'a RiotApi,
}
impl<'a> {{= endpoint }}<'a> {
{{
2021-05-22 01:22:51 +00:00
for (const [ route, path ] of endpointMethods)
2021-01-16 22:16:56 +00:00
{
2021-05-22 01:22:51 +00:00
for (const [ verb, operation ] of Object.entries(path))
2021-01-16 22:16:56 +00:00
{
2021-05-22 01:22:51 +00:00
if (verb.startsWith('x-')) continue;
if ((operation.parameters || []).some(p => 'header' === p.in)) /* Do not support header parameter methods. */
continue;
const operationId = operation.operationId;
const method = dotUtils.changeCase.snakeCase(operationId.slice(operationId.indexOf('.') + 1));
const resp200 = operation.responses['200'];
/* Return type checks. */
let hasReturn = false;
let returnType = '()';
let returnTypeTurbofish = '';
let returnOptional = false;
if (resp200 && resp200.content)
2021-01-16 22:16:56 +00:00
{
2021-05-22 01:22:51 +00:00
hasReturn = true;
const jsonInfo = resp200.content['application/json'];
const parseType = dotUtils.stringifyType(jsonInfo.schema, { endpoint, fullpath: false });
returnTypeTurbofish = `::<${parseType}>`;
returnOptional = !!operation['x-nullable-404'];
returnType = returnOptional ? `Option<${parseType}>` : parseType;
}
/* Body content checks. */
let bodyType = null;
if (operation.requestBody)
{
const jsonInfo = operation.requestBody.content['application/json'];
bodyType = dotUtils.stringifyType(jsonInfo.schema, { endpoint, fullpath: false });
}
/* Description processing. */
let descArr = operation.description.split('\n');
/* Build argument comment & string. */
const argBuilder = [];
/* Add body params before path/query. */
if (bodyType) {
argBuilder.push(', body: &', bodyType);
}
/* Path and query params. */
const allParams = operation.parameters;
let queryParams = [];
let routeArgument;
if (allParams && allParams.length)
{
let pathParams = allParams.filter(p => 'path' === p.in)
.sortBy(({ name }) => route.indexOf(name));
let reqParams = allParams.filter(p => 'path' !== p.in && p.required);
let optParams = allParams.filter(p => 'path' !== p.in && !p.required)
.sortBy(({ name }) => {
let match = /(^[a-z]+|[A-Z]+(?![a-z])|[A-Z][a-z]+)/.exec(name);
return match.slice(1).reverse().join('');
});
queryParams = reqParams.concat(optParams);
for (let paramList of [ pathParams, reqParams, optParams ])
2021-01-16 22:16:56 +00:00
{
2021-05-22 01:22:51 +00:00
let required = paramList === pathParams;
for (let param of paramList)
{
argBuilder.push(', ', dotUtils.changeCase.snakeCase(param.name), ': ',
dotUtils.stringifyType(param.schema, { endpoint, optional: !required, owned: false }));
}
2021-01-16 22:16:56 +00:00
}
2021-05-22 01:22:51 +00:00
routeArgument = dotUtils.formatRouteArgument(route, pathParams);
}
else
{
routeArgument = dotUtils.formatRouteArgument(route);
2021-01-16 22:16:56 +00:00
}
2021-05-22 01:22:51 +00:00
for (var descLine of descArr)
{
2021-01-16 22:16:56 +00:00
}}
///{{= descLine ? ' ' + descLine : '' }}
{{
2021-05-22 01:22:51 +00:00
}
2021-01-16 22:16:56 +00:00
}}
/// # Parameters
/// * `region` - Region to query.
{{
2021-05-22 01:22:51 +00:00
if (allParams)
2021-01-16 22:16:56 +00:00
{
2021-05-22 01:22:51 +00:00
for (let param of allParams)
{
2021-01-16 22:16:56 +00:00
}}
/// * `{{= param.name }}`{{= param.required ? '' : ' (optional)' }}{{= param.description ? ' - ' + param.description : ''}}
{{
2021-05-22 01:22:51 +00:00
}
2021-01-16 22:16:56 +00:00
}
}}
/// # Riot Developer API Reference
2021-05-22 01:22:51 +00:00
/// <a href="{{= operation.externalDocs.url }}" target="_blank">`{{= operationId }}`</a>
2021-01-16 22:16:56 +00:00
///
/// Note: this method is automatically generated.
pub fn {{= method }}(&self, region: Region{{= argBuilder.join('') }})
-> impl Future<Output = Result<{{= returnType }}>> + 'a
{
2021-05-22 00:31:52 +00:00
#[allow(unused_mut)]
2021-05-22 01:22:51 +00:00
let mut request = self.base.request(Method::{{= verb.toUpperCase() }}, region.into(), {{= routeArgument }});
2021-01-16 22:16:56 +00:00
{{
2021-05-22 01:22:51 +00:00
for (let queryParam of queryParams)
{
2021-01-16 22:16:56 +00:00
}}
{{= dotUtils.formatAddQueryParam(queryParam) }};
{{
2021-05-22 01:22:51 +00:00
}
2021-01-16 22:16:56 +00:00
}}
2021-05-22 01:22:51 +00:00
{{? bodyType }}
let request = request.body(serde_json::ser::to_vec(body).unwrap());
{{?}}
self.base.execute{{= hasReturn ? (returnOptional ? '_opt' : '_val') : '' }}{{= returnTypeTurbofish }}("{{= operationId }}", region.into(), request)
2021-01-16 22:16:56 +00:00
}
{{
2021-05-22 01:22:51 +00:00
}
2021-01-16 22:16:56 +00:00
}
}}
}
{{
}
}}