Skip to content

Commit 9796855

Browse files
committed
Add new mustache templates for v7
Remove old mustache templates
1 parent b4792b9 commit 9796855

File tree

78 files changed

+5479
-1001
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+5479
-1001
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("{{packageTitle}}")]
9+
[assembly: AssemblyDescription("{{packageDescription}}")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("{{packageCompany}}")]
12+
[assembly: AssemblyProduct("{{packageProductName}}")]
13+
[assembly: AssemblyCopyright("{{packageCopyright}}")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// Version information for an assembly consists of the following four values:
23+
//
24+
// Major Version
25+
// Minor Version
26+
// Build Number
27+
// Revision
28+
//
29+
// You can specify all the values or you can default the Build and Revision Numbers
30+
// by using the '*' as shown below:
31+
// [assembly: AssemblyVersion("1.0.*")]
32+
[assembly: AssemblyVersion("{{packageVersion}}")]
33+
[assembly: AssemblyFileVersion("{{packageVersion}}")]
34+
{{^supportsAsync}}
35+
// Settings which don't support asynchronous operations rely on non-public constructors
36+
// This is due to how RestSharp requires the type constraint `where T : new()` in places it probably shouldn't.
37+
[assembly: InternalsVisibleTo("RestSharp")]
38+
[assembly: InternalsVisibleTo("NewtonSoft.Json")]
39+
[assembly: InternalsVisibleTo("JsonSubTypes")]
40+
{{/supportsAsync}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{#isNullable}}{{nrt?}}{{^nrt}}{{#vendorExtensions.x-is-value-type}}?{{/vendorExtensions.x-is-value-type}}{{/nrt}}{{/isNullable}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{#lambda.first}}{{#isNullable}}{{nrt?}}{{^nrt}}{{#vendorExtensions.x-is-value-type}}?{{/vendorExtensions.x-is-value-type}}{{/nrt}} {{/isNullable}}{{^required}}{{nrt?}}{{^nrt}}{{#vendorExtensions.x-is-value-type}}?{{/vendorExtensions.x-is-value-type}}{{/nrt}} {{/required}}{{/lambda.first}}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{{>partial_header}}
2+
using Newtonsoft.Json.Converters;
3+
4+
namespace {{packageName}}.Client
5+
{
6+
/// <summary>
7+
/// Formatter for 'date' openapi formats ss defined by full-date - RFC3339
8+
/// see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#data-types
9+
/// </summary>
10+
public class OpenAPIDateConverter : IsoDateTimeConverter
11+
{
12+
/// <summary>
13+
/// Initializes a new instance of the <see cref="OpenAPIDateConverter" /> class.
14+
/// </summary>
15+
public OpenAPIDateConverter()
16+
{
17+
// full-date = date-fullyear "-" date-month "-" date-mday
18+
DateTimeFormat = "yyyy-MM-dd";
19+
}
20+
}
21+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// {{{name}}} ({{{dataType}}}) pattern.
2+
Regex regex{{{name}}} = new Regex(@"{{{vendorExtensions.x-regex}}}"{{#vendorExtensions.x-modifiers}}{{#-first}}, {{/-first}}RegexOptions.{{{.}}}{{^-last}} | {{/-last}}{{/vendorExtensions.x-modifiers}});
3+
if (!regex{{{name}}}.Match(this.{{{name}}}{{#isUuid}}.ToString(){{/isUuid}}).Success)
4+
{
5+
yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for {{{name}}}, must match a pattern of " + regex{{{name}}}, new [] { "{{{name}}}" });
6+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
{{>partial_header}}
2+
3+
using System;
4+
using System.Threading.Tasks;
5+
using Newtonsoft.Json;
6+
using RestSharp;
7+
using RestSharp.Authenticators;
8+
9+
namespace {{packageName}}.{{clientPackage}}.Auth
10+
{
11+
/// <summary>
12+
/// An authenticator for OAuth2 authentication flows
13+
/// </summary>
14+
public class OAuthAuthenticator : IAuthenticator
15+
{
16+
private TokenResponse{{nrt?}} _token;
17+
18+
/// <summary>
19+
/// Returns the current authentication token. Can return null if there is no authentication token, or it has expired.
20+
/// </summary>
21+
public string{{nrt?}} Token
22+
{
23+
get
24+
{
25+
if (_token == null) return null;
26+
if (_token.ExpiresIn == null) return _token.AccessToken;
27+
if (_token.ExpiresAt < DateTime.Now) return null;
28+
29+
return _token.AccessToken;
30+
}
31+
}
32+
33+
readonly string _tokenUrl;
34+
readonly string _clientId;
35+
readonly string _clientSecret;
36+
readonly string{{nrt?}} _scope;
37+
readonly string _grantType;
38+
readonly JsonSerializerSettings _serializerSettings;
39+
readonly IReadableConfiguration _configuration;
40+
41+
/// <summary>
42+
/// Initialize the OAuth2 Authenticator
43+
/// </summary>
44+
public OAuthAuthenticator(
45+
string tokenUrl,
46+
string clientId,
47+
string clientSecret,
48+
string{{nrt?}} scope,
49+
OAuthFlow? flow,
50+
JsonSerializerSettings serializerSettings,
51+
IReadableConfiguration configuration)
52+
{
53+
_tokenUrl = tokenUrl;
54+
_clientId = clientId;
55+
_clientSecret = clientSecret;
56+
_scope = scope;
57+
_serializerSettings = serializerSettings;
58+
_configuration = configuration;
59+
60+
switch (flow)
61+
{
62+
/*case OAuthFlow.ACCESS_CODE:
63+
_grantType = "authorization_code";
64+
break;
65+
case OAuthFlow.IMPLICIT:
66+
_grantType = "implicit";
67+
break;
68+
case OAuthFlow.PASSWORD:
69+
_grantType = "password";
70+
break;*/
71+
case OAuthFlow.APPLICATION:
72+
_grantType = "client_credentials";
73+
break;
74+
default:
75+
break;
76+
}
77+
}
78+
79+
/// <summary>
80+
/// Creates an authentication parameter from an access token.
81+
/// </summary>
82+
/// <returns>An authentication parameter.</returns>
83+
protected async ValueTask<Parameter> GetAuthenticationParameter()
84+
{
85+
var token = string.IsNullOrEmpty(Token) ? await GetToken().ConfigureAwait(false) : Token;
86+
return new HeaderParameter(KnownHeaders.Authorization, token);
87+
}
88+
89+
/// <summary>
90+
/// Gets the token from the OAuth2 server.
91+
/// </summary>
92+
/// <returns>An authentication token.</returns>
93+
async Task<string> GetToken()
94+
{
95+
var client = new RestClient(_tokenUrl, configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(_serializerSettings, _configuration)));
96+
97+
var request = new RestRequest();
98+
if (!string.IsNullOrWhiteSpace(_token?.RefreshToken))
99+
{
100+
request.AddParameter("grant_type", "refresh_token")
101+
.AddParameter("refresh_token", _token.RefreshToken);
102+
}
103+
else
104+
{
105+
request
106+
.AddParameter("grant_type", _grantType)
107+
.AddParameter("client_id", _clientId)
108+
.AddParameter("client_secret", _clientSecret);
109+
}
110+
if (!string.IsNullOrEmpty(_scope))
111+
{
112+
request.AddParameter("scope", _scope);
113+
}
114+
_token = await client.PostAsync<TokenResponse>(request).ConfigureAwait(false);
115+
// RFC6749 - token_type is case insensitive.
116+
// RFC6750 - In Authorization header Bearer should be capitalized.
117+
// Fix the capitalization irrespective of token_type casing.
118+
switch (_token?.TokenType?.ToLower())
119+
{
120+
case "bearer":
121+
return $"Bearer {_token.AccessToken}";
122+
default:
123+
return $"{_token?.TokenType} {_token?.AccessToken}";
124+
}
125+
}
126+
127+
/// <summary>
128+
/// Retrieves the authentication token (creating a new one if necessary) and adds it to the current request
129+
/// </summary>
130+
/// <param name="client"></param>
131+
/// <param name="request"></param>
132+
/// <returns></returns>
133+
public async ValueTask Authenticate(IRestClient client, RestRequest request)
134+
=> request.AddOrUpdateParameter(await GetAuthenticationParameter().ConfigureAwait(false));
135+
}
136+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{{>partial_header}}
2+
3+
namespace {{packageName}}.{{clientPackage}}.Auth
4+
{
5+
/// <summary>
6+
/// Available flows for OAuth2 authentication
7+
/// </summary>
8+
public enum OAuthFlow
9+
{
10+
/// <summary>Authorization code flow</summary>
11+
ACCESS_CODE,
12+
/// <summary>Implicit flow</summary>
13+
IMPLICIT,
14+
/// <summary>Password flow</summary>
15+
PASSWORD,
16+
/// <summary>Client credentials flow</summary>
17+
APPLICATION
18+
}
19+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{{>partial_header}}
2+
3+
using System;
4+
using Newtonsoft.Json;
5+
6+
namespace {{packageName}}.{{clientPackage}}.Auth
7+
{
8+
class TokenResponse
9+
{
10+
[JsonProperty("token_type")]
11+
public string TokenType { get; set; }
12+
[JsonProperty("access_token")]
13+
public string AccessToken { get; set; }
14+
[JsonProperty("expires_in")]
15+
public int? ExpiresIn { get; set; }
16+
[JsonProperty("created")]
17+
public DateTime? Created { get; set; }
18+
19+
[JsonProperty("refresh_token")]
20+
public string{{nrt?}} RefreshToken { get; set; }
21+
22+
public DateTime? ExpiresAt => ExpiresIn == null ? null : Created?.AddSeconds(ExpiresIn.Value);
23+
}
24+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// <auto-generated>
2+
{{>partial_header}}
3+
4+
{{#nrt}}
5+
#nullable enable
6+
7+
{{/nrt}}
8+
using System;
9+
10+
namespace {{packageName}}.{{corePackageName}}.{{clientPackage}}
11+
{
12+
/// <summary>
13+
/// API Exception
14+
/// </summary>
15+
{{>visibility}} class ApiException : Exception
16+
{
17+
/// <summary>
18+
/// The reason the api request failed
19+
/// </summary>
20+
public string{{nrt?}} ReasonPhrase { get; }
21+
22+
/// <summary>
23+
/// The HttpStatusCode
24+
/// </summary>
25+
public System.Net.HttpStatusCode StatusCode { get; }
26+
27+
/// <summary>
28+
/// The raw data returned by the api
29+
/// </summary>
30+
public string RawContent { get; }
31+
32+
/// <summary>
33+
/// Construct the ApiException from parts of the response
34+
/// </summary>
35+
/// <param name="reasonPhrase"></param>
36+
/// <param name="statusCode"></param>
37+
/// <param name="rawContent"></param>
38+
public ApiException(string{{nrt?}} reasonPhrase, System.Net.HttpStatusCode statusCode, string rawContent) : base(reasonPhrase ?? rawContent)
39+
{
40+
ReasonPhrase = reasonPhrase;
41+
42+
StatusCode = statusCode;
43+
44+
RawContent = rawContent;
45+
}
46+
}
47+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
using Microsoft.Extensions.DependencyInjection;
3+
4+
namespace {{packageName}}.{{corePackageName}}.{{clientPackage}}
5+
{
6+
/// <summary>
7+
/// The factory interface for creating the services that can communicate with the {{packageName}} APIs.
8+
/// </summary>
9+
{{>visibility}} interface {{interfacePrefix}}ApiFactory
10+
{
11+
/// <summary>
12+
/// A method to create an IApi of type IResult
13+
/// </summary>
14+
/// <typeparam name="IResult"></typeparam>
15+
/// <returns></returns>
16+
IResult Create<IResult>() where IResult : {{interfacePrefix}}{{packageName}}ApiService;
17+
}
18+
19+
/// <summary>
20+
/// The implementation of <see cref="{{interfacePrefix}}ApiFactory"/>.
21+
/// </summary>
22+
{{>visibility}} class ApiFactory : {{interfacePrefix}}ApiFactory
23+
{
24+
/// <summary>
25+
/// The service provider
26+
/// </summary>
27+
public IServiceProvider Services { get; }
28+
29+
/// <summary>
30+
/// Initializes a new instance of the <see cref="ApiFactory"/> class.
31+
/// </summary>
32+
/// <param name="services"></param>
33+
public ApiFactory(IServiceProvider services)
34+
{
35+
Services = services;
36+
}
37+
38+
/// <summary>
39+
/// A method to create an IApi of type IResult
40+
/// </summary>
41+
/// <typeparam name="IResult"></typeparam>
42+
/// <returns></returns>
43+
public IResult Create<IResult>() where IResult : {{interfacePrefix}}{{packageName}}ApiService
44+
{
45+
return Services.GetRequiredService<IResult>();
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)