Generation

generate functionSun, 19 Mar 2023

Extension method for httpclient api calls sending an endpoint, httpmethod, and content data. Furthermore create a configuration class to configure auth type

public static async Task<T> ExecuteRequest<T>(this HttpClient client, string endpoint, HttpMethod httpMethod, string contentData) { try { var request = new HttpRequestMessage(httpMethod, $"{_baseURL}/{endpoint}"); if (httpMethod != HttpMethod.Get) { request.Content = new StringContent(contentData, Encoding.UTF8, "application/json"); } switch (_authType) { case AuthType.Basic: var enc = Encoding.ASCII.GetBytes($"{_username}:{_password}"); request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(enc)); break; case AuthType.Bearer: request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _token); break; case AuthType.Cookie: request.Headers.Add("Cookie

Questions about programming?Chat with your personal AI assistant