Skip to content
This repository has been archived by the owner on Nov 10, 2022. It is now read-only.

Commit

Permalink
Hello RiotGamesCollection<T>!
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaeldui committed Jan 13, 2022
1 parent 2968dfd commit 1de629a
Show file tree
Hide file tree
Showing 36 changed files with 771 additions and 604 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public virtual void AddGroupsAsNestedClassesWithEndpoints(IEnumerable<IGrouping<

Dictionary<string, string>? pathParameters = ToPathParameters(getMethodObject?.Parameters);

string returnType = responseSchema.GetTypeName();
string returnType = LeagueClientEndpointsHelper.GetTypeName(responseSchema);
string? typeArgument = TypeArgumentStatement(returnType.Remove("[]"));
var baseMethod = Enums.Contains(returnType) ? "GetEnumAsync" : GetRiotHttpClientMethod(HttpMethod.Get, returnType, ref typeArgument);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,15 @@ public static string GetNameFromPath(string path, bool? isPlural)
return firstPart._toName() + secondPart._toName() + lastPart?._toName();
}

public static string GetTypeName(this OpenApiSchemaObject schema)
{
var name = OpenApiComponentHelper.GetTypeName(schema);
if (name.EndsWith("[]"))
name = $"LeagueClientCollection<{name.RemoveEnd("[]")}>";

return name;
}


public static string GetTypeName(this LcuParameterObject parameter)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ private void _addDto(string identifier, LcuComponentSchemaObject schema)

if (schema.Enum != null)
{
if (!_usingSystemTextJsonSerialization)
{
_usingSystemTextJsonSerialization = true;
_namespace = _namespace.AddSystemTextJsonSerializationUsing();
}

member = PublicEnumDeclaration(identifier, schema.Enum.ToPascalCase())
.AddJsonStringEnumAttribute();
}
Expand All @@ -55,7 +61,7 @@ private void _addDto(string identifier, LcuComponentSchemaObject schema)
string typeName;

if (kv.Value.Type == "array")
typeName = kv.Value.Items.GetTypeName() + "[]";
typeName = $"LeagueClientCollection<{kv.Value.Items.GetTypeName()}>";
else
typeName = kv.Value.GetTypeName();

Expand Down
13 changes: 1 addition & 12 deletions RiotGames.Client.CodeGeneration/OpenApiEndpointGeneratorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,7 @@ protected virtual string GetRiotHttpClientMethod(HttpMethod httpMethod, string r
string? specificMethod = null;

// Current work-around, added some type specific HTTP methods.
if (returnType.EndsWith("[]"))
if (returnType.StartsWith("string") || returnType.StartsWith("dynamic") || returnType.StartsWith("int") || returnType.StartsWith("long"))
{
specificMethod = "SystemType";
if (returnType.StartsWith("dynamic"))
typeArgument = TypeArgumentStatement("ExpandoObject[]");
else
typeArgument = TypeArgumentStatement(returnType);
}
else
specificMethod = "Array";
else if (returnType is "int" or "string" or "bool" or "dynamic" or "long" or "double")
if (returnType is "int" or "string" or "bool" or "dynamic" or "long" or "double")
{
specificMethod = "SystemType";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ protected override void AddEndpoint(EndpointDefinition? endpoint)

string clientName = isPlatform ? "PlatformClient" : "RegionalClient";

string returnType = responseSchema.GetTypeName();
string? typeArgument = TypeArgumentStatement(returnType.Remove("[]"));
string returnType = responseSchema.GetTypeName(_client);
string? typeArgument = TypeArgumentStatement(returnType);

string httpClientMethod = GetRiotHttpClientMethod(HttpMethod.Get, returnType, ref typeArgument);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void AddDto(Schema schema)
identifier = "X" + identifier;
}

string typeName = kv.Value.GetTypeName();
string typeName = kv.Value.GetTypeName(_client);

//typeName += "?"; // Make nullable

Expand Down
128 changes: 64 additions & 64 deletions RiotGames.Client.CodeGeneration/RiotGamesApi/RiotApiModelsHelper.cs
Original file line number Diff line number Diff line change
@@ -1,64 +1,64 @@
using MingweiSamuel;
using MingweiSamuel.RiotApi;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RiotGames.Client.CodeGeneration.RiotGamesApi
{
internal static class RiotApiModelsHelper
{
public static string GetTypeNameFromRef(string @ref)
{
@ref = RiotApiHacks.EndpointsWithDuplicateSchemas
.FirstOrDefault(kvp => @ref.Contains(kvp.Key))
.Value + @ref?.Split('.')?.Last();
return OpenApiComponentHelper.GetTypeNameFromRef(@ref);
}

public static string GetTypeName(this RiotApiSchemaObject schema)
{
if (schema.Ref != null)
return GetTypeNameFromRef(schema.Ref);
else if (schema.XType == null && schema.Type == null)
Debugger.Break();
else
switch (schema.Type)
{
case "array":
return (schema.XType.Remove("Set[").Remove("List[").TrimEnd(']')).RemoveDtoSuffix() + "[]";
case "integer":
if (schema.XType == null)
throw new Exception("Schema.XType is null for some reason.");
return schema.XType;
case "string":
return schema.Type;
}

throw new Exception("Couldn't figure out the response type.");
}

public static string GetTypeName(this OpenApiComponentPropertyObject property)
{
if (property.Type == "array" || (property as RiotApiComponentPropertyObject)?.XType == "array")
return GetTypeName(property.Items) + "[]";

if (property.Ref != null)
return GetTypeNameFromRef(property.Ref);

var name = ((property as RiotApiComponentPropertyObject)?.XType ?? property.Format ?? property.Type).RemoveDtoSuffix();

return name switch
{
"int32" => "int",
"int64" => "long",
"boolean" => "bool",
_ => name,
};
}

}
}
using MingweiSamuel;
using MingweiSamuel.RiotApi;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RiotGames.Client.CodeGeneration.RiotGamesApi
{
internal static class RiotApiModelsHelper
{
public static string GetTypeNameFromRef(string @ref)
{
@ref = RiotApiHacks.EndpointsWithDuplicateSchemas
.FirstOrDefault(kvp => @ref.Contains(kvp.Key))
.Value + @ref?.Split('.')?.Last();
return OpenApiComponentHelper.GetTypeNameFromRef(@ref);
}

public static string GetTypeName(this RiotApiSchemaObject schema, Client client)
{
if (schema.Ref != null)
return GetTypeNameFromRef(schema.Ref);
else if (schema.XType == null && schema.Type == null)
Debugger.Break();
else
switch (schema.Type)
{
case "array":
return $"{client}Collection<{schema.XType.RemoveStart("Set[").RemoveStart("List[").TrimEnd(']').RemoveDtoSuffix()}>";
case "integer":
if (schema.XType == null)
throw new Exception("Schema.XType is null for some reason.");
return schema.XType;
case "string":
return schema.Type;
}

throw new Exception("Couldn't figure out the response type.");
}

public static string GetTypeName(this OpenApiComponentPropertyObject property, Client client)
{
if (property.Type == "array" || (property as RiotApiComponentPropertyObject)?.XType == "array")
return $"{client}Collection<{GetTypeName(property.Items, client)}>";

if (property.Ref != null)
return GetTypeNameFromRef(property.Ref);

var name = ((property as RiotApiComponentPropertyObject)?.XType ?? property.Format ?? property.Type).RemoveDtoSuffix();

return name switch
{
"int32" => "int",
"int64" => "long",
"boolean" => "bool",
_ => name,
};
}

}
}
17 changes: 0 additions & 17 deletions RiotGames.Client/Attributes/JsonAttributes.cs

This file was deleted.

51 changes: 24 additions & 27 deletions RiotGames.Client/Http/RiotGamesApiHttpClient.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
using Camille.Enums;

namespace RiotGames
{
internal class RiotGamesApiHttpClient<TObjectBase> : RiotGamesHttpClient<TObjectBase>
{
private RiotGamesApiHttpClient(string apiKey, string apiSubDomain)
{
HttpClient.DefaultRequestHeaders.TryAddWithoutValidation("X-Riot-Token", apiKey);
HttpClient.BaseAddress = new Uri($"https://{apiSubDomain}.api.riotgames.com");
}

internal RiotGamesApiHttpClient(string apiKey, RegionalRoute region) : this(apiKey, region.ToString().ToLower())
{

}

internal RiotGamesApiHttpClient(string apiKey, PlatformRoute platform) : this(apiKey, platform.ToString().ToLower())
{

}

internal RiotGamesApiHttpClient(string apiKey, ValPlatformRoute platform) : this(apiKey, platform.ToString().ToLower())
{

}
}
using Camille.Enums;

namespace RiotGames
{
internal class RiotGamesApiHttpClient<TObjectBase> : RiotGamesHttpClient<TObjectBase>
{
private RiotGamesApiHttpClient(string apiKey, string apiSubDomain)
{
HttpClient.DefaultRequestHeaders.TryAddWithoutValidation("X-Riot-Token", apiKey);
HttpClient.BaseAddress = new Uri($"https://{apiSubDomain}.api.riotgames.com");
}

internal RiotGamesApiHttpClient(string apiKey, RegionalRoute region) : this(apiKey, region.ToString().ToLower())
{
}

internal RiotGamesApiHttpClient(string apiKey, PlatformRoute platform) : this(apiKey, platform.ToString().ToLower())
{
}

internal RiotGamesApiHttpClient(string apiKey, ValPlatformRoute platform) : this(apiKey, platform.ToString().ToLower())
{
}
}
}
8 changes: 2 additions & 6 deletions RiotGames.Client/Http/RiotGamesHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,19 @@ static RiotGamesHttpClient()
internal protected RiotGamesHttpClient()
{
HttpClient = new HttpClient();
HttpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", USER_AGENT);
HttpClient.DefaultRequestHeaders.Add("User-Agent", USER_AGENT);
}

internal protected RiotGamesHttpClient(HttpClientHandler httpClientHandler)
{
HttpClient = new HttpClient(httpClientHandler);
HttpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", USER_AGENT);
HttpClient.DefaultRequestHeaders.Add("User-Agent", USER_AGENT);
}

internal async Task<TResult?> GetAsync<TResult>(string? requestUri)
where TResult : TObjectBase =>
await HttpClient.GetFromJsonAsync<TResult>(requestUri);

internal async Task<TResult[]?> GetArrayAsync<TResult>(string? requestUri)
where TResult : TObjectBase =>
await HttpClient.GetFromJsonAsync<TResult[]>(requestUri);

internal async Task<T> GetSystemTypeAsync<T>(string? requestUri)
{
switch (Type.GetTypeCode(typeof(T)))
Expand Down
Loading

0 comments on commit 1de629a

Please sign in to comment.