Skip to content

Commit

Permalink
Added ticket inform operation
Browse files Browse the repository at this point in the history
  • Loading branch information
zb-sr committed Jun 18, 2024
1 parent 899e98a commit fe60ade
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
28 changes: 27 additions & 1 deletion src/Sportradar.Mbs.Sdk/Internal/Connection/TokenProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using Sportradar.Mbs.Sdk.Exceptions;
Expand Down Expand Up @@ -127,7 +128,7 @@ internal async Task<string> GetAuthTokenAsync(CancellationToken cancellationToke

var response = JsonSerializer.Deserialize<AuthResponse>(content);
if (response == null || string.IsNullOrEmpty(response.AccessToken))
return null;
return ProcessPossibleError(response);

var token = response.AccessToken.NotNull();
if (response.ExpiresIn is > 1)
Expand All @@ -139,6 +140,27 @@ internal async Task<string> GetAuthTokenAsync(CancellationToken cancellationToke
return token;
}

private string? ProcessPossibleError(AuthResponse? authResponse)
{
if (authResponse == null) return null;

if (string.IsNullOrEmpty(authResponse.Error) && string.IsNullOrEmpty(authResponse.ErrorDescription))
return null;

var msg = new StringBuilder();
msg.Append("Auth error");
if (!string.IsNullOrEmpty(authResponse.Error))
{
msg.Append(": ").Append(authResponse.Error.NotNull());
}
if (!string.IsNullOrEmpty(authResponse.ErrorDescription))
{
msg.Append(": ").Append(authResponse.ErrorDescription.NotNull());
}

throw new AuthTokenFailureException(msg.ToString());
}

public class AuthResponse
{
[JsonPropertyName("access_token")]
Expand All @@ -151,5 +173,9 @@ public class AuthResponse
public string? RefreshToken { get; set; }
[JsonPropertyName("scope")]
public string? Scope { get; set; }
[JsonPropertyName("error")]
public string? Error { get; set; }
[JsonPropertyName("error_description")]
public string? ErrorDescription { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public async Task<TicketResponse> SendTicketAsync(TicketRequest request)
return await ProcessRequestAsync<TicketResponse>("ticket-placement", request).ConfigureAwait(false);
}

public async Task<TicketInformResponse> SendTicketInformAsync(TicketInformRequest request)
{
return await ProcessRequestAsync<TicketInformResponse>("ticket-placement-inform", request).ConfigureAwait(false);
}

public async Task<TicketAckResponse> SendTicketAckAsync(TicketAckRequest request)
{
return await ProcessRequestAsync<TicketAckResponse>("ticket-placement-ack", request).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal class DecimalJsonConverter : JsonConverter<decimal>
public override decimal Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var jsonVal = reader.GetString();
if (decimal.TryParse(jsonVal, out var result)) return result;
if (decimal.TryParse(jsonVal, NumberStyles.Number, CultureInfo.InvariantCulture, out var result)) return result;

throw new JsonException("Unknown decimal: " + jsonVal);
}
Expand Down
8 changes: 8 additions & 0 deletions src/Sportradar.Mbs.Sdk/Protocol/ITicketProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ public interface ITicketProtocol
/// <exception cref="SdkException">Thrown when operation has failed.</exception>
Task<TicketResponse> SendTicketAsync(TicketRequest request);

/// <summary>
/// Sends a ticket inform request asynchronously.
/// </summary>
/// <param name="request">The ticket inform request to send.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the ticket inform response.</returns>
/// <exception cref="SdkException">Thrown when operation has failed.</exception>
Task<TicketInformResponse> SendTicketInformAsync(TicketInformRequest request);

/// <summary>
/// Sends a ticket acknowledgement request asynchronously.
/// </summary>
Expand Down

0 comments on commit fe60ade

Please sign in to comment.