|
| 1 | +using System.Net.Http; |
| 2 | +using System.Text.Json; |
| 3 | +using System.Threading; |
| 4 | +using Vapi.Client.Core; |
| 5 | + |
| 6 | +#nullable enable |
| 7 | + |
| 8 | +namespace Vapi.Client; |
| 9 | + |
| 10 | +public partial class LogsClient |
| 11 | +{ |
| 12 | + private RawClient _client; |
| 13 | + |
| 14 | + internal LogsClient(RawClient client) |
| 15 | + { |
| 16 | + _client = client; |
| 17 | + } |
| 18 | + |
| 19 | + /// <example> |
| 20 | + /// <code> |
| 21 | + /// await client.Logs.GetAsync(new LogsGetRequest()); |
| 22 | + /// </code> |
| 23 | + /// </example> |
| 24 | + public async Task<LogsPaginatedResponse> GetAsync( |
| 25 | + LogsGetRequest request, |
| 26 | + RequestOptions? options = null, |
| 27 | + CancellationToken cancellationToken = default |
| 28 | + ) |
| 29 | + { |
| 30 | + var _query = new Dictionary<string, object>(); |
| 31 | + if (request.OrgId != null) |
| 32 | + { |
| 33 | + _query["orgId"] = request.OrgId; |
| 34 | + } |
| 35 | + if (request.Type != null) |
| 36 | + { |
| 37 | + _query["type"] = request.Type.Value.Stringify(); |
| 38 | + } |
| 39 | + if (request.AssistantId != null) |
| 40 | + { |
| 41 | + _query["assistantId"] = request.AssistantId; |
| 42 | + } |
| 43 | + if (request.PhoneNumberId != null) |
| 44 | + { |
| 45 | + _query["phoneNumberId"] = request.PhoneNumberId; |
| 46 | + } |
| 47 | + if (request.CustomerId != null) |
| 48 | + { |
| 49 | + _query["customerId"] = request.CustomerId; |
| 50 | + } |
| 51 | + if (request.SquadId != null) |
| 52 | + { |
| 53 | + _query["squadId"] = request.SquadId; |
| 54 | + } |
| 55 | + if (request.CallId != null) |
| 56 | + { |
| 57 | + _query["callId"] = request.CallId; |
| 58 | + } |
| 59 | + if (request.Page != null) |
| 60 | + { |
| 61 | + _query["page"] = request.Page.ToString(); |
| 62 | + } |
| 63 | + if (request.SortOrder != null) |
| 64 | + { |
| 65 | + _query["sortOrder"] = request.SortOrder.Value.Stringify(); |
| 66 | + } |
| 67 | + if (request.Limit != null) |
| 68 | + { |
| 69 | + _query["limit"] = request.Limit.ToString(); |
| 70 | + } |
| 71 | + if (request.CreatedAtGt != null) |
| 72 | + { |
| 73 | + _query["createdAtGt"] = request.CreatedAtGt.Value.ToString(Constants.DateTimeFormat); |
| 74 | + } |
| 75 | + if (request.CreatedAtLt != null) |
| 76 | + { |
| 77 | + _query["createdAtLt"] = request.CreatedAtLt.Value.ToString(Constants.DateTimeFormat); |
| 78 | + } |
| 79 | + if (request.CreatedAtGe != null) |
| 80 | + { |
| 81 | + _query["createdAtGe"] = request.CreatedAtGe.Value.ToString(Constants.DateTimeFormat); |
| 82 | + } |
| 83 | + if (request.CreatedAtLe != null) |
| 84 | + { |
| 85 | + _query["createdAtLe"] = request.CreatedAtLe.Value.ToString(Constants.DateTimeFormat); |
| 86 | + } |
| 87 | + if (request.UpdatedAtGt != null) |
| 88 | + { |
| 89 | + _query["updatedAtGt"] = request.UpdatedAtGt.Value.ToString(Constants.DateTimeFormat); |
| 90 | + } |
| 91 | + if (request.UpdatedAtLt != null) |
| 92 | + { |
| 93 | + _query["updatedAtLt"] = request.UpdatedAtLt.Value.ToString(Constants.DateTimeFormat); |
| 94 | + } |
| 95 | + if (request.UpdatedAtGe != null) |
| 96 | + { |
| 97 | + _query["updatedAtGe"] = request.UpdatedAtGe.Value.ToString(Constants.DateTimeFormat); |
| 98 | + } |
| 99 | + if (request.UpdatedAtLe != null) |
| 100 | + { |
| 101 | + _query["updatedAtLe"] = request.UpdatedAtLe.Value.ToString(Constants.DateTimeFormat); |
| 102 | + } |
| 103 | + var response = await _client.MakeRequestAsync( |
| 104 | + new RawClient.JsonApiRequest |
| 105 | + { |
| 106 | + BaseUrl = _client.Options.BaseUrl, |
| 107 | + Method = HttpMethod.Get, |
| 108 | + Path = "logs", |
| 109 | + Query = _query, |
| 110 | + Options = options, |
| 111 | + }, |
| 112 | + cancellationToken |
| 113 | + ); |
| 114 | + var responseBody = await response.Raw.Content.ReadAsStringAsync(); |
| 115 | + if (response.StatusCode is >= 200 and < 400) |
| 116 | + { |
| 117 | + try |
| 118 | + { |
| 119 | + return JsonUtils.Deserialize<LogsPaginatedResponse>(responseBody)!; |
| 120 | + } |
| 121 | + catch (JsonException e) |
| 122 | + { |
| 123 | + throw new VapiException("Failed to deserialize response", e); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + throw new VapiApiException( |
| 128 | + $"Error with status code {response.StatusCode}", |
| 129 | + response.StatusCode, |
| 130 | + responseBody |
| 131 | + ); |
| 132 | + } |
| 133 | +} |
0 commit comments