protected async Task <T> Post <T>(IEndpoint endpoint, Dictionary <String, String> parameters, CancellationToken ct) where T : IPortalResponse { var url = endpoint.BuildAbsoluteUrl(RootUrl).Split('?').FirstOrDefault(); var token = await CheckGenerateToken(ct); if (ct.IsCancellationRequested) { return(default(T)); } // these should have already been added if (!parameters.ContainsKey("f")) { parameters.Add("f", "json"); } if (!parameters.ContainsKey("token") && token != null && !String.IsNullOrWhiteSpace(token.Value)) { parameters.Add("token", token.Value); _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(token.Value); if (token.AlwaysUseSsl) { url = url.Replace("http:", "https:"); } } HttpContent content = null; try { content = new FormUrlEncodedContent(parameters); } catch (FormatException) { var tempContent = new MultipartFormDataContent(); foreach (var keyValuePair in parameters) { tempContent.Add(new StringContent(keyValuePair.Value), keyValuePair.Key); } content = tempContent; } _httpClient.CancelPendingRequests(); Uri uri; bool validUrl = Uri.TryCreate(url, UriKind.Absolute, out uri); if (!validUrl) { throw new HttpRequestException(String.Format("Not a valid url: {0}", url)); } System.Diagnostics.Debug.WriteLine(uri); String resultString = String.Empty; try { HttpResponseMessage response = await _httpClient.PostAsync(uri, content, ct); response.EnsureSuccessStatusCode(); resultString = await response.Content.ReadAsStringAsync(); } catch (TaskCanceledException cex) { System.Diagnostics.Debug.WriteLine(cex.ToString()); return(default(T)); } catch (HttpRequestException) { throw; } System.Diagnostics.Debug.WriteLine(resultString); var result = Serializer.AsPortalResponse <T>(resultString); if (result.Error != null) { throw new InvalidOperationException(result.Error.ToString()); } return(result); }
protected Task <T> Get <T>(IEndpoint endpoint, CancellationToken ct) where T : IPortalResponse { Guard.AgainstNullArgument("endpoint", endpoint); return(Get <T>(endpoint.BuildAbsoluteUrl(RootUrl), ct)); }
protected Task <T> Get <T>(IEndpoint endpoint, CancellationToken ct) where T : IPortalResponse { return(Get <T>(endpoint.BuildAbsoluteUrl(RootUrl), ct)); }
public String BuildAbsoluteUrl(String rootUrl) { return(Endpoint.BuildAbsoluteUrl(rootUrl)); }