|
| 1 | +namespace Comanda.Internal.Contracts.Clients; |
| 2 | + |
| 3 | +public sealed class CredentialsClient(HttpClient httpClient) : ICredentialsClient |
| 4 | +{ |
| 5 | + private readonly JsonSerializerOptions serializerOptions = new() |
| 6 | + { |
| 7 | + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, |
| 8 | + PropertyNameCaseInsensitive = true |
| 9 | + }; |
| 10 | + |
| 11 | + public async Task<Result<IReadOnlyCollection<CredentialScheme>>> GetCredentialsAsync( |
| 12 | + CredentialsFetchParameters parameters, CancellationToken cancellation = default) |
| 13 | + { |
| 14 | + var response = await httpClient.GetAsync($"establishments/{parameters.EstablishmentId}/integrations/credentials", cancellation); |
| 15 | + var content = await response.Content.ReadAsStringAsync(cancellation); |
| 16 | + |
| 17 | + var error = response.StatusCode switch |
| 18 | + { |
| 19 | + HttpStatusCode.NotFound => EstablishmentErrors.EstablishmentDoesNotExist, |
| 20 | + HttpStatusCode.Unauthorized => CommonErrors.UnauthorizedAccess, |
| 21 | + HttpStatusCode.Forbidden => CommonErrors.UnauthorizedAccess, |
| 22 | + HttpStatusCode.InternalServerError => CommonErrors.OperationFailed, |
| 23 | + |
| 24 | + _ => null |
| 25 | + }; |
| 26 | + |
| 27 | + if (error is not null) |
| 28 | + { |
| 29 | + return Result<IReadOnlyCollection<CredentialScheme>>.Failure(error); |
| 30 | + } |
| 31 | + |
| 32 | + var credentials = JsonSerializer.Deserialize<IReadOnlyCollection<CredentialScheme>>(content, serializerOptions); |
| 33 | + if (credentials is null) |
| 34 | + { |
| 35 | + return Result<IReadOnlyCollection<CredentialScheme>>.Failure(CommonErrors.InvalidContent); |
| 36 | + } |
| 37 | + |
| 38 | + return Result<IReadOnlyCollection<CredentialScheme>>.Success(credentials); |
| 39 | + } |
| 40 | + |
| 41 | + public async Task<Result<CredentialScheme>> AssignIntegrationCredentialAsync( |
| 42 | + CredentialCreationScheme parameters, CancellationToken cancellation = default) |
| 43 | + { |
| 44 | + var response = await httpClient.PostAsJsonAsync($"establishments/{parameters.EstablishmentId}/integrations/credentials", parameters, cancellation); |
| 45 | + var content = await response.Content.ReadAsStringAsync(cancellation); |
| 46 | + |
| 47 | + var error = response.StatusCode switch |
| 48 | + { |
| 49 | + HttpStatusCode.NotFound => EstablishmentErrors.EstablishmentDoesNotExist, |
| 50 | + HttpStatusCode.Unauthorized => CommonErrors.UnauthorizedAccess, |
| 51 | + HttpStatusCode.Forbidden => CommonErrors.UnauthorizedAccess, |
| 52 | + HttpStatusCode.InternalServerError => CommonErrors.OperationFailed, |
| 53 | + |
| 54 | + _ => null |
| 55 | + }; |
| 56 | + |
| 57 | + if (error is not null) |
| 58 | + { |
| 59 | + return Result<CredentialScheme>.Failure(error); |
| 60 | + } |
| 61 | + |
| 62 | + var credential = JsonSerializer.Deserialize<CredentialScheme>(content, serializerOptions); |
| 63 | + if (credential is null) |
| 64 | + { |
| 65 | + return Result<CredentialScheme>.Failure(CommonErrors.InvalidContent); |
| 66 | + } |
| 67 | + |
| 68 | + return Result<CredentialScheme>.Success(credential); |
| 69 | + } |
| 70 | + |
| 71 | + public async Task<Result<CredentialScheme>> UpdateCredentialAsync( |
| 72 | + CredentialModificationScheme parameters, CancellationToken cancellation = default) |
| 73 | + { |
| 74 | + var response = await httpClient.PutAsJsonAsync($"establishments/{parameters.EstablishmentId}/integrations/credentials/{parameters.CredentialId}", parameters, cancellation); |
| 75 | + var content = await response.Content.ReadAsStringAsync(cancellation); |
| 76 | + |
| 77 | + if (!response.IsSuccessStatusCode) |
| 78 | + { |
| 79 | + var error = JsonSerializer.Deserialize<Error>(content, serializerOptions); |
| 80 | + if (error is not null) |
| 81 | + return Result<CredentialScheme>.Failure(error); |
| 82 | + |
| 83 | + return Result<CredentialScheme>.Failure(CommonErrors.OperationFailed); |
| 84 | + } |
| 85 | + |
| 86 | + var credential = JsonSerializer.Deserialize<CredentialScheme>(content, serializerOptions); |
| 87 | + if (credential is null) |
| 88 | + { |
| 89 | + return Result<CredentialScheme>.Failure(CommonErrors.InvalidContent); |
| 90 | + } |
| 91 | + |
| 92 | + return Result<CredentialScheme>.Success(credential); |
| 93 | + } |
| 94 | +} |
0 commit comments