2 * Copyright(c) 2024 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 using System.Net.Http.Headers;
20 using System.Net.Http;
22 using System.Threading.Tasks;
23 using System.ComponentModel;
25 namespace Tizen.AIAvatar
27 internal class RestClient : IRestClient, IDisposable
29 private readonly HttpClient client;
31 internal RestClient(HttpClient httpClient)
36 [EditorBrowsable(EditorBrowsableState.Never)]
37 public async Task<string> SendRequestAsync(HttpMethod method, string endpoint, string bearerToken = null, string jsonData = null)
39 AddBearerToken(bearerToken);
41 HttpRequestMessage request = new HttpRequestMessage(method, endpoint);
45 request.Content = new StringContent(jsonData, Encoding.UTF8, "application/json");
48 HttpResponseMessage response = await client.SendAsync(request);
50 return await HandleResponse(response);
53 [EditorBrowsable(EditorBrowsableState.Never)]
59 private void AddBearerToken(string bearerToken)
61 if (!string.IsNullOrEmpty(bearerToken))
63 client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", bearerToken);
67 private async Task<string> HandleResponse(HttpResponseMessage response)
69 if (response.IsSuccessStatusCode)
71 return await response.Content.ReadAsStringAsync();
75 throw new HttpRequestException($"HTTP request failed with status code {response.StatusCode}");