public Task <IResponse> UpdateUserAsync(User user, EndpointSettings settings) { CheckUserAndThrowIfInvalid(user); var endpoint = Endpoint.TrimEnd('/') + "/" + user.Guid; return(SendAsync <ListActiveDirectoryUserResponse>(ToActiveDirectoryUser(user), (client, content) => client.PatchAsync(endpoint, content), settings)); }
public Task <IResponse> RemoveUserAsync(string guid, EndpointSettings settings) { if (string.IsNullOrEmpty(guid)) { throw new ArgumentException("guid must not be empty or null."); } var endpoint = Endpoint.TrimEnd('/') + "/" + guid; return(SendAsync <ListActiveDirectoryUserResponse>(null, (client, content) => client.DeleteAsync(endpoint), settings)); }
public Task <IResponse> AddUserAsync(User user, EndpointSettings settings) { return(SendAsync <ListActiveDirectoryUserResponse>(ToActiveDirectoryUser(user), (client, content) => client.PostAsync(Endpoint, content), settings)); }
public Task <IResponse> GetUsersAsync(EndpointSettings settings) { return(SendAsync <ListActiveDirectoryUserResponse>(null, (client, content) => client.GetAsync(Endpoint), settings)); }
private async Task <IResponse> SendAsync <T>(IRequest request, Func <HttpClient, StringContent, Task <HttpResponseMessage> > action, EndpointSettings settings) where T : IResponse { using (var client = new HttpClient()) { client.BaseAddress = new Uri(settings.Url); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Add("X-Token", settings.Token); logger.LogDebug($"Endpoint Base-URL is: {client.BaseAddress.ToString()}"); var json = JsonConvert.SerializeObject(request, new JsonSerializerSettings { StringEscapeHandling = StringEscapeHandling.EscapeNonAscii }); var content = new StringContent(json, Encoding.UTF8, "application/json"); var response = await action(client, content); logger.LogDebug($"Got HTTP status code {response.StatusCode}."); try { if (response.IsSuccessStatusCode) { if (response.StatusCode == System.Net.HttpStatusCode.NoContent) { return(new EmptyResponse()); } return(JsonConvert.DeserializeObject <T>(await response.Content.ReadAsStringAsync())); } var responseContent = await response.Content.ReadAsStringAsync(); if (response.StatusCode == System.Net.HttpStatusCode.BadRequest) { return(JsonConvert.DeserializeObject <ViolationListResponse>(responseContent)); } return(JsonConvert.DeserializeObject <ErrorResponse>(responseContent)); } catch (Exception e) { logger.LogError(e, "Failed to send request."); } } return(null); }