示例#1
0
        protected Request CreateRequest(APIFormat apiFormat)
        {
            Request request = new Request(config, apiFormat);

            request.QueryParams.Add("sellerID", config.SellerID);
            return(request);
        }
    public static string Generate(LogChannelsConfig.Logger outputAPI, string nameSpace, string conditionals, string channel, LogChannelsConfig.Severity severity)
    {
        APIFormat       apiFormat          = apiToFormat[outputAPI];
        SeverityStrings severityStrings    = apiFormat.SeverityToStrings[severity];
        string          methodCallTemplate = apiFormat.MethodCallTemplate;

        string formattedTemplate = string.Format(methodCallTemplate, nameSpace, channel, conditionals, severityStrings.CallMethod, severityStrings.LogMethod);

        return(formattedTemplate);
    }
示例#3
0
        /// <summary>
        /// Create an API URL combining the function name, base and format.
        /// </summary>
        /// <param name="apiFunction">The API function name</param>
        /// <param name="format">The API format requested (JSON or XML)</param>
        /// <param name="queryString">Optional query string</param>
        /// <returns>A string containing the requested URL</returns>
        private static string MakeURL(string apiFunction, APIFormat format, string queryString)
        {
            StringBuilder url = new StringBuilder();

            url.AppendFormat("{0}{1}.{2}", APIBase, apiFunction, APIFormatToString(format));
            if (queryString != null)
            {
                url.AppendFormat("?{0}", queryString);
            }
            return(url.ToString());
        }
        public static ISerializer GetSerializer(APIFormat format)
        {
            switch (format)
            {
            case APIFormat.JSON:
                return(jsonSerializer);

            default:
            case APIFormat.XML:
                return(xmlSerializer);
            }
        }
 public Request(APIConfig cfg, APIFormat?apiFormat = null)
 {
     config = cfg;
     if (apiFormat.HasValue)
     {
         _APIFormat = apiFormat.Value;
     }
     else
     {
         _APIFormat = cfg.APIFormat;
     }
     HttpRequest = new HttpRequestMessage();
 }
示例#6
0
 public System.Exception CreateApiException(APIFormat format, string content, IResponse response)
 {
     try
     {
         var errors = SerializerFactory.GetSerializer(format).Deserialize <Errors>(content);
         return(ApiException.Factory(errors, response));
     }
     catch (System.Exception ex)
     {
         var exceptionList = new System.Exception[] { ex };
         var aggrEx        = new AggregateException("Unable to parse error response >" + content + "<", exceptionList);
         throw aggrEx;
     }
 }
示例#7
0
 /// <summary>
 /// Construct an HttpWebRequest object using the GET method to call the specified
 /// CIXAPI function. The function name should be specified as documented without
 /// the format prefix. Thus "user.getprofile" rather than "user.getprofile.xml".
 /// </summary>
 /// <param name="apiFunction">The API function name</param>
 /// <param name="username">CIX username</param>
 /// <param name="password">CIX password</param>
 /// <param name="format">The API format requested (JSON or XML)</param>
 /// <returns>A constructed HttpWebRequest</returns>
 internal static HttpWebRequest GetWithCredentials(string apiFunction, string username, string password, APIFormat format)
 {
     return Create(apiFunction, username, password, format, APIMethod.GET, null, null);
 }
示例#8
0
 /// <summary>
 /// Construct an HttpWebRequest object using the GET method to call the specified
 /// CIXAPI function. The function name should be specified as documented without
 /// the format prefix. Thus "user.getprofile" rather than "user.getprofile.xml".
 /// </summary>
 /// <param name="apiFunction">The API function name</param>
 /// <param name="format">The API format requested (JSON or XML)</param>
 /// <returns>A constructed HttpWebRequest</returns>
 internal static HttpWebRequest Get(string apiFunction, APIFormat format)
 {
     return Create(apiFunction, CIX.Username, CIX.Password, format, APIMethod.GET, null, null);
 }
 public ExceptionInfo(APIFormat format)
 {
     Format = format;
 }
示例#10
0
 /// <summary>
 /// Create an API URL combining the function name, base and format.
 /// </summary>
 /// <param name="apiFunction">The API function name</param>
 /// <param name="format">The API format requested (JSON or XML)</param>
 /// <param name="queryString">Optional query string</param>
 /// <returns>A string containing the requested URL</returns>
 private static string MakeURL(string apiFunction, APIFormat format, string queryString)
 {
     StringBuilder url = new StringBuilder();
     url.AppendFormat("{0}{1}.{2}", APIBase, apiFunction, APIFormatToString(format));
     if (queryString != null)
     {
         url.AppendFormat("?{0}", queryString);
     }
     return url.ToString();
 }
示例#11
0
 /// <summary>
 /// Converts the specified APIFormat to a string.
 /// </summary>
 /// <param name="format">The API format requested (JSON or XML)</param>
 /// <returns>The string representing the API format</returns>
 private static string APIFormatToString(APIFormat format)
 {
     return format == APIFormat.JSON ? "json" : "xml";
 }
示例#12
0
        /// <summary>
        /// Construct an HttpWebRequest object using the specified CIXAPI function, format
        /// and method. Any authentication rider is attached to the header as required and
        /// the appropriate content type set.
        /// </summary>
        /// <param name="apiFunction">The API function name</param>
        /// <param name="username">Authentication username</param>
        /// <param name="password">Authentication password</param>
        /// <param name="format">The API format requested (JSON or XML)</param>
        /// <param name="method">The API method required (GET or POST)</param>
        /// <param name="postObject">For POST, this is the object to be posted</param>
        /// <param name="queryString">Optional query string for the URL</param>
        /// <returns>A constructed HttpWebRequest</returns>
        private static HttpWebRequest Create(string apiFunction, string username, string password, APIFormat format, APIMethod method, object postObject, string queryString)
        {
            HttpWebRequest wrGeturl;

            byte[] postMessageBytes;

            if (username == null || password == null)
            {
                return(null);
            }

            if (method == APIMethod.POST)
            {
                var o = postObject as Image;
                if (o != null)
                {
                    Image postImage = o;

                    ImageConverter converter = new ImageConverter();
                    postMessageBytes = (byte[])converter.ConvertTo(postImage, typeof(byte[]));

                    if (postMessageBytes == null)
                    {
                        return(null);
                    }

                    wrGeturl        = (HttpWebRequest)WebRequest.Create(MakeURL(apiFunction, format, queryString));
                    wrGeturl.Method = APIMethodToString(method);

                    if (ImageFormat.Jpeg.Equals(postImage.RawFormat))
                    {
                        wrGeturl.ContentType = "image/jpeg";
                    }
                    if (ImageFormat.Gif.Equals(postImage.RawFormat))
                    {
                        wrGeturl.ContentType = "image/gif";
                    }
                    if (ImageFormat.Png.Equals(postImage.RawFormat))
                    {
                        wrGeturl.ContentType = "image/png";
                    }
                    if (ImageFormat.Bmp.Equals(postImage.RawFormat))
                    {
                        wrGeturl.ContentType = "image/bitmap";
                    }
                    wrGeturl.ContentLength = postMessageBytes.Length;
                }
                else
                {
                    var s = postObject as string;
                    if (s != null)
                    {
                        string postString = s;

                        ASCIIEncoding encoder = new ASCIIEncoding();
                        postMessageBytes = encoder.GetBytes(postString);

                        wrGeturl        = (HttpWebRequest)WebRequest.Create(MakeURL(apiFunction, format, queryString));
                        wrGeturl.Method = APIMethodToString(method);

                        wrGeturl.ContentLength = postMessageBytes.Length;
                        wrGeturl.ContentType   = "application/text";
                    }
                    else
                    {
                        StringBuilder postMessageXml = new StringBuilder();

                        using (XmlWriter writer = XmlWriter.Create(postMessageXml))
                        {
                            XmlSerializer serializer = new XmlSerializer(postObject.GetType());
                            serializer.Serialize(writer, postObject);
                        }

                        // Remove the header
                        postMessageXml.Replace("<?xml version=\"1.0\" encoding=\"utf-16\"?>", "");

                        // Messages are posted as 7-bit ASCII.
                        UTF8Encoding encoder = new UTF8Encoding();
                        postMessageBytes = encoder.GetBytes(postMessageXml.ToString());

                        wrGeturl        = (HttpWebRequest)WebRequest.Create(MakeURL(apiFunction, format, queryString));
                        wrGeturl.Method = APIMethodToString(method);

                        wrGeturl.ContentLength = encoder.GetByteCount(postMessageXml.ToString());
                        wrGeturl.ContentType   = "application/xml; charset=utf-8";
                        wrGeturl.Accept        = "application/xml; charset=utf-8";
                    }
                }
            }
            else
            {
                wrGeturl        = (HttpWebRequest)WebRequest.Create(MakeURL(apiFunction, format, queryString));
                wrGeturl.Method = APIMethodToString(method);

                postMessageBytes = null;

                if (format == APIFormat.XML)
                {
                    wrGeturl.ContentType = "application/xml; charset=utf-8";
                    wrGeturl.Accept      = "application/xml; charset=utf-8";
                }
                else
                {
                    wrGeturl.ContentType = "application/json";
                    wrGeturl.Accept      = "application/json";
                }
            }

            string authInfo = username + ":" + password;

            authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
            wrGeturl.Headers.Add("Authorization", "Basic " + authInfo);
            wrGeturl.PreAuthenticate = true;

            if (postMessageBytes != null)
            {
                Stream dataStream = wrGeturl.GetRequestStream();
                dataStream.Write(postMessageBytes, 0, postMessageBytes.Length);
                dataStream.Close();
            }

            return(wrGeturl);
        }
示例#13
0
 /// <summary>
 /// Construct an HttpWebRequest object using the POST method to call the specified
 /// CIXAPI function. The function name should be specified as documented without
 /// the format prefix. Thus "forum.postmessage" rather than "forum.postmessage.xml".
 /// </summary>
 /// <param name="apiFunction">The API function name</param>
 /// <param name="format">The API format requested (JSON or XML)</param>
 /// <param name="postObject">The object to be posted</param>
 /// <returns>A constructed HttpWebRequest</returns>
 public static HttpWebRequest Post(string apiFunction, APIFormat format, object postObject)
 {
     return(Create(apiFunction, Username, Password, format, APIMethod.POST, postObject, null));
 }
示例#14
0
 /// <summary>
 /// Construct an HttpWebRequest object using the GET method to call the specified
 /// CIXAPI function. The function name should be specified as documented without
 /// the format prefix. Thus "user.getprofile" rather than "user.getprofile.xml".
 /// </summary>
 /// <param name="apiFunction">The API function name</param>
 /// <param name="format">The API format requested (JSON or XML)</param>
 /// <param name="queryString">Query string for the URL</param>
 /// <returns>A constructed HttpWebRequest</returns>
 public static HttpWebRequest GetWithQuery(string apiFunction, APIFormat format, string queryString)
 {
     return(Create(apiFunction, Username, Password, format, APIMethod.GET, null, queryString));
 }
 public Response(HttpResponseMessage response, APIFormat apiFormat)
 {
     originalResponse = response;
     _apiFormat       = apiFormat;
 }
示例#16
0
        private Response BuildResponseObject(string content, System.Net.HttpStatusCode code, APIFormat apiFormat)
        {
            var httpResponse = new HttpResponseMessage()
            {
                Content    = new StringContent(content),
                StatusCode = code
            };

            return(new Response(httpResponse, apiFormat));
        }
示例#17
0
        private static HttpWebRequest Create(string apiFunction, string username, string password, APIFormat format, APIMethod method, object postObject, string queryString)
        {
            HttpWebRequest request;

            byte[] postMessageBytes;

            if (username == null || password == null)
            {
                return(null);
            }

            if (method == APIMethod.POST)
            {
                var o = postObject as Image;
                if (o != null)
                {
                    Image postImage = o;

                    ImageConverter converter = new ImageConverter();
                    postMessageBytes = (byte[])converter.ConvertTo(postImage, typeof(byte[]));

                    if (postMessageBytes == null)
                    {
                        return(null);
                    }

                    request        = (HttpWebRequest)WebRequest.Create(MakeURL(apiFunction, format, queryString));
                    request.Method = APIMethodToString(method);

                    if (ImageFormat.Jpeg.Equals(postImage.RawFormat))
                    {
                        request.ContentType = "image/jpeg";
                    }
                    if (ImageFormat.Gif.Equals(postImage.RawFormat))
                    {
                        request.ContentType = "image/gif";
                    }
                    if (ImageFormat.Png.Equals(postImage.RawFormat))
                    {
                        request.ContentType = "image/png";
                    }
                    if (ImageFormat.Bmp.Equals(postImage.RawFormat))
                    {
                        request.ContentType = "image/bitmap";
                    }
                    request.ContentLength = postMessageBytes.Length;
                }
                else
                {
                    var s = postObject as string;
                    if (s != null)
                    {
                        string postString = s;

                        ASCIIEncoding encoder = new ASCIIEncoding();
                        postMessageBytes = encoder.GetBytes(postString);

                        request        = (HttpWebRequest)WebRequest.Create(MakeURL(apiFunction, format, queryString));
                        request.Method = APIMethodToString(method);

                        request.ContentLength = postMessageBytes.Length;
                        request.ContentType   = "application/text";
                    }
                    else
                    {
                        string postMessageXml;

                        using (MemoryStream memoryStream = new MemoryStream())
                        {
                            using (StreamReader reader = new StreamReader(memoryStream))
                            {
                                DataContractSerializer serializer = new DataContractSerializer(postObject.GetType());
                                serializer.WriteObject(memoryStream, postObject);
                                memoryStream.Position = 0;
                                postMessageXml        = reader.ReadToEnd();
                            }
                        }

                        // Messages are posted as 7-bit ASCII.
                        UTF8Encoding encoder = new UTF8Encoding();
                        postMessageBytes = encoder.GetBytes(postMessageXml);

                        request        = (HttpWebRequest)WebRequest.Create(MakeURL(apiFunction, format, queryString));
                        request.Method = APIMethodToString(method);

                        request.ContentLength = encoder.GetByteCount(postMessageXml);
                        request.ContentType   = "application/xml; charset=utf-8";
                        request.Accept        = "application/xml; charset=utf-8";
                    }
                }
            }
            else
            {
                request        = (HttpWebRequest)WebRequest.Create(MakeURL(apiFunction, format, queryString));
                request.Method = APIMethodToString(method);

                postMessageBytes = null;

                if (format == APIFormat.XML)
                {
                    request.ContentType = "application/xml; charset=utf-8";
                    request.Accept      = "application/xml; charset=utf-8";
                }
                else
                {
                    request.ContentType = "application/json";
                    request.Accept      = "application/json";
                }
            }

            string authInfo = username + ":" + password;

            authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
            request.Headers.Add("Authorization", "Basic " + authInfo);
            request.PreAuthenticate = true;

            if (postMessageBytes != null)
            {
                Stream dataStream = request.GetRequestStream();
                dataStream.Write(postMessageBytes, 0, postMessageBytes.Length);
                dataStream.Close();
            }

            return(request);
        }
示例#18
0
 /// <summary>
 /// Construct an HttpWebRequest object using the GET method to call the specified
 /// CIXAPI function. The function name should be specified as documented without
 /// the format prefix. Thus "user.getprofile" rather than "user.getprofile.xml".
 /// </summary>
 /// <param name="apiFunction">The API function name</param>
 /// <param name="format">The API format requested (JSON or XML)</param>
 /// <param name="queryString">Query string for the URL</param>
 /// <returns>A constructed HttpWebRequest</returns>
 internal static HttpWebRequest GetWithQuery(string apiFunction, APIFormat format, string queryString)
 {
     return Create(apiFunction, CIX.Username, CIX.Password, format, APIMethod.GET, null, queryString);
 }
示例#19
0
 /// <summary>
 /// Construct an HttpWebRequest object using the POST method to call the specified
 /// CIXAPI function. The function name should be specified as documented without
 /// the format prefix. Thus "forum.postmessage" rather than "forum.postmessage.xml".
 /// </summary>
 /// <param name="apiFunction">The API function name</param>
 /// <param name="format">The API format requested (JSON or XML)</param>
 /// <param name="postObject">The object to be posted</param>
 /// <returns>A constructed HttpWebRequest</returns>
 internal static HttpWebRequest Post(string apiFunction, APIFormat format, object postObject)
 {
     return Create(apiFunction, CIX.Username, CIX.Password, format, APIMethod.POST, postObject, null);
 }
示例#20
0
 /// <summary>
 /// Converts the specified APIFormat to a string.
 /// </summary>
 /// <param name="format">The API format requested (JSON or XML)</param>
 /// <returns>The string representing the API format</returns>
 private static string APIFormatToString(APIFormat format)
 {
     return(format == APIFormat.JSON ? "json" : "xml");
 }
示例#21
0
        /// <summary>
        /// Construct an HttpWebRequest object using the specified CIXAPI function, format
        /// and method. Any authentication rider is attached to the header as required and
        /// the appropriate content type set.
        /// </summary>
        /// <param name="apiFunction">The API function name</param>
        /// <param name="username">Authentication username</param>
        /// <param name="password">Authentication password</param>
        /// <param name="format">The API format requested (JSON or XML)</param>
        /// <param name="method">The API method required (GET or POST)</param>
        /// <param name="postObject">For POST, this is the object to be posted</param>
        /// <param name="queryString">Optional query string for the URL</param>
        /// <returns>A constructed HttpWebRequest</returns>
        private static HttpWebRequest Create(string apiFunction, string username, string password, APIFormat format, APIMethod method, object postObject, string queryString)
        {
            HttpWebRequest request;
            byte[] postMessageBytes;

            if (username == null || password == null)
            {
                return null;
            }

            if (method == APIMethod.POST)
            {
                var o = postObject as Image;
                if (o != null)
                {
                    Image postImage = o;

                    ImageConverter converter = new ImageConverter();
                    postMessageBytes = (byte[])converter.ConvertTo(postImage, typeof(byte[]));

                    if (postMessageBytes == null)
                    {
                        return null;
                    }

                    request = (HttpWebRequest)WebRequest.Create(MakeURL(apiFunction, format, queryString));
                    request.Method = APIMethodToString(method);

                    if (ImageFormat.Jpeg.Equals(postImage.RawFormat))
                    {
                        request.ContentType = "image/jpeg";
                    }
                    if (ImageFormat.Gif.Equals(postImage.RawFormat))
                    {
                        request.ContentType = "image/gif";
                    }
                    if (ImageFormat.Png.Equals(postImage.RawFormat))
                    {
                        request.ContentType = "image/png";
                    }
                    if (ImageFormat.Bmp.Equals(postImage.RawFormat))
                    {
                        request.ContentType = "image/bitmap";
                    }
                    request.ContentLength = postMessageBytes.Length;
                }
                else
                {
                    var s = postObject as string;
                    if (s != null)
                    {
                        string postString = s;

                        ASCIIEncoding encoder = new ASCIIEncoding();
                        postMessageBytes = encoder.GetBytes(postString);

                        request = (HttpWebRequest)WebRequest.Create(MakeURL(apiFunction, format, queryString));
                        request.Method = APIMethodToString(method);

                        request.ContentLength = postMessageBytes.Length;
                        request.ContentType = "application/text";
                    }
                    else
                    {
                        string postMessageXml;

                        using (MemoryStream memoryStream = new MemoryStream())
                        {
                            using (StreamReader reader = new StreamReader(memoryStream))
                            {
                                DataContractSerializer serializer = new DataContractSerializer(postObject.GetType());
                                serializer.WriteObject(memoryStream, postObject);
                                memoryStream.Position = 0;
                                postMessageXml = reader.ReadToEnd();
                            }
                        }

                        // Messages are posted as 7-bit ASCII.
                        UTF8Encoding encoder = new UTF8Encoding();
                        postMessageBytes = encoder.GetBytes(postMessageXml);

                        request = (HttpWebRequest)WebRequest.Create(MakeURL(apiFunction, format, queryString));
                        request.Method = APIMethodToString(method);

                        request.ContentLength = encoder.GetByteCount(postMessageXml);
                        request.ContentType = "application/xml; charset=utf-8";
                        request.Accept = "application/xml; charset=utf-8";
                    }
                }
            }
            else
            {
                request = (HttpWebRequest)WebRequest.Create(MakeURL(apiFunction, format, queryString));
                request.Method = APIMethodToString(method);

                postMessageBytes = null;

                if (format == APIFormat.XML)
                {
                    request.ContentType = "application/xml; charset=utf-8";
                    request.Accept = "application/xml; charset=utf-8";
                }
                else
                {
                    request.ContentType = "application/json";
                    request.Accept = "application/json";
                }
            }

            string authInfo = username + ":" + password;
            authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
            request.Headers.Add("Authorization", "Basic " + authInfo);
            request.PreAuthenticate = true;

            if (postMessageBytes != null)
            {
                Stream dataStream = request.GetRequestStream();
                dataStream.Write(postMessageBytes, 0, postMessageBytes.Length);
                dataStream.Close();
            }

            return request;
        }
示例#22
0
 /// <summary>
 /// Construct an HttpWebRequest object using the GET method to call the specified
 /// CIXAPI function. The function name should be specified as documented without
 /// the format prefix. Thus "user.getprofile" rather than "user.getprofile.xml".
 /// </summary>
 /// <param name="apiFunction">The API function name</param>
 /// <param name="username">CIX username</param>
 /// <param name="password">CIX password</param>
 /// <param name="format">The API format requested (JSON or XML)</param>
 /// <returns>A constructed HttpWebRequest</returns>
 public static HttpWebRequest GetWithCredentials(string apiFunction, string username, string password, APIFormat format)
 {
     return(Create(apiFunction, username, password, format, APIMethod.GET, null, null));
 }
示例#23
0
 /// <summary>
 /// Construct an HttpWebRequest object using the GET method to call the specified
 /// CIXAPI function. The function name should be specified as documented without
 /// the format prefix. Thus "user.getprofile" rather than "user.getprofile.xml".
 /// </summary>
 /// <param name="apiFunction">The API function name</param>
 /// <param name="format">The API format requested (JSON or XML)</param>
 /// <returns>A constructed HttpWebRequest</returns>
 public static HttpWebRequest Get(string apiFunction, APIFormat format)
 {
     return(Create(apiFunction, Username, Password, format, APIMethod.GET, null, null));
 }
        private async Task <ExceptionInfo> GetInfromationFromResponseAsync(HttpResponseMessage response, APIFormat format)
        {
            ExceptionInfo ret = new ExceptionInfo(format);

            ret.RawContent = await response.Content.ReadAsStringAsync();

            return(ret);
        }