public async Task <Offering> RequestAsync(OfferingRequest request, IVendorCredentials credentials) { if (request == null) { throw new ArgumentNullException(nameof(request)); } Offering offering = null; var client = _httpClientCreator.Create(credentials); var webRequest = new HttpRequestMessage(HttpMethod.Post, _configuration.ServiceUrl) { Content = new StringContent(_serializer.Serialize(request), Encoding.UTF8, "application/json") }; webRequest.Headers.Add(AuthTokenHeaderKey, _authTokenGenerator.Generate(credentials.Id, credentials.SharedSecret)); var response = await client.SendAsync(webRequest).ConfigureAwait(false); if (response.IsSuccessStatusCode) { offering = _deserializer.Deserialize <Offering>(response.Content.ReadAsStringAsync().Result); offering.Success = true; } else { offering = ProcessUnsuccessfulRequest(response); } return(offering); }
public void When_Credentials_Are_Null_Then_Create_Throws_ArgumentNullException() { // set up var client = new HttpClientCreator(new ProxyConfiguration()); IVendorCredentials credentials = null; // execute var ex = Assert.Throws <ArgumentNullException>(() => client.Create(credentials)); // verify Assert.Equal("credentials", ex.ParamName); }
public HttpClient Create(IVendorCredentials credentials) { if (credentials == null) { throw new ArgumentNullException(nameof(credentials)); } // reuse HttpClient for each vendor & proxy combination var key = string.Concat(credentials.Id.ToString("N"), _proxyConfiguration.Address, _proxyConfiguration.Username); if (!Clients.ContainsKey(key)) { lock (SyncLock) { if (!Clients.ContainsKey(key)) { var handler = new HttpClientHandler(); if (_proxyConfiguration.Enabled) { handler.UseProxy = true; handler.Proxy = new WebProxy(_proxyConfiguration.Address); if (string.IsNullOrWhiteSpace(_proxyConfiguration.Username)) { handler.UseDefaultCredentials = true; } else { handler.UseDefaultCredentials = false; handler.Credentials = new NetworkCredential(_proxyConfiguration.Username, _proxyConfiguration.Password); } } var client = new HttpClient(handler); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Add(VendorIdHeaderKey, credentials.Id.ToString("D")); Clients.Add(key, client); } } } return(Clients[key]); }
public async Task <OfferingSaleCancellationResponse> CancelAsync(Guid offeringId, IVendorCredentials credentials) { if (offeringId == Guid.Empty) { throw new ArgumentNullException(nameof(offeringId)); } if (credentials == null) { throw new ArgumentNullException(nameof(credentials)); } var client = _httpClientCreator.Create(credentials); var request = new HttpRequestMessage(HttpMethod.Post, _configuration.ServiceUrl) { Content = new StringContent(_serializer.Serialize(new OfferingSaleCancellationRequest { OfferingId = offeringId }), Encoding.UTF8, "application/json") }; request.Headers.Add(AuthTokenHeaderKey, _authTokenGenerator.Generate(credentials.Id, credentials.SharedSecret)); var response = await client.SendAsync(request).ConfigureAwait(false);; OfferingSaleCancellationResponse cancellationResponse; if (!response.IsSuccessStatusCode) { cancellationResponse = ProcessUnsuccessfulRequest(response); } else { cancellationResponse = new OfferingSaleCancellationResponse { Success = true }; } return(cancellationResponse); }
public OfferingSaleCancellationResponse Cancel(Guid offeringId, IVendorCredentials credentials) { return(CancelAsync(offeringId, credentials).Result); }
public OfferingSaleCanceller(IOfferingSaleCancellerConfiguration configuration, ISerializer serializer, IDeserializer deserializer, IHttpClientCreator httpClientCreator, IAuthTokenGenerator authTokenGenerator, IVendorCredentials defaultCredentials) : this(configuration, serializer, deserializer, httpClientCreator, authTokenGenerator) { _defaultCredentials = defaultCredentials; }
public OfferingResultWriteResponse Write(OfferingResult offeringResult, IVendorCredentials credentials) { return(WriteAsync(offeringResult, credentials).Result); }
public Offering Request(OfferingRequest request, IVendorCredentials credentials) { return(RequestAsync(request, credentials).Result); }