internal RecurlyException(string message, Errors errors) : base(message) { Errors = errors.ValidationErrors; TransactionError = errors.TransactionError; }
/// <summary> /// Used for downloading PDFs /// </summary> /// <param name="urlPath"></param> /// <param name="acceptType"></param> /// <param name="acceptLanguage"></param> /// <returns></returns> public virtual byte[] PerformDownloadRequest(string urlPath, string acceptType, string acceptLanguage) { var url = Settings.GetServerUri(urlPath); var request = (HttpWebRequest)WebRequest.Create(url); request.Accept = acceptType; request.ContentType = "application/xml; charset=utf-8"; // The request is an XML document request.SendChunked = false; // Send it all as one request request.UserAgent = Settings.UserAgent; request.Headers.Add(HttpRequestHeader.Authorization, Settings.AuthorizationHeaderValue); request.Method = "GET"; request.Headers.Add("Accept-Language", acceptLanguage); Debug.WriteLine(String.Format("Recurly: Requesting {0} {1}", request.Method, request.RequestUri)); try { var r = (HttpWebResponse)request.GetResponse(); byte[] pdf; var buffer = new byte[2048]; if (!request.HaveResponse || r.StatusCode != HttpStatusCode.OK) { return(null); } using (var ms = new MemoryStream()) { using (var reader = new BinaryReader(r.GetResponseStream(), Encoding.Default)) { int bytesRead; while ((bytesRead = reader.Read(buffer, 0, 2048)) > 0) { ms.Write(buffer, 0, bytesRead); } } pdf = ms.ToArray(); } return(pdf); } catch (WebException ex) { if (ex.Response == null) { throw; } var response = (HttpWebResponse)ex.Response; var statusCode = response.StatusCode; Errors errors; Debug.WriteLine(String.Format("Recurly Library Received: {0} - {1}", (int)statusCode, statusCode)); switch (response.StatusCode) { case HttpStatusCode.OK: case HttpStatusCode.Accepted: case HttpStatusCode.Created: case HttpStatusCode.NoContent: return(null); case HttpStatusCode.NotFound: errors = Errors.ReadResponseAndParseErrors(response); if (errors.ValidationErrors.HasAny()) { throw new NotFoundException(errors.ValidationErrors[0].Message, errors); } throw new NotFoundException("The requested object was not found.", errors); case HttpStatusCode.Unauthorized: case HttpStatusCode.Forbidden: errors = Errors.ReadResponseAndParseErrors(response); throw new InvalidCredentialsException(errors); case HttpStatusCode.PreconditionFailed: errors = Errors.ReadResponseAndParseErrors(response); throw new ValidationException(errors); case HttpStatusCode.ServiceUnavailable: throw new TemporarilyUnavailableException(); case HttpStatusCode.InternalServerError: errors = Errors.ReadResponseAndParseErrors(response); throw new ServerException(errors); } if ((int)statusCode == ValidationException.HttpStatusCode) // Unprocessable Entity { errors = Errors.ReadResponseAndParseErrors(response); throw new ValidationException(errors); } throw; } }
internal RecurlyException(Errors errors) { Errors = errors.ValidationErrors; TransactionError = errors.TransactionError; }
protected virtual HttpStatusCode PerformRequest(HttpRequestMethod method, string urlPath, WriteXmlDelegate writeXmlDelegate, ReadXmlDelegate readXmlDelegate, ReadXmlListDelegate readXmlListDelegate, ReadResponseDelegate reseponseDelegate) { var url = Settings.GetServerUri(urlPath); #if (DEBUG) Console.WriteLine("Requesting " + method + " " + url); #endif var request = (HttpWebRequest)WebRequest.Create(url); if (!request.RequestUri.Host.EndsWith(Settings.ValidDomain)) { throw new RecurlyException("Domain " + request.RequestUri.Host + " is not a valid Recurly domain"); } request.Accept = "application/xml"; // Tells the server to return XML instead of HTML request.ContentType = "application/xml; charset=utf-8"; // The request is an XML document request.SendChunked = false; // Send it all as one request request.UserAgent = Settings.UserAgent; request.Headers.Add(HttpRequestHeader.Authorization, Settings.AuthorizationHeaderValue); request.Headers.Add("X-Api-Version", Settings.RecurlyApiVersion); request.Method = method.ToString().ToUpper(); Debug.WriteLine(String.Format("Recurly: Requesting {0} {1}", request.Method, request.RequestUri)); if ((method == HttpRequestMethod.Post || method == HttpRequestMethod.Put) && (writeXmlDelegate != null)) { // 60 second timeout -- some payment gateways (e.g. PayPal) can take a while to respond request.Timeout = 60000; // Write POST/PUT body using (var requestStream = request.GetRequestStream()) { WritePostParameters(requestStream, writeXmlDelegate); } } else { request.ContentLength = 0; } try { using (var response = (HttpWebResponse)request.GetResponse()) { ReadWebResponse(response, readXmlDelegate, readXmlListDelegate, reseponseDelegate); return(response.StatusCode); } } catch (WebException ex) { if (ex.Response == null) { throw; } var response = (HttpWebResponse)ex.Response; var statusCode = response.StatusCode; Errors errors; Debug.WriteLine(String.Format("Recurly Library Received: {0} - {1}", (int)statusCode, statusCode)); switch (response.StatusCode) { case HttpStatusCode.OK: case HttpStatusCode.Accepted: case HttpStatusCode.Created: case HttpStatusCode.NoContent: ReadWebResponse(response, readXmlDelegate, readXmlListDelegate, reseponseDelegate); return(HttpStatusCode.NoContent); case HttpStatusCode.NotFound: errors = Errors.ReadResponseAndParseErrors(response); if (errors.ValidationErrors.HasAny()) { throw new NotFoundException(errors.ValidationErrors[0].Message, errors); } throw new NotFoundException("The requested object was not found.", errors); case HttpStatusCode.Unauthorized: case HttpStatusCode.Forbidden: errors = Errors.ReadResponseAndParseErrors(response); throw new InvalidCredentialsException(errors); case HttpStatusCode.BadRequest: case HttpStatusCode.PreconditionFailed: errors = Errors.ReadResponseAndParseErrors(response); throw new ValidationException(errors); case HttpStatusCode.ServiceUnavailable: throw new TemporarilyUnavailableException(); case HttpStatusCode.InternalServerError: errors = Errors.ReadResponseAndParseErrors(response); throw new ServerException(errors); } if ((int)statusCode == ValidationException.HttpStatusCode) // Unprocessable Entity { errors = Errors.ReadResponseAndParseErrors(response); if (errors.ValidationErrors.HasAny()) { Debug.WriteLine(errors.ValidationErrors[0].ToString()); } else { Debug.WriteLine("Client Error: " + response.ToString()); } throw new ValidationException(errors); } throw; } }