public Task <PlivoResponse <T> > SendRequest <T>(string method, string uri, Dictionary <string, object> data, Dictionary <string, string> filesToUpload = null) where T : new() { switch (method) { case "GET": break; case "POST": break; case "DELETE": break; default: throw new NotSupportedException( method + " not supported"); } Request = new PlivoRequest(method, uri, "", data); var jsonSettings = new JsonSerializerSettings { ContractResolver = new PascalCasePropertyNamesContractResolver(), NullValueHandling = NullValueHandling.Ignore }; // create Plivo response object along with the deserialized object PlivoResponse <T> plivoResponse = new PlivoResponse <T>( StatusCode, new List <string>(), Response, JsonConvert.DeserializeObject <T>(Response, jsonSettings), new PlivoRequest(method, uri, string.Empty, data)); return(Task.FromResult(plivoResponse)); }
/// <summary> /// Sends the request. /// </summary> /// <returns>The request.</returns> /// <param name="method">Method.</param> /// <param name="uri">URI.</param> /// <param name="data">Data.</param> /// <typeparam name="T">The 1st type parameter.</typeparam> public async Task <PlivoResponse <T> > SendRequest <T>(string method, string uri, Dictionary <string, object> data, Dictionary <string, string> filesToUpload = null) where T : new() { HttpResponseMessage response = null; HttpRequestMessage request = null; switch (method) { case "GET": request = new HttpRequestMessage(HttpMethod.Get, uri + AsQueryString(data)); request.Headers.Add("Accept", "application/json"); break; case "POST": request = new HttpRequestMessage(HttpMethod.Post, uri); if (filesToUpload == null) { request.Headers.Add("Accept", "application/json"); request.Content = new StringContent( JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json" ); } else { MultipartFormDataContent multipartContent = new MultipartFormDataContent(); foreach (var key in filesToUpload.Keys) { FileInfo fileInfo = new FileInfo(filesToUpload[key]); string fileName = fileInfo.Name; HttpContent fileContents = new ByteArrayContent(File.ReadAllBytes(fileInfo.FullName)); string fileHeader = null; switch (fileName.Split('.')[1].ToLower()) { case "jpg": fileHeader = "image/jpeg"; break; case "png": fileHeader = "image/png"; break; case "jpeg": fileHeader = "image/jpeg"; break; case "pdf": fileHeader = "application/pdf"; break; } fileContents.Headers.Add("Content-Type", fileHeader); multipartContent.Add(fileContents, "file", fileName); } foreach (var key in data.Keys) { HttpContent stringContent = new StringContent((string)data[key].ToString()); multipartContent.Add(stringContent, key); } request.Content = multipartContent; } break; case "DELETE": request = new HttpRequestMessage(HttpMethod.Delete, uri); request.Headers.Add("Accept", "application/json"); request.Content = new StringContent( JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json" ); break; default: throw new NotSupportedException( method + " not supported"); } response = await _client.SendAsync(request).ConfigureAwait(false); var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false); // create Plivo response object along with the deserialized object PlivoResponse <T> plivoResponse; try { plivoResponse = new PlivoResponse <T>( (uint)response.StatusCode.GetHashCode(), response.Headers.Select(item => item.Key + "=" + item.Value).ToList(), responseContent, responseContent != string.Empty ? JsonConvert.DeserializeObject <T>(responseContent, _jsonSettings) : new T(), new PlivoRequest(method, uri, request.Headers.ToString(), data, filesToUpload)); } catch (Newtonsoft.Json.JsonReaderException) { plivoResponse = new PlivoResponse <T>( (uint)response.StatusCode.GetHashCode(), response.Headers.Select(item => item.Key + "=" + item.Value).ToList(), responseContent, responseContent != string.Empty ? JsonConvert.DeserializeObject <T>(responseContent) : new T(), new PlivoRequest(method, uri, request.Headers.ToString(), data, filesToUpload)); } return(plivoResponse); }
/// <summary> /// Sends the request. /// </summary> /// <returns>The request.</returns> /// <param name="method">Method.</param> /// <param name="uri">URI.</param> /// <param name="data">Data.</param> /// <typeparam name="T">The 1st type parameter.</typeparam> public async Task <PlivoResponse <T> > SendRequest <T>(string method, string uri, Dictionary <string, object> data, Dictionary <string, string> filesToUpload = null) where T : new() { HttpResponseMessage response = null; HttpRequestMessage request = null; bool isCallInsightsRequest = false; bool isVoiceRequest = false; bool isLookupRequest = false; if (data.ContainsKey("is_call_insights_request")) { isCallInsightsRequest = true; data.Remove("is_call_insights_request"); } else if (data.ContainsKey("is_voice_request")) { isVoiceRequest = true; data.Remove("is_voice_request"); } else if (data.ContainsKey("is_lookup_request")) { isLookupRequest = true; data.Remove("is_lookup_request"); } request = NewRequestFunc(method, uri, data, filesToUpload); if (isCallInsightsRequest) { response = await _callInsightsclient.SendAsync(request).ConfigureAwait(false); } else if (isVoiceRequest) { response = await _voiceBaseUriClient.SendAsync(request).ConfigureAwait(false); if ((int)response.StatusCode >= 500) { request = NewRequestFunc(method, uri, data, filesToUpload); response = await _voiceFallback1Client.SendAsync(request).ConfigureAwait(false); if ((int)response.StatusCode >= 500) { request = NewRequestFunc(method, uri, data, filesToUpload); response = await _voiceFallback2Client.SendAsync(request).ConfigureAwait(false); } } } else if (isLookupRequest) { response = await _lookupClient.SendAsync(request).ConfigureAwait(false); } else { response = await _client.SendAsync(request).ConfigureAwait(false); } if (response.StatusCode.ToString() == "Unauthorized") { throw new PlivoAuthenticationException("Unauthorized Request. Please check credentials"); } var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false); // create Plivo response object along with the deserialized object PlivoResponse <T> plivoResponse; try { plivoResponse = new PlivoResponse <T>( (uint)response.StatusCode.GetHashCode(), response.Headers.Select(item => item.Key + "=" + item.Value).ToList(), responseContent, responseContent != string.Empty ? JsonConvert.DeserializeObject <T>(responseContent, _jsonSettings) : new T(), new PlivoRequest(method, uri, request.Headers.ToString(), data, filesToUpload)); } catch (Newtonsoft.Json.JsonReaderException) { plivoResponse = new PlivoResponse <T>( (uint)response.StatusCode.GetHashCode(), response.Headers.Select(item => item.Key + "=" + item.Value).ToList(), responseContent, responseContent != string.Empty ? JsonConvert.DeserializeObject <T>(responseContent) : new T(), new PlivoRequest(method, uri, request.Headers.ToString(), data, filesToUpload)); } return(plivoResponse); }