|
| 1 | +using System.Net.Http.Headers; |
| 2 | +using System.Text; |
| 3 | +using System.Text.Json; |
| 4 | +using System.Text.Json.Serialization; |
| 5 | + |
| 6 | +namespace Termii; |
| 7 | + |
| 8 | +internal sealed class TermiiContactClient : ITermiiContactClient |
| 9 | +{ |
| 10 | + private static readonly JsonSerializerOptions JsonSerializerOptions = new(JsonSerializerDefaults.Web) |
| 11 | + { |
| 12 | + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, |
| 13 | + }; |
| 14 | + |
| 15 | + private readonly TermiiJsonHttpPipeline _pipeline; |
| 16 | + |
| 17 | + public TermiiContactClient(TermiiJsonHttpPipeline pipeline) |
| 18 | + { |
| 19 | + _pipeline = pipeline ?? throw new ArgumentNullException(nameof(pipeline)); |
| 20 | + } |
| 21 | + |
| 22 | + public Task<ContactListResponse> GetAsync( |
| 23 | + string phonebookId, |
| 24 | + GetContactsRequest? request = null, |
| 25 | + CancellationToken cancellationToken = default) |
| 26 | + { |
| 27 | + TermiiRequestValidation.Required(phonebookId, nameof(phonebookId)); |
| 28 | + |
| 29 | + return _pipeline.SendJsonAsync<ContactListResponse>( |
| 30 | + HttpMethod.Get, |
| 31 | + (request ?? new GetContactsRequest()).ToPath(phonebookId), |
| 32 | + body: null, |
| 33 | + TermiiAuthenticationLocation.Query, |
| 34 | + cancellationToken); |
| 35 | + } |
| 36 | + |
| 37 | + public Task<ContactResponse> AddAsync( |
| 38 | + string phonebookId, |
| 39 | + AddContactRequest request, |
| 40 | + CancellationToken cancellationToken = default) |
| 41 | + { |
| 42 | + TermiiRequestValidation.Required(phonebookId, nameof(phonebookId)); |
| 43 | + |
| 44 | + if (request is null) |
| 45 | + { |
| 46 | + throw new ArgumentNullException(nameof(request)); |
| 47 | + } |
| 48 | + |
| 49 | + request.Validate(); |
| 50 | + |
| 51 | + return _pipeline.SendJsonAsync<ContactResponse>( |
| 52 | + HttpMethod.Post, |
| 53 | + $"/api/phonebooks/{Uri.EscapeDataString(phonebookId)}/contacts", |
| 54 | + request, |
| 55 | + TermiiAuthenticationLocation.Body, |
| 56 | + cancellationToken); |
| 57 | + } |
| 58 | + |
| 59 | + public Task<ContactUploadResponse> UploadAsync( |
| 60 | + UploadContactsRequest request, |
| 61 | + CancellationToken cancellationToken = default) |
| 62 | + { |
| 63 | + if (request is null) |
| 64 | + { |
| 65 | + throw new ArgumentNullException(nameof(request)); |
| 66 | + } |
| 67 | + |
| 68 | + request.Validate(); |
| 69 | + |
| 70 | + var content = new MultipartFormDataContent(); |
| 71 | + var fileContent = new StreamContent(request.File); |
| 72 | + fileContent.Headers.ContentType = new MediaTypeHeaderValue(request.ContentType); |
| 73 | + content.Add(fileContent, "file", request.FileName); |
| 74 | + |
| 75 | + var contact = new |
| 76 | + { |
| 77 | + pid = request.PhonebookId, |
| 78 | + country_code = request.CountryCode, |
| 79 | + api_key = _pipeline.ApiKey, |
| 80 | + }; |
| 81 | + content.Add( |
| 82 | + new StringContent(JsonSerializer.Serialize(contact, JsonSerializerOptions), Encoding.UTF8, "application/json"), |
| 83 | + "contact"); |
| 84 | + |
| 85 | + return _pipeline.SendContentJsonAsync<ContactUploadResponse>( |
| 86 | + HttpMethod.Post, |
| 87 | + "/api/phonebooks/contacts/upload", |
| 88 | + content, |
| 89 | + cancellationToken); |
| 90 | + } |
| 91 | + |
| 92 | + public Task<ContactOperationResponse> DeleteAsync( |
| 93 | + string phonebookId, |
| 94 | + CancellationToken cancellationToken = default) |
| 95 | + { |
| 96 | + TermiiRequestValidation.Required(phonebookId, nameof(phonebookId)); |
| 97 | + |
| 98 | + return _pipeline.SendJsonAsync<ContactOperationResponse>( |
| 99 | + HttpMethod.Delete, |
| 100 | + $"/api/phonebooks/{Uri.EscapeDataString(phonebookId)}/contacts", |
| 101 | + body: null, |
| 102 | + TermiiAuthenticationLocation.Query, |
| 103 | + cancellationToken); |
| 104 | + } |
| 105 | +} |
0 commit comments