Skip to content

Commit

Permalink
Added Rate limited exception
Browse files Browse the repository at this point in the history
  • Loading branch information
craftersmine authored Mar 2, 2023
2 parents 71497be + 066d191 commit 07a5614
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="craftersmine.SteamGridDB.Net" Version="1.1.2" />
<ProjectReference Include="..\craftersmine.SteamGridDB.Net\craftersmine.SteamGridDB.Net.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ public enum ExceptionType
/// </summary>
Forbidden = 403,
/// <summary>
/// When you are rate limited on the server
/// </summary>
RateLimited = 429,
/// <summary>
/// When unknown error occurred
/// </summary>
Unknown = 0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Text;

namespace craftersmine.SteamGridDBNet.Exceptions
{
/// <summary>
/// The exception that is thrown when you are being rate-limited on server
/// </summary>
[Serializable]
public class SteamGridDbRateLimitedException : SteamGridDbException
{
public TimeSpan RetryAfter { get; private set; }

/// <inheritdoc cref="SteamGridDbException"/>
public SteamGridDbRateLimitedException(TimeSpan retryAfter)
{
RetryAfter = retryAfter;
}

/// <inheritdoc cref="SteamGridDbException"/>
public SteamGridDbRateLimitedException(string message, TimeSpan retryAfter) : base(message)
{
RetryAfter = retryAfter;
}

/// <inheritdoc cref="SteamGridDbException"/>
public SteamGridDbRateLimitedException(string message, TimeSpan retryAfter, Exception inner) : base(message, inner)
{
RetryAfter = retryAfter;
}

/// <inheritdoc cref="SteamGridDbException"/>
protected SteamGridDbRateLimitedException(
SerializationInfo info,
StreamingContext context) : base(info, context)
{
}
}
}
9 changes: 9 additions & 0 deletions craftersmine.SteamGridDB.Net/Resources/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions craftersmine.SteamGridDB.Net/Resources/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@
<data name="Exception.NotFound" xml:space="preserve">
<value>Requested grid/hero/icon/logo/game not found on SteamGridDB.</value>
</data>
<data name="Exception.RateLimited" xml:space="preserve">
<value>You are rate limited by the server! Retry after: {0}</value>
</data>
<data name="Exception.Unauthorized" xml:space="preserve">
<value>Unauthorized request to SteamGridDB. Probably due to missing or invalid API key.</value>
</data>
Expand Down
60 changes: 60 additions & 0 deletions craftersmine.SteamGridDB.Net/SteamGridDb.cs

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions craftersmine.SteamGridDB.Net/SteamGridDbObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public class SteamGridDbObject
/// <returns></returns>
/// <exception cref="SteamGridDbException">When unknown exception occurred</exception>
/// <exception cref="SteamGridDbImageException">When error occurred while downloading image</exception>
/// <exception cref="SteamGridDbRateLimitedException">When you've been rate limited by the server</exception>
/// <exception cref="ArgumentNullException">When image URL is null</exception>
public async Task<Stream> GetImageAsStreamAsync(bool thumbnail)
{
Expand All @@ -131,6 +132,13 @@ public async Task<Stream> GetImageAsStreamAsync(bool thumbnail)
if (respObj is null)
throw new SteamGridDbImageException(ExceptionType.Unknown,
Resources.Resources.Exception_UnknownImageException);

if (!(resp.Headers.RetryAfter is null) && resp.Headers.RetryAfter.Delta.HasValue)
{
throw new SteamGridDbRateLimitedException(
string.Format(Resources.Resources.Exception_RateLimited,
resp.Headers.RetryAfter.Delta.ToString()), resp.Headers.RetryAfter.Delta.Value) {ExceptionType = ExceptionType.RateLimited };
}

switch (resp.StatusCode)
{
Expand Down Expand Up @@ -162,6 +170,9 @@ public async Task<Stream> GetImageAsStreamAsync(bool thumbnail)
/// Downloads full image to specified file
/// </summary>
/// <param name="filePath">Full path of file to download</param>
/// <exception cref="SteamGridDbException">When unknown exception occurred</exception>
/// <exception cref="SteamGridDbImageException">When error occurred while downloading image</exception>
/// <exception cref="SteamGridDbRateLimitedException">When you've been rate limited by the server</exception>
/// <exception cref="UnauthorizedAccessException">When access to file is forbidden</exception>
/// <exception cref="ArgumentException">When path is empty, null, has only whitespaces or has invalid characters</exception>
/// <exception cref="ArgumentNullException">When path is empty, null or has only whitespaces</exception>
Expand All @@ -181,6 +192,9 @@ public async void DownloadToFileAsync(string filePath)
/// Downloads thumbnail image to specified file
/// </summary>
/// <param name="filePath">Full path of file to download</param>
/// <exception cref="SteamGridDbException">When unknown exception occurred</exception>
/// <exception cref="SteamGridDbImageException">When error occurred while downloading image</exception>
/// <exception cref="SteamGridDbRateLimitedException">When you've been rate limited by the server</exception>
/// <exception cref="UnauthorizedAccessException">When access to file is forbidden</exception>
/// <exception cref="ArgumentException">When path is empty, null, has only whitespaces or has invalid characters</exception>
/// <exception cref="ArgumentNullException">When path is empty, null or has only whitespaces</exception>
Expand All @@ -204,6 +218,7 @@ public async void DownloadThumbnailToFileAsync(string filePath)
/// <exception cref="SteamGridDbUnauthorizedException">When your API key is invalid, not set, or you've reset it on API preferences page and use old one</exception>
/// <exception cref="SteamGridDbBadRequestException">When library makes invalid request to server due to invalid URI generated</exception>
/// <exception cref="SteamGridDbForbiddenException">When you don't have permissions to perform action on item, probably because you don't own item</exception>
/// <exception cref="SteamGridDbRateLimitedException">When you've been rate limited by the server</exception>
/// <exception cref="SteamGridDbException">When unknown exception occurred in request</exception>
public async Task<bool> DeleteFromServerAsync()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFramework>netstandard2.0</TargetFramework>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<RootNamespace>craftersmine.SteamGridDBNet</RootNamespace>
<Version>1.1.3</Version>
<Version>1.1.4</Version>
<Authors>craftersmine</Authors>
<Description>A .NET implementation of SteamGridDB API</Description>
<Copyright>Copyright © craftersmine 2022</Copyright>
Expand All @@ -18,8 +18,7 @@
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
<IncludeSymbols>True</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<DebugSymbols>True</DebugSymbols>
<DebugType>full</DebugType>
<DebugType>portable</DebugType>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 07a5614

Please sign in to comment.