devicehive-.net /src/Server/DeviceHive.Test/JsonClient.cs

Language C# Lines 136
MD5 Hash 9df93fdbc6711121cab0a9e140e06325
Repository https://github.com/oryol/devicehive-.net.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.IO;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace DeviceHive.Test
{
    public class JsonClient
    {
        private string _baseUrl;

        public JsonClient(string baseUrl)
        {
            _baseUrl = baseUrl;
        }

        public JsonResponse Get(string url, Authorization auth = null)
        {
            return Run("GET", url, auth: auth);
        }

        public JsonResponse Post(string url, object request, Authorization auth = null)
        {
            return Run("POST", url, jsonRequest: JObject.FromObject(request), auth: auth);
        }

        public JsonResponse Put(string url, object request, Authorization auth = null)
        {
            return Run("PUT", url, jsonRequest: JObject.FromObject(request), auth: auth);
        }

        public JsonResponse Delete(string url, Authorization auth = null)
        {
            return Run("DELETE", url, auth: auth);
        }

        public JsonResponse Run(string method, string url, JObject jsonRequest = null, Authorization auth = null)
        {
            if (string.IsNullOrEmpty(method))
                throw new ArgumentException("Method is null or empty!", "method");
            if (string.IsNullOrEmpty(url))
                throw new ArgumentException("URL is null or empty!", "url");

            // prepare request
            var request = (HttpWebRequest)HttpWebRequest.Create(_baseUrl + url);
            request.Method = method;
            request.Accept = "application/json";
            if (auth != null)
            {
                switch (auth.Type)
                {
                    case "User":
                        request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(
                            Encoding.UTF8.GetBytes(string.Format("{0}:{1}", auth.Login, auth.Password)));
                        break;
                    case "Device":
                        request.Headers["Auth-DeviceID"] = auth.Login;
                        request.Headers["Auth-DeviceKey"] = auth.Password;
                        break;
                }
            }
            if (jsonRequest != null)
            {
                request.ContentType = "application/json";
                using (var stream = request.GetRequestStream())
                {
                    using (var writer = new StreamWriter(stream))
                    {
                        writer.Write(jsonRequest.ToString());
                    }
                }
            }

            // perform a call
            HttpWebResponse response;
            try
            {
                response = (HttpWebResponse)request.GetResponse();
            }
            catch (WebException ex)
            {
                response = (HttpWebResponse)ex.Response;
            }

            // parse response
            using (var stream = response.GetResponseStream())
            {
                using (var reader = new StreamReader(stream))
                {
                    var responseString = reader.ReadToEnd();
                    try
                    {
                        var json = string.IsNullOrEmpty(responseString) ? null : JToken.Parse(responseString);
                        return new JsonResponse((int)response.StatusCode, json);
                    }
                    catch (JsonReaderException ex)
                    {
                        throw new WebException(string.Format("Error while parsing server response! " +
                            "Status: {0}, Response: {1}", (int)response.StatusCode, responseString), ex);
                    }
                }
            }
        }
    }

    public class Authorization
    {
        public string Type { get; private set; }
        public string Login { get; private set; }
        public string Password { get; private set; }

        public Authorization(string type, string login, string password)
        {
            Type = type;
            Login = login;
            Password = password;
        }
    }

    public class JsonResponse
    {
        public int Status { get; private set; }
        public JToken Json { get; private set; }

        public JsonResponse(int status, JToken json)
        {
            Status = status;
            Json = json;
        }
    }
}
Back to Top