Ejemplo n.º 1
0
 public NationBuilderRemoteException(HttpStatusCode httpStatusCode,
     RemoteException remoteException, NationBuilderWebRequest nationBuilderRequest, Exception innerException = null)
     : base(GetMessage(remoteException), innerException)
 {
     this.HttpStatusCode = httpStatusCode;
     this.ExceptionCode = null == remoteException.code ? remoteException.error : remoteException.code;
     this.RemoteException = remoteException;
     this.NationBuilderRequest = nationBuilderRequest;
 }
        protected void ReceiveVoidHttpResponse(NationBuilderWebRequest req, HttpStatusCode expectedStatusCode = HttpStatusCode.NoContent)
        {
            HttpWebResponse response = RequestGetResponse(req);

            if (response.StatusCode != expectedStatusCode)
            {
                throw new InvalidOperationException("Unexpected HTTP status code: " + response.StatusCode + "\n" + response.StatusDescription);
            }
        }
        protected NationBuilderWebRequest MakeHttpRequest(StringBuilder url, string method = "GET")
        {
            var res = new NationBuilderWebRequest(url.ToString());

            res.Proxy       = HttpProxy;
            res.ContentType = "application/json";
            res.Accept      = "application/json";
            res.Method      = method;

            return(res);
        }
        protected HttpWebResponse RequestGetResponse(NationBuilderWebRequest req)
        {
            HttpWebResponse response;

            try
            {
                response = (HttpWebResponse)req.GetResponse();
            }
            catch (WebException exc)
            {
                response = (HttpWebResponse)exc.Response;

                if (null == response)
                {
                    throw; // unrecognized exceptions back.
                }

                // Marshall Nation Builder exceptions back:
                switch (response.StatusCode)
                {
                case HttpStatusCode.BadRequest:
                case HttpStatusCode.NotFound:
                    RemoteException exceptionInformation;

                    try {
                        exceptionInformation = DeserializeNationBuilderObject <RemoteException>(response.GetResponseStream());
                    }
                    catch
                    {
                        Stream stream = response.GetResponseStream();

                        stream.Seek(0, SeekOrigin.Begin);
                        using (var reader = new StreamReader(stream, Encoding.UTF8)) {
                            exceptionInformation = new RemoteException()
                            {
                                message           = "Unknown error. See 'error_description' field for the unparsed server response.",
                                error_description = reader.ReadToEnd(),
                            };
                        }
                    }

                    throw new NationBuilderRemoteException(response.StatusCode, exceptionInformation, req, exc);
                }

                // Throw unrecognized exceptions back:
                throw;
            }

            return(response);
        }
        protected NationBuilderWebRequest MakeHttpPostRequest <PostDataT>(StringBuilder url, PostDataT postData, string method = "POST")
        {
            var res = new NationBuilderWebRequest(url.ToString());

            res.Proxy       = HttpProxy;
            res.ContentType = "application/json";
            res.Accept      = "application/json";
            res.Method      = method;

            byte[] postDataOctets = ((MemoryStream)SerializeNationBuilderObject <PostDataT>(postData)).ToArray();

            res.SendData(postDataOctets);

            return(res);
        }
        protected ResponseT DeserializeHttpResponse <ResponseT>
            (NationBuilderWebRequest req, int minimumStatusCode, int maximumStatusCode)
            where ResponseT : NationBuilderResponse
        {
            HttpWebResponse response      = RequestGetResponse(req);
            int             statusCodeInt = (int)response.StatusCode;

            if (statusCodeInt < minimumStatusCode || statusCodeInt > maximumStatusCode)
            {
                throw new InvalidOperationException("Unexpected HTTP status code: " + response.StatusCode + "\n" + response.StatusDescription);
            }

            var res = DeserializeNationBuilderObject <ResponseT>(response.GetResponseStream());

            res.Http = new HttpResponseInformation(response);

            return(res);
        }
 protected ResponseT DeserializeHttpResponse <ResponseT>(NationBuilderWebRequest req)
     where ResponseT : NationBuilderResponse
 {
     return(DeserializeHttpResponse <ResponseT>(req, 200, 299));
 }
 protected ResponseT DeserializeHttpResponse <ResponseT>
     (NationBuilderWebRequest req, HttpStatusCode expectedStatusCode)
     where ResponseT : NationBuilderResponse
 {
     return(DeserializeHttpResponse <ResponseT>(req, (int)expectedStatusCode, (int)expectedStatusCode));
 }