myGengo.Client.request C# (CSharp) Method

request() public method

public request ( string url, string method, string format, IDictionary param ) : WebResponse
url string
method string
format string
param IDictionary
return System.Net.WebResponse
        public WebResponse request(string url, string method, string format, IDictionary param)
        {
            format = format.ToLower();
            string[] formats = { "xml", "json" };
            if (!formats.Contains(format))
            {
                throw new GengoException("Invalid response format: " + format + ", accepted formats are: json or xml.");
            }
            method = method.ToUpper();
            string[] methods = { "GET", "POST", "PUT", "DELETE" };
            if (!methods.Contains(method))
            {
                throw new GengoException("HTTP method: " + method + " not supported");
            }

            string data = Utils.buildQuery(param);

            switch (method)
            {
                case "GET":
                    url = url + "?" + data;
                    client = (HttpWebRequest)WebRequest.Create(url);
                    client.Method = method;
                    client.Proxy = _proxy;
                    switch (format)
                    {
                        case "xml":
                            client.Accept = "application/xml";
                            break;
                        case "json":
                            client.Accept = "application/json";
                            break;
                    }
                    break;
                case "PUT":
                    client = (HttpWebRequest)WebRequest.Create(url);
                    client.Method = method;
                    client.Proxy = _proxy;
                    switch (format)
                    {
                        case "xml":
                            client.Accept = "application/xml";
                            break;
                        case "json":
                            client.Accept = "application/json";
                            break;
                    }
                    client.ContentType = "text/plain";
                    using (StreamWriter writer = new StreamWriter(client.GetRequestStream())) writer.Write(data);
                    break;
                case "POST":
                    client = (HttpWebRequest)WebRequest.Create(url);
                    client.Method = method;
                    client.Proxy = _proxy;
                    switch (format)
                    {
                        case "xml":
                            client.Accept = "application/xml";
                            break;
                        case "json":
                            client.Accept = "application/json";
                            break;
                    }
                    client.Accept = "application/json";
                    client.ContentType = "application/x-www-form-urlencoded";
                    client.ContentLength = data.Length;
                    using (StreamWriter writer = new StreamWriter(client.GetRequestStream())) writer.Write(data);
                    break;
                case "DELETE":
                    url = url + "?" + data;
                    client = (HttpWebRequest)WebRequest.Create(url);
                    client.Method = method;
                    client.Proxy = _proxy;
                    switch (format)
                    {
                        case "xml":
                            client.Accept = "application/xml";
                            break;
                        case "json":
                            client.Accept = "application/json";
                            break;
                    }
                    break;
            }
            try
            {
                //client.Proxy = _proxy;
                client.UserAgent = "myGengo spGengo SharePoint plugin; Version 1.0; https://github.com/MiguelRa/spGengo;";

                return client.GetResponse();
            }
            catch (Exception e)
            {
                throw new GengoException(e.Message);
            }
        }