Riven/srcgen/endpoints.rs.dt

211 lines
7.4 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;
2021-05-22 04:25:19 +00:00
use crate::consts::{ RegionalRoute, PlatformRoute, ValPlatformRoute };
2021-01-16 22:16:56 +00:00
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 }
}
{{
}
}}
}
{{
2021-05-22 02:04:04 +00:00
for (let [ endpointName, endpointMethods ] of Object.entries(endpointGroups))
{
2021-01-16 22:16:56 +00:00
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;
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. */
2021-05-22 04:25:19 +00:00
const argBuilder = [
'route: ', dotUtils.changeCase.pascalCase(operation['x-route-enum']), 'Route'
];
2021-05-22 01:22:51 +00:00
/* Add body params before path/query. */
if (bodyType) {
argBuilder.push(', body: &', bodyType);
}
/* Path and query params. */
const allParams = operation.parameters;
let queryParams = [];
2021-05-22 02:04:04 +00:00
let headerParams = [];
2021-05-22 01:22:51 +00:00
let routeArgument;
if (allParams && allParams.length)
{
2021-05-22 02:04:04 +00:00
const pathParams = allParams.filter(p => 'path' === p.in)
2021-05-22 01:22:51 +00:00
.sortBy(({ name }) => route.indexOf(name));
2021-05-22 02:04:04 +00:00
const reqQueryParams = allParams.filter(p => 'query' === p.in && p.required);
const optQueryParams = allParams.filter(p => 'query' === p.in && !p.required)
2021-05-22 01:22:51 +00:00
.sortBy(({ name }) => {
let match = /(^[a-z]+|[A-Z]+(?![a-z])|[A-Z][a-z]+)/.exec(name);
return match.slice(1).reverse().join('');
});
2021-05-22 02:04:04 +00:00
queryParams = reqQueryParams.concat(optQueryParams);
2021-05-22 01:22:51 +00:00
2021-05-22 02:04:04 +00:00
headerParams = allParams.filter(p => 'header' === p.in);
for (let paramList of [ pathParams, reqQueryParams, optQueryParams, headerParams ])
2021-01-16 22:16:56 +00:00
{
2021-05-22 02:04:04 +00:00
const required = paramList === pathParams;
for (const param of paramList)
2021-05-22 01:22:51 +00:00
{
argBuilder.push(', ', dotUtils.changeCase.snakeCase(param.name), ': ',
2021-05-22 02:04:04 +00:00
dotUtils.stringifyType(param.schema, { endpoint, optional: !(required || param.required), owned: false }));
2021-05-22 01:22:51 +00:00
}
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
2021-05-22 04:25:19 +00:00
/// * `route` - Route to query.
2021-01-16 22:16:56 +00:00
{{
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
}}
2021-05-22 05:13:22 +00:00
/// * `{{= dotUtils.changeCase.snakeCase(param.name) }}` ({{= param.required ? 'required' : 'optional' }}, in {{= param.in }}){{= param.description ? ' - ' + param.description : ''}}
2021-01-16 22:16:56 +00:00
{{
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.
2021-05-22 04:25:19 +00:00
pub fn {{= method }}(&self, {{= argBuilder.join('') }})
2021-01-16 22:16:56 +00:00
-> impl Future<Output = Result<{{= returnType }}>> + 'a
{
2021-05-22 04:25:19 +00:00
let route_str = route.into();
let request = self.base.request(Method::{{= verb.toUpperCase() }}, route_str, {{= 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
}}
2021-05-22 02:04:04 +00:00
{{= dotUtils.formatAddQueryParam(queryParam) }}
{{
}
}}
{{
for (const headerParam of headerParams)
{
}}
{{= dotUtils.formatAddHeaderParam(headerParam) }}
2021-01-16 22:16:56 +00:00
{{
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());
{{?}}
2021-05-22 04:25:19 +00:00
self.base.execute{{= hasReturn ? (returnOptional ? '_opt' : '_val') : '' }}{{= returnTypeTurbofish }}("{{= operationId }}", route_str, 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
}
}}
}
{{
}
}}