Esempio n. 1
0
 internal static NexmoResponse DoDeleteRequest(Uri uri, Dictionary <string, string> parameters, Credentials creds = null) => DoRequest("DELETE", uri, parameters, creds);
Esempio n. 2
0
 internal static NexmoResponse DoPostRequest(Uri uri, Dictionary <string, string> parameters, Credentials creds = null) => DoRequest("POST", uri, parameters, creds);
Esempio n. 3
0
 internal static NexmoResponse DoRequestWithContent(Uri uri, object parameters, Credentials creds = null) => DoRequest("POST", uri, parameters, creds);
Esempio n. 4
0
        internal static NexmoResponse DoPostRequest(Uri uri, object parameters, Credentials creds = null)
        {
            var apiParams = GetParameters(parameters);

            return(DoPostRequest(uri, apiParams, creds));
        }
Esempio n. 5
0
 internal static NexmoResponse DoPostRequestWithContent(Uri uri, object parameters, Credentials creds = null)
 {
     return(DoRequestWithContent(uri, parameters, creds));
 }
Esempio n. 6
0
        public static NexmoResponse DoRequest(string method, Uri uri, object requestBody, Credentials creds = null)
        {
            var sb         = new StringBuilder();
            var parameters = new Dictionary <string, string>();

            sb = BuildQueryString(parameters, creds);

            var requestContent = JsonConvert.SerializeObject(requestBody);

            var req = new HttpRequestMessage
            {
                RequestUri = new Uri((uri.OriginalString + $"?{sb}").ToLower()),
                Method     = new HttpMethod(method),
                Content    = new StringContent(requestContent, Encoding.UTF8, "application/json"),
            };

            VersionedApiRequest.SetUserAgent(ref req, creds);

            using (LogProvider.OpenMappedContext("ApiRequest.DoRequest", uri.GetHashCode()))
            {
                Logger.Debug($"{method} {uri} {sb}");
                var sendTask = Configuration.Instance.Client.SendAsync(req);
                sendTask.Wait();

                if (!sendTask.Result.IsSuccessStatusCode)
                {
                    Logger.Error($"FAIL: {sendTask.Result.StatusCode}");

                    if (string.Compare(Configuration.Instance.Settings["appSettings:Nexmo.Api.EnsureSuccessStatusCode"],
                                       "true", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        sendTask.Result.EnsureSuccessStatusCode();
                    }

                    return(new NexmoResponse
                    {
                        Status = sendTask.Result.StatusCode
                    });
                }

                string jsonResult;
                var    readTask = sendTask.Result.Content.ReadAsStreamAsync();
                readTask.Wait();
                using (var sr = new StreamReader(readTask.Result))
                {
                    jsonResult = sr.ReadToEnd();
                }
                Logger.Debug(jsonResult);
                return(new NexmoResponse
                {
                    Status = sendTask.Result.StatusCode,
                    JsonResponse = jsonResult
                });
            }
        }
Esempio n. 7
0
        private static StringBuilder BuildQueryString(IDictionary <string, string> parameters, Credentials creds = null)
        {
            var apiKey         = (creds?.ApiKey ?? Configuration.Instance.Settings["appSettings:Nexmo.api_key"]).ToLower();
            var apiSecret      = creds?.ApiSecret ?? Configuration.Instance.Settings["appSettings:Nexmo.api_secret"];
            var securitySecret = creds?.SecuritySecret ?? Configuration.Instance.Settings["appSettings:Nexmo.security_secret"];

            var sb = new StringBuilder();
            Action <IDictionary <string, string>, StringBuilder> buildStringFromParams = (param, strings) =>
            {
                foreach (var kvp in param)
                {
                    strings.AppendFormat("{0}={1}&", WebUtility.UrlEncode(kvp.Key), WebUtility.UrlEncode(kvp.Value));
                }
            };

            parameters.Add("api_key", apiKey);
            if (string.IsNullOrEmpty(securitySecret))
            {
                // security secret not provided, do not sign
                parameters.Add("api_secret", apiSecret);
                buildStringFromParams(parameters, sb);
                return(sb);
            }
            // security secret provided, sort and sign request
            parameters.Add("timestamp", ((int)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds).ToString(CultureInfo.InvariantCulture));
            var sortedParams = new SortedDictionary <string, string>(parameters);

            buildStringFromParams(sortedParams, sb);
            var queryToSign = "&" + sb;

            queryToSign = queryToSign.Remove(queryToSign.Length - 1) + securitySecret.ToUpper();
            var hashgen = MD5.Create();
            var hash    = hashgen.ComputeHash(Encoding.UTF8.GetBytes(queryToSign));

            sb.AppendFormat("sig={0}", ByteArrayToHexHelper.ByteArrayToHex(hash).ToLower());
            return(sb);
        }
Esempio n. 8
0
        /// <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, creds);

            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 (LogProvider.OpenMappedContext("ApiRequest.DoRequest", uri.GetHashCode()))
            {
                Logger.Debug($"{method} {uri} {sb}");
                var sendTask = Configuration.Instance.Client.SendAsync(req);
                sendTask.Wait();

                if (!sendTask.Result.IsSuccessStatusCode)
                {
                    Logger.Error($"FAIL: {sendTask.Result.StatusCode}");

                    if (string.Compare(Configuration.Instance.Settings["appSettings:Nexmo.Api.EnsureSuccessStatusCode"],
                                       "true", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        sendTask.Result.EnsureSuccessStatusCode();
                    }

                    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();
                }
                Logger.Debug(json);
                return(new NexmoResponse
                {
                    Status = sendTask.Result.StatusCode,
                    JsonResponse = json
                });
            }
        }
Esempio n. 9
0
        internal static string DoRequest(Uri uri, object parameters, Credentials creds = null)
        {
            var sb = GetQueryStringBuilderFor(parameters, creds);

            return(DoRequest(new Uri(uri, "?" + sb), creds));
        }
Esempio n. 10
0
        /// <summary>
        /// Send a GET 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="uri">The URI to GET</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 string DoRequest(Uri uri, Dictionary <string, string> parameters, Credentials creds = null)
        {
            var sb = BuildQueryString(parameters, creds);

            return(DoRequest(new Uri(uri, "?" + sb), creds));
        }