Skip to content

Commit

Permalink
Merge pull request #29 from ashakoor/main
Browse files Browse the repository at this point in the history
v0.7.2-beta.1
  • Loading branch information
Cytraen authored Mar 5, 2021
2 parents cde0655 + ebb604a commit 6dd33ee
Show file tree
Hide file tree
Showing 25 changed files with 543 additions and 177 deletions.
14 changes: 4 additions & 10 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,16 @@ jobs:
run: dotnet build ./BungieSharper -c Release

- name: Pack entities NuGet package
run: dotnet pack ./BungieSharper.Entities -c Release --no-build --output EntitiesPackage --version-suffix "ci.$(date -u +%Y%m%dT%H%M%S)+sha.${GITHUB_SHA:0:9}" -p:ContinuousIntegrationBuild=true
run: dotnet pack ./BungieSharper.Entities -c Release --no-build --output OutputPackage --version-suffix "ci.$(date -u +%Y%m%dT%H%M%S)+sha.${GITHUB_SHA:0:9}" -p:ContinuousIntegrationBuild=true

- name: Pack client NuGet package
run: dotnet pack ./BungieSharper -c Release --no-build --output ClientPackage --version-suffix "ci.$(date -u +%Y%m%dT%H%M%S)+sha.${GITHUB_SHA:0:9}" -p:ContinuousIntegrationBuild=true
run: dotnet pack ./BungieSharper -c Release --no-build --output OutputPackage --version-suffix "ci.$(date -u +%Y%m%dT%H%M%S)+sha.${GITHUB_SHA:0:9}" -p:ContinuousIntegrationBuild=true

- name: Upload entities artifact (nupkg/snupkg)
uses: actions/upload-artifact@v1
with:
name: BungieSharper.Entities - ${{ env.SHA }}
path: EntitiesPackage

- name: Upload client artifact (nupkg/snupkg)
- name: Upload artifact (nupkg/snupkg)
uses: actions/upload-artifact@v1
with:
name: BungieSharper - ${{ env.SHA }}
path: ClientPackage
path: OutputPackage

- name: Build test
run: dotnet build ./BungieSharper.Tests -c Release
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<IsPackable>false</IsPackable>

<TargetFramework>net5.0</TargetFramework>
<Version>0.7.1</Version>
<Version>0.7.2-beta.1</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<Nullable>enable</Nullable>

<TargetFramework>net5.0</TargetFramework>
<Version>0.7.1</Version>
<Version>0.7.2-beta.1</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion BungieSharper.CodeGen/BungieSharper.CodeGen.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<Nullable>enable</Nullable>

<TargetFramework>net5.0</TargetFramework>
<Version>0.7.1</Version>
<Version>0.7.2-beta.1</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
4 changes: 3 additions & 1 deletion BungieSharper.CodeGen/Generation/GeneratePath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public static string GeneratePathContent(string path, PathObject pathDef)
var requiredScopes = responseMethodInfo.Security != null ? responseMethodInfo.Security.Any() ? responseMethodInfo.Security[0].OAuth2 : null : null;

content += FormatStrings.FormatMethodSummaries(pathDef.Description, parameters, responseMethodInfo.XPreview ?? false, responseMethodInfo.Deprecated ?? false, requiredScopes);
content += " /// <param name=\"authToken\">The OAuth access token to autheticate the request with.</param>\n";
content += " /// <param name=\"cancelToken\">The <see cref=\"CancellationToken\" /> to observe.</param>\n";

var responseRef = responseMethodInfo.Responses[200].Ref;

Expand Down Expand Up @@ -152,7 +154,7 @@ public static string GeneratePathContent(string path, PathObject pathDef)
content += $" return _apiAccessor.ApiRequestAsync<{responseType}>(\n";
content += $" new Uri($\"{path.TrimStart('/')}\"{queryStringParamFinal}, UriKind.Relative),\n";

content += $" {(responseMethodInfo.RequestBody != null ? "new StringContent(JsonSerializer.Serialize(requestBody), System.Text.Encoding.UTF8, \"application/json\")" : "null")}, HttpMethod.{httpMethod}, authToken, AuthHeaderType.Bearer, cancelToken\n";
content += $" {(responseMethodInfo.RequestBody != null ? "new StringContent(JsonSerializer.Serialize(requestBody), System.Text.Encoding.UTF8, \"application/json\")" : "null")}, HttpMethod.{httpMethod}, authToken, cancelToken\n";
content += $" );\n }}";

return content.Replace("System.DateTime", "DateTime");
Expand Down
2 changes: 1 addition & 1 deletion BungieSharper.Entities/BungieSharper.Entities.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<Nullable>enable</Nullable>

<TargetFrameworks>net5.0</TargetFrameworks>
<Version>0.7.1</Version>
<Version>0.7.2-beta.1</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
Expand Down
2 changes: 1 addition & 1 deletion BungieSharper.Tests/BungieSharper.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<IsPackable>false</IsPackable>

<TargetFramework>net5.0</TargetFramework>
<Version>0.7.1</Version>
<Version>0.7.2-beta.1</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion BungieSharper/BungieSharper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<Nullable>enable</Nullable>

<TargetFrameworks>net5.0</TargetFrameworks>
<Version>0.7.1</Version>
<Version>0.7.2-beta.1</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
Expand Down
38 changes: 22 additions & 16 deletions BungieSharper/Client/ApiAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ internal ApiAccessor()
var httpClientHandler = new HttpClientHandler
{
CookieContainer = cookieContainer,
UseCookies = true
UseCookies = true,
MaxConnectionsPerServer = SimultaneousRequests
};

_httpClient = new HttpClient(httpClientHandler)
Expand Down Expand Up @@ -62,9 +63,13 @@ internal void SetApiKey(string apiKey)
_httpClient.DefaultRequestHeaders.Add("X-API-Key", apiKey);
}

internal void SetUserAgent(string userAgent)
internal void RemoveUserAgent()
{
_httpClient.DefaultRequestHeaders.Remove("User-Agent");
}

internal void AddUserAgent(string userAgent)
{
_httpClient.DefaultRequestHeaders.Add("User-Agent", userAgent);
}

Expand All @@ -73,15 +78,15 @@ internal void SetRetryErrorCodes(List<PlatformErrorCodes> errorCodes)
_retryErrorCodes = errorCodes;
}

internal void SetRateLimit(ushort requestsPerSecond)
internal void SetRateLimit(byte requestsPerSecond)
{
_msPerRequest = TimeSpan.FromMilliseconds(1000.0 / requestsPerSecond * SimultaneousRequests);
}

internal async Task<T> ApiRequestAsync<T>(Uri uri, HttpContent? httpContent, HttpMethod httpMethod, string? authToken, AuthHeaderType authType, CancellationToken cancelToken)
internal async Task<T> ApiRequestAsync<T>(Uri uri, HttpContent? httpContent, HttpMethod httpMethod, string? authToken, CancellationToken cancelToken)
{
var semaphoreTask = _semaphore.WaitAsync(cancelToken).ConfigureAwait(false);
var httpRequestMessage = HttpRequestGenerator.MakeApiRequestMessage(uri, httpContent, httpMethod, authToken, authType);
var httpRequestMessage = HttpRequestGenerator.MakeApiRequestMessage(uri, httpContent, httpMethod, authToken);
await semaphoreTask;

while (true)
Expand All @@ -98,7 +103,7 @@ internal async Task<T> ApiRequestAsync<T>(Uri uri, HttpContent? httpContent, Htt
if (httpResponseMessage.Content.Headers.ContentType?.MediaType != "application/json")
{
await AwaitThrottleAndReleaseSemaphore(throttleTask, _semaphore).ConfigureAwait(false);
throw new ContentNotJsonException(httpResponseMessage);
throw ContentNotJsonException.NewContentNotJsonException(httpResponseMessage);
}

var apiResponse = JsonSerializer.Deserialize<Entities.ApiResponse<T>>(
Expand All @@ -107,7 +112,8 @@ await httpResponseMessage.Content.ReadAsStringAsync(cancelToken).ConfigureAwait(

if (apiResponse is null)
{
throw new ContentNullJsonException(httpResponseMessage);
await AwaitThrottleAndReleaseSemaphore(throttleTask, _semaphore).ConfigureAwait(false);
throw ContentNullJsonException.NewContentNullJsonException(httpResponseMessage);
}

if (apiResponse.ErrorCode != PlatformErrorCodes.Success)
Expand All @@ -120,16 +126,15 @@ await httpResponseMessage.Content.ReadAsStringAsync(cancelToken).ConfigureAwait(
else
{
await AwaitThrottleAndReleaseSemaphore(throttleTask, _semaphore).ConfigureAwait(false);

throw new NonRetryErrorCodeException(apiResponse);
throw NonRetryErrorCodeException.NewNonRetryErrorCodeException(apiResponse);
}
}
else
{
if (apiResponse.Response == null)
{
await AwaitThrottleAndReleaseSemaphore(throttleTask, _semaphore).ConfigureAwait(false);
throw new NullResponseException(apiResponse);
throw NullResponseException.NewNullResponseException(apiResponse);
}

await AwaitThrottleAndReleaseSemaphore(throttleTask, _semaphore).ConfigureAwait(false);
Expand All @@ -138,10 +143,10 @@ await httpResponseMessage.Content.ReadAsStringAsync(cancelToken).ConfigureAwait(
}
}

internal async Task<Entities.TokenResponse> ApiTokenRequestResponseAsync(Uri uri, HttpContent httpContent, HttpMethod httpMethod, string? authToken, AuthHeaderType authType, CancellationToken cancelToken)
internal async Task<Entities.TokenResponse> ApiTokenRequestResponseAsync(Uri uri, HttpContent httpContent, HttpMethod httpMethod, CancellationToken cancelToken)
{
var semaphoreTask = _semaphore.WaitAsync(cancelToken).ConfigureAwait(false);
var httpRequestMessage = HttpRequestGenerator.MakeApiRequestMessage(uri, httpContent, httpMethod, authToken, authType);
var httpRequestMessage = HttpRequestGenerator.MakeApiRequestMessage(uri, httpContent, httpMethod);
await semaphoreTask;

while (true)
Expand All @@ -158,7 +163,7 @@ await httpResponseMessage.Content.ReadAsStringAsync(cancelToken).ConfigureAwait(
if (httpResponseMessage.Content.Headers.ContentType?.MediaType != "application/json")
{
await AwaitThrottleAndReleaseSemaphore(throttleTask, _semaphore).ConfigureAwait(false);
throw new ContentNotJsonException(httpResponseMessage);
throw ContentNotJsonException.NewContentNotJsonException(httpResponseMessage);
}

var apiResponse = JsonSerializer.Deserialize<Entities.TokenResponse>(
Expand All @@ -167,7 +172,8 @@ await httpResponseMessage.Content.ReadAsStringAsync(cancelToken).ConfigureAwait(

if (apiResponse is null)
{
throw new ContentNullJsonException(httpResponseMessage);
await AwaitThrottleAndReleaseSemaphore(throttleTask, _semaphore).ConfigureAwait(false);
throw ContentNullJsonException.NewContentNullJsonException(httpResponseMessage);
}

await AwaitThrottleAndReleaseSemaphore(throttleTask, _semaphore).ConfigureAwait(false);
Expand All @@ -177,14 +183,14 @@ await httpResponseMessage.Content.ReadAsStringAsync(cancelToken).ConfigureAwait(

internal async Task<Stream> GetStream(Uri uri, CancellationToken cancelToken)
{
var requestMsg = HttpRequestGenerator.MakeApiRequestMessage(uri, null, HttpMethod.Get, null, AuthHeaderType.None);
var requestMsg = HttpRequestGenerator.MakeApiRequestMessage(uri, null, HttpMethod.Get);
var response = await GetApiResponseAsync(requestMsg, cancelToken);
return await response.Content.ReadAsStreamAsync(cancelToken);
}

internal async Task<string> GetString(Uri uri, CancellationToken cancelToken)
{
var requestMsg = HttpRequestGenerator.MakeApiRequestMessage(uri, null, HttpMethod.Get, null, AuthHeaderType.None);
var requestMsg = HttpRequestGenerator.MakeApiRequestMessage(uri, null, HttpMethod.Get);
var response = await GetApiResponseAsync(requestMsg, cancelToken);
return await response.Content.ReadAsStringAsync(cancelToken);
}
Expand Down
Loading

0 comments on commit 6dd33ee

Please sign in to comment.