Example #1
0
        /// <summary>
        /// Makes a PATCH request to API
        /// </summary>
        /// <param name="endPoint">The api endPoint</param>
        /// <param name="stringBody">The request payload</param>
        /// <returns>The response</returns>
        public string patch(string endPoint, IPaymentRailsMappable body)
        {
            body.IsMappable();
            HttpContent jsonBody = convertBody(body.ToJson());
            string      result   = "";

            try
            {
                httpClient = createRequest(endPoint, "PATCH", body);

                var request = new HttpRequestMessage(new HttpMethod("PATCH"), endPoint)
                {
                    Content = jsonBody
                };
                System.Threading.Tasks.Task <HttpResponseMessage> responseTask = httpClient.SendAsync(request);

                HttpResponseMessage response = responseTask.Result;
                result = response.Content.ReadAsStringAsync().Result;
                if ((int)response.StatusCode != 200)
                {
                    throwStatusCodeException((int)response.StatusCode, response.Content.ReadAsStringAsync().Result);
                }
            }
            catch (System.Net.Http.HttpRequestException)
            {
                throw new InvalidStatusCodeException(result);
            }
            return(result);
        }
Example #2
0
        private HttpClient createRequest(string endPoint, string method, IPaymentRailsMappable body = null)
        {
            try
            {
                httpClient             = new HttpClient();
                httpClient.BaseAddress = new Uri(this.config.ApiBase);

                TimeSpan epochTicks = new TimeSpan(new DateTime(1970, 1, 1).Ticks);
                TimeSpan unixTicks  = new TimeSpan(DateTime.UtcNow.Ticks) - epochTicks;
                int      unixTime   = (int)unixTicks.TotalSeconds;

                var authorization = generateAuthorization(unixTime + "", endPoint, method, body);

                httpClient.DefaultRequestHeaders.Add("Authorization", authorization);
                httpClient.DefaultRequestHeaders.Add("X-PR-Timestamp", unixTime + "");
                return(httpClient);
            }
            catch (System.Net.Http.HttpRequestException e)
            {
                throw new InvalidStatusCodeException(e.Message);
            }
        }
Example #3
0
        /// <summary>
        /// Makes a POST request to API
        /// </summary>
        /// <param name="endPoint">The api endPoint</param>
        /// <param name="stringBody">The request payload</param>
        /// <returns>The Response</returns>
        public string post(string endPoint, IPaymentRailsMappable body)
        {
            body.IsMappable();
            HttpContent jsonBody = convertBody(body.ToJson());
            string      result   = "";

            try
            {
                httpClient = createRequest(endPoint, "POST", body);
                HttpResponseMessage response = httpClient.PostAsync(endPoint, jsonBody).Result;
                result = response.Content.ReadAsStringAsync().Result;
                if ((int)response.StatusCode != 200)
                {
                    throwStatusCodeException((int)response.StatusCode, response.Content.ReadAsStringAsync().Result);
                }
            }
            catch (System.Net.Http.HttpRequestException)
            {
                throw new InvalidStatusCodeException(result);
            }

            return(result);
        }
Example #4
0
        private string generateAuthorization(string timeStamp, string endPoint, string method, IPaymentRailsMappable body)
        {
            string newBody = "";

            if (body != null)
            {
                newBody = body.ToJson();
            }

            string message = timeStamp + "\n" + method + "\n" + endPoint + "\n" + newBody + "\n";

            if (this.config.ApiSecret == null || this.config.ApiSecret == "")
            {
                throw new InvalidCredentialsException("API Secret must be provided.");
            }
            if (this.config.ApiKey == null || this.config.ApiKey == "")
            {
                throw new InvalidCredentialsException("API Key must be provided.");
            }
            var signature = GetHash(message, this.config.ApiSecret);

            return("prsign " + this.config.ApiKey + ":" + signature);
        }