/// <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, 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", SDK.ProductId); mpRequest.Request.Headers.Add("x-tracking-id", SDK.TrackingId); 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 (requestOptions.TrackHeaders != null) { foreach (var trackHeader in requestOptions.TrackHeaders) { if (mpRequest.Request.Headers[trackHeader.Key] == null && trackHeader.Value != null) { mpRequest.Request.Headers[trackHeader.Key] = trackHeader.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); mpRequest.Request.Proxy = proxy; return(mpRequest); }