public JToken ExecuteGenericRequest( HttpMethod httpMethod, string path, PayloadType payloadType, JObject payload, SDK sdk) { if (sdk.GetAccessToken() != null) { path = sdk.BaseUrl + path + "?access_token=" + sdk.GetAccessToken(); } MPRequest request = this.CreateRequest(httpMethod, path, payloadType, payload, (WebHeaderCollection)null, 0, 0); if (((IEnumerable <HttpMethod>) new HttpMethod[2] { HttpMethod.POST, HttpMethod.PUT }).Contains <HttpMethod>(httpMethod)) { Stream requestStream = request.Request.GetRequestStream(); requestStream.Write(request.RequestPayload, 0, request.RequestPayload.Length); requestStream.Close(); } try { using (HttpWebResponse response = (HttpWebResponse)request.Request.GetResponse()) return(JToken.Parse(new StreamReader(response.GetResponseStream(), Encoding.UTF8).ReadToEnd())); } catch (WebException ex) { return(JToken.Parse(new StreamReader((ex.Response as HttpWebResponse).GetResponseStream(), Encoding.UTF8).ReadToEnd())); } }
/// <summary> /// Execute a request to an api endpoint. /// </summary> /// <returns>Api response with the result of the call.</returns> public MPAPIResponse ExecuteRequest( HttpMethod httpMethod, string path, PayloadType payloadType, JObject payload, MPRequestOptions requestOptions) { if (requestOptions == null) { requestOptions = new MPRequestOptions(); } MPRequest mpRequest = CreateRequest(httpMethod, path, payloadType, payload, requestOptions); string result = string.Empty; int retriesLeft = requestOptions.Retries; if (new HttpMethod[] { HttpMethod.POST, HttpMethod.PUT }.Contains(httpMethod)) { using (Stream requestStream = mpRequest.Request.GetRequestStream()) { requestStream.Write(mpRequest.RequestPayload, 0, mpRequest.RequestPayload.Length); } } try { return(ExecuteRequest(mpRequest.Request, response => new MPAPIResponse(httpMethod, mpRequest.Request, payload, response), requestOptions.Retries)); } catch (Exception ex) { throw new MPRESTException(ex.Message); } }
/// <summary> /// Core module implementation. Execute a request to an endpoint. /// </summary> /// <returns>Api response with the result of the call.</returns> public MPAPIResponse ExecuteRequestCore( HttpMethod httpMethod, string path, PayloadType payloadType, JObject payload, WebHeaderCollection colHeaders, int connectionTimeout, int retries) { try { MPRequest mpRequest = CreateRequest(httpMethod, path, payloadType, payload, colHeaders, connectionTimeout, retries); string result = string.Empty; if (new HttpMethod[] { HttpMethod.POST, HttpMethod.PUT }.Contains(httpMethod)) { Stream requestStream = mpRequest.Request.GetRequestStream(); requestStream.Write(mpRequest.RequestPayload, 0, mpRequest.RequestPayload.Length); requestStream.Close(); } try { using (HttpWebResponse response = (HttpWebResponse)mpRequest.Request.GetResponse()) { return(new MPAPIResponse(httpMethod, mpRequest.Request, payload, response)); } } catch (WebException ex) { if (ex.Status == WebExceptionStatus.ProtocolError) { HttpWebResponse errorResponse = ex.Response as HttpWebResponse; return(new MPAPIResponse(httpMethod, mpRequest.Request, payload, errorResponse)); } else { if (--retries == 0) { throw; } return(ExecuteRequestCore(httpMethod, path, payloadType, payload, colHeaders, connectionTimeout, retries)); } } } catch (Exception ex) { throw new MPRESTException(ex.Message); } }
public JToken ExecuteGenericRequest( HttpMethod httpMethod, string path, PayloadType payloadType, JObject payload) { WebHeaderCollection header = new WebHeaderCollection(); if (SDK.GetAccessToken() != null) { if (!path.Contains("/oauth/token")) { header.Add("Authorization", String.Format("Bearer {0}", SDK.GetAccessToken())); } path = SDK.BaseUrl + path; } MPRequest mpRequest = CreateRequest(httpMethod, path, payloadType, payload, header, 0, 0); if (new HttpMethod[] { HttpMethod.POST, HttpMethod.PUT }.Contains(httpMethod)) { Stream requestStream = mpRequest.Request.GetRequestStream(); requestStream.Write(mpRequest.RequestPayload, 0, mpRequest.RequestPayload.Length); requestStream.Close(); } try { using (HttpWebResponse response = (HttpWebResponse)mpRequest.Request.GetResponse()) { Stream dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream, Encoding.UTF8); String StringResponse = reader.ReadToEnd(); return(JToken.Parse(StringResponse)); } } catch (WebException ex) { HttpWebResponse errorResponse = ex.Response as HttpWebResponse; Stream dataStream = errorResponse.GetResponseStream(); StreamReader reader = new StreamReader(dataStream, Encoding.UTF8); String StringResponse = reader.ReadToEnd(); return(JToken.Parse(StringResponse)); } }
public JToken ExecuteGenericRequest( HttpMethod httpMethod, string path, PayloadType payloadType, JObject payload) { if (SDK.GetAccessToken() != null) { path = SDK.BaseUrl + path + "?access_token=" + SDK.GetAccessToken(); } MPRequest mpRequest = CreateRequest(httpMethod, path, payloadType, payload, null, 0, 0); if (new HttpMethod[] { HttpMethod.POST, HttpMethod.PUT }.Contains(httpMethod)) { Stream requestStream = mpRequest.Request.GetRequestStream(); requestStream.Write(mpRequest.RequestPayload, 0, mpRequest.RequestPayload.Length); requestStream.Close(); } try { using (HttpWebResponse response = (HttpWebResponse)mpRequest.Request.GetResponse()) { Stream dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream, Encoding.UTF8); String StringResponse = reader.ReadToEnd(); return JToken.Parse(StringResponse); } } catch (WebException ex) { HttpWebResponse errorResponse = ex.Response as HttpWebResponse; Stream dataStream = errorResponse.GetResponseStream(); StreamReader reader = new StreamReader(dataStream, Encoding.UTF8); String StringResponse = reader.ReadToEnd(); return JToken.Parse(StringResponse); } }
public MPAPIResponse ExecuteRequestCore( HttpMethod httpMethod, string path, PayloadType payloadType, JObject payload, WebHeaderCollection colHeaders, int connectionTimeout, int retries) { MPRequest request = this.CreateRequest(httpMethod, path, payloadType, payload, colHeaders, connectionTimeout, retries); string empty = string.Empty; if (((IEnumerable <HttpMethod>) new HttpMethod[2] { HttpMethod.POST, HttpMethod.PUT }).Contains <HttpMethod>(httpMethod)) { Stream requestStream = request.Request.GetRequestStream(); requestStream.Write(request.RequestPayload, 0, request.RequestPayload.Length); requestStream.Close(); } try { using (HttpWebResponse response = (HttpWebResponse)request.Request.GetResponse()) return(new MPAPIResponse(httpMethod, request.Request, payload, response)); } catch (WebException ex) { if (ex.Status == WebExceptionStatus.ProtocolError) { HttpWebResponse response = ex.Response as HttpWebResponse; return(new MPAPIResponse(httpMethod, request.Request, payload, response)); } if (--retries != 0) { return(this.ExecuteRequestCore(httpMethod, path, payloadType, payload, colHeaders, connectionTimeout, retries)); } throw; } }
/// <summary> /// Execute a request to an api endpoint. /// </summary> /// <returns>Api response with the result of the call.</returns> public MPAPIResponse ExecuteRequest( HttpMethod httpMethod, string path, PayloadType payloadType, JObject payload, MPRequestOptions requestOptions) { DateTime start = DateTime.Now; if (requestOptions == null) { requestOptions = new MPRequestOptions(); } MPRequest mpRequest = CreateRequest(httpMethod, path, payloadType, payload, requestOptions); if (new HttpMethod[] { HttpMethod.POST, HttpMethod.PUT }.Contains(httpMethod)) { using (Stream requestStream = mpRequest.Request.GetRequestStream()) { requestStream.Write(mpRequest.RequestPayload, 0, mpRequest.RequestPayload.Length); } } try { Int32 retries; DateTime startRequest = DateTime.Now; var response = ExecuteRequest(mpRequest.Request, requestOptions.Retries, out retries); DateTime endRequest = DateTime.Now; // Send metrics SendMetrics(mpRequest.Request, response, retries, start, startRequest, endRequest); return(new MPAPIResponse(httpMethod, mpRequest.Request, payload, response)); } catch (Exception ex) { throw new MPRESTException(ex.Message); } }
/// <summary> /// Create a request to use in the call to a certain endpoint. /// </summary> /// <returns>Api response with the result of the call.</returns> public MPRequest CreateRequest(HttpMethod httpMethod, string path, PayloadType payloadType, JObject payload, WebHeaderCollection colHeaders, int connectionTimeout, int retries) { if (string.IsNullOrEmpty(path)) { throw new MPRESTException("Uri can not be an empty string."); } if (httpMethod.Equals(HttpMethod.GET)) { if (payload != null) { throw new MPRESTException("Payload not supported for this method."); } } else if (httpMethod.Equals(HttpMethod.POST)) { if (payload == null) { throw new MPRESTException("Must include payload for this method."); } } else if (httpMethod.Equals(HttpMethod.PUT)) { if (payload == null) { throw new MPRESTException("Must include payload for this method."); } } else if (httpMethod.Equals(HttpMethod.DELETE)) { if (payload != null) { throw new MPRESTException("Payload not supported for this method."); } } MPRequest mpRequest = new MPRequest(); mpRequest.Request = (HttpWebRequest)HttpWebRequest.Create(path); mpRequest.Request.Method = httpMethod.ToString(); if (connectionTimeout > 0) { mpRequest.Request.Timeout = connectionTimeout; } if (colHeaders != null) { foreach (var header in colHeaders) { mpRequest.Request.Headers.Add(header.ToString(), colHeaders[header.ToString()]); } } if (payload != null) // POST & PUT { byte[] data = null; if (payloadType != PayloadType.JSON) { var parametersDict = payload.ToObject <Dictionary <string, string> >(); StringBuilder parametersString = new StringBuilder(); parametersString.Append(string.Format("{0}={1}", parametersDict.First().Key, parametersDict.First().Value)); parametersDict.Remove(parametersDict.First().Key); foreach (var value in parametersDict) { parametersString.Append(string.Format("&{0}={1}", value.Key, value.Value)); } data = Encoding.ASCII.GetBytes(parametersString.ToString()); } else { data = Encoding.ASCII.GetBytes(payload.ToString()); } mpRequest.Request.ContentLength = data.Length; mpRequest.Request.ContentType = payloadType == PayloadType.JSON ? "application/json" : "application/x-www-form-urlencoded"; mpRequest.RequestPayload = data; } return(mpRequest); }
/// <summary> /// Create a request to use in the call to a certain endpoint. /// </summary> /// <returns>Api response with the result of the call.</returns> public MPRequest CreateRequest(HttpMethod httpMethod, string path, PayloadType payloadType, JObject payload, MPRequestOptions requestOptions) { if (string.IsNullOrEmpty(path)) { throw new MPRESTException("Uri can not be an empty string."); } if (httpMethod.Equals(HttpMethod.GET)) { if (payload != null) { throw new MPRESTException("Payload not supported for this method."); } } else if (httpMethod.Equals(HttpMethod.POST)) { //if (payload == null) //{ // throw new MPRESTException("Must include payload for this method."); //} } else if (httpMethod.Equals(HttpMethod.PUT)) { if (payload == null) { throw new MPRESTException("Must include payload for this method."); } } else if (httpMethod.Equals(HttpMethod.DELETE)) { if (payload != null) { throw new MPRESTException("Payload not supported for this method."); } } MPRequest mpRequest = new MPRequest(); mpRequest.Request = (HttpWebRequest)HttpWebRequest.Create(path); mpRequest.Request.Method = httpMethod.ToString(); if (requestOptions == null) { requestOptions = new MPRequestOptions(); } if (requestOptions.Timeout > 0) { mpRequest.Request.Timeout = requestOptions.Timeout; } //mpRequest.Request.Headers.Add("x-product-id", "BC32BHVTRPP001U8NHL0"); if (requestOptions.CustomHeaders != null) { foreach (var header in requestOptions.CustomHeaders) { if (mpRequest.Request.Headers[header.Key] == null) { mpRequest.Request.Headers.Add(header.Key, header.Value); } } } if (payload != null) // POST & PUT { byte[] data = null; if (payloadType != PayloadType.JSON) { var parametersDict = payload.ToObject <Dictionary <string, string> >(); StringBuilder parametersString = new StringBuilder(); parametersString.Append(string.Format("{0}={1}", parametersDict.First().Key, parametersDict.First().Value)); parametersDict.Remove(parametersDict.First().Key); foreach (var value in parametersDict) { parametersString.Append(string.Format("&{0}={1}", value.Key, value.Value.ToString())); } data = Encoding.ASCII.GetBytes(parametersString.ToString()); } else { data = Encoding.ASCII.GetBytes(payload.ToString()); } //mpRequest.Request.UserAgent = "MercadoPago DotNet SDK/" + SDK.Version; mpRequest.Request.ContentLength = data.Length; mpRequest.Request.ContentType = payloadType == PayloadType.JSON ? "application/json" : "application/x-www-form-urlencoded"; mpRequest.RequestPayload = data; } IWebProxy proxy = requestOptions.Proxy != null ? requestOptions.Proxy : (_proxy != null ? _proxy : SDK.Proxy); if (proxy != null) { mpRequest.Request.Proxy = proxy; } return(mpRequest); }
public MPRequest CreateRequest( HttpMethod httpMethod, string path, PayloadType payloadType, JObject payload, WebHeaderCollection colHeaders, int connectionTimeout, int retries) { if (string.IsNullOrEmpty(path)) { throw new MPRESTException("Uri can not be an empty string."); } if (httpMethod.Equals((object)HttpMethod.GET)) { if (payload != null) { throw new MPRESTException("Payload not supported for this method."); } } else if (!httpMethod.Equals((object)HttpMethod.POST)) { if (httpMethod.Equals((object)HttpMethod.PUT)) { if (payload == null) { throw new MPRESTException("Must include payload for this method."); } } else if (httpMethod.Equals((object)HttpMethod.DELETE) && payload != null) { throw new MPRESTException("Payload not supported for this method."); } } MPRequest mpRequest = new MPRequest(); mpRequest.Request = (HttpWebRequest)WebRequest.Create(path); mpRequest.Request.Method = httpMethod.ToString(); if (connectionTimeout > 0) { mpRequest.Request.Timeout = connectionTimeout; } if (colHeaders != null) { foreach (object colHeader in (NameObjectCollectionBase)colHeaders) { mpRequest.Request.Headers.Add(colHeader.ToString(), colHeaders[colHeader.ToString()]); } } mpRequest.Request.ContentType = "application/json"; mpRequest.Request.UserAgent = "MercadoPago DotNet SDK/1.0.30"; mpRequest.Request.Headers.Add("x-product-id", "BC32BHVTRPP001U8NHL0"); if (payload != null) { byte[] bytes; if (payloadType != PayloadType.JSON) { Dictionary <string, string> source = payload.ToObject <Dictionary <string, string> >(); StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append(string.Format("{0}={1}", (object)source.First <KeyValuePair <string, string> >().Key, (object)source.First <KeyValuePair <string, string> >().Value)); source.Remove(source.First <KeyValuePair <string, string> >().Key); foreach (KeyValuePair <string, string> keyValuePair in source) { stringBuilder.Append(string.Format("&{0}={1}", (object)keyValuePair.Key, (object)keyValuePair.Value.ToString())); } bytes = Encoding.ASCII.GetBytes(stringBuilder.ToString()); } else { bytes = Encoding.ASCII.GetBytes(payload.ToString()); } mpRequest.Request.ContentLength = (long)bytes.Length; mpRequest.Request.ContentType = payloadType == PayloadType.JSON ? "application/json" : "application/x-www-form-urlencoded"; mpRequest.RequestPayload = bytes; } return(mpRequest); }