/// <summary> /// Send a request to the Nexmo API. /// Do not include credentials in the parameters object. If you need to override credentials, use the optional Credentials parameter. /// </summary> /// <param name="method">HTTP method (POST, PUT, DELETE, etc)</param> /// <param name="uri">The URI to communicate with</param> /// <param name="parameters">Parameters required by the endpoint (do not include credentials)</param> /// <param name="creds">(Optional) Overridden credentials for only this request</param> /// <returns></returns> public static NexmoResponse DoRequest(string method, Uri uri, Dictionary <string, string> parameters, Credentials creds = null) { var sb = new StringBuilder(); // if parameters is null, assume that key and secret have been taken care of if (null != parameters) { sb = BuildQueryString(parameters, creds); } var req = new HttpRequestMessage { RequestUri = uri, Method = new HttpMethod(method), }; VersionedApiRequest.SetUserAgent(ref req); var data = Encoding.ASCII.GetBytes(sb.ToString()); req.Content = new ByteArrayContent(data); req.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/x-www-form-urlencoded"); using (Configuration.Instance.ApiLogger.BeginScope("ApiRequest.DoRequest {0}", uri.GetHashCode())) { Configuration.Instance.ApiLogger.LogDebug($"{method} {uri} {sb}"); var sendTask = Configuration.Instance.Client.SendAsync(req); sendTask.Wait(); if (!sendTask.Result.IsSuccessStatusCode) { Configuration.Instance.ApiLogger.LogError($"FAIL: {sendTask.Result.StatusCode}"); throw NexmoApiExceptionFactory.CreateForResponse(sendTask.Result.StatusCode); // return new NexmoResponse // { // Status = sendTask.Result.StatusCode // }; } string json; var readTask = sendTask.Result.Content.ReadAsStreamAsync(); readTask.Wait(); using (var sr = new StreamReader(readTask.Result)) { json = sr.ReadToEnd(); } Configuration.Instance.ApiLogger.LogDebug(json); return(new NexmoResponse { Status = sendTask.Result.StatusCode, JsonResponse = json }); } }
/// <summary> /// Send a request to the versioned Nexmo API. /// Do not include credentials in the parameters object. If you need to override credentials, use the optional Credentials parameter. /// </summary> /// <param name="method">HTTP method (POST, PUT, DELETE, etc)</param> /// <param name="uri">The URI to communicate with</param> /// <param name="payload">Parameters required by the endpoint (do not include credentials)</param> /// <param name="creds">(Optional) Overridden credentials for only this request</param> /// <returns></returns> public static NexmoResponse DoRequest(string method, Uri uri, object payload, Credentials creds = null) { var appId = creds?.ApplicationId ?? Configuration.Instance.Settings["appSettings:Nexmo.Application.Id"]; var appKeyPath = creds?.ApplicationKey ?? Configuration.Instance.Settings["appSettings:Nexmo.Application.Key"]; var req = new HttpRequestMessage { RequestUri = uri, Method = new HttpMethod(method), }; SetUserAgent(ref req); // attempt bearer token auth req.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", Jwt.CreateToken(appId, appKeyPath)); var data = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(payload)); req.Content = new ByteArrayContent(data); req.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); using (Configuration.Instance.ApiLogger.BeginScope("ApiRequest.DoRequest {0}", uri.GetHashCode())) { Configuration.Instance.ApiLogger.LogDebug($"{method} {uri} {payload}"); var sendTask = Configuration.Instance.Client.SendAsync(req); sendTask.Wait(); if (!sendTask.Result.IsSuccessStatusCode) { Configuration.Instance.ApiLogger.LogError($"FAIL: {sendTask.Result.StatusCode}"); throw NexmoApiExceptionFactory.CreateForResponse(sendTask.Result.StatusCode); // return new NexmoResponse // { // Status = sendTask.Result.StatusCode // }; } string json; var readTask = sendTask.Result.Content.ReadAsStreamAsync(); readTask.Wait(); using (var sr = new StreamReader(readTask.Result)) { json = sr.ReadToEnd(); } Configuration.Instance.ApiLogger.LogDebug(json); return(new NexmoResponse { Status = sendTask.Result.StatusCode, JsonResponse = json }); } }