예제 #1
0
        public T Get <T>(string route, ServerConfiguration webApi = null)
        {
            if (MockValues.ContainsKey(route))
            {
                return((T)MockValues[route]);
            }

            var client = new HttpClient();
            HttpResponseMessage response;

            client.BaseAddress = webApi == null ? BaseAddress : new Uri(webApi.ToString());
            if (Token == null)
            {
                response = client.PostAsync("api/token/connectserver", Content).Result;
                if (response.IsSuccessStatusCode)
                {
                    Token = response.Content.ReadAsStringAsync().Result;
                }
                else
                {
                    throw new HttpRequestException(response.Headers.ToString());
                }
            }

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Token);
            response = client.GetAsync(route).Result;
            if (response.IsSuccessStatusCode)
            {
                return(JsonConvert.DeserializeObject <T>(response.Content.ReadAsStringAsync().Result));
            }

            throw new HttpRequestException(response.Headers.ToString());
        }
예제 #2
0
        public T Get <T>(string route, ServerConfiguration webApi = null, object id = null)
        {
            if (MockValues.ContainsKey(route))
            {
                return((T)MockValues[route]);
            }

            var client   = new HttpClient();
            var response = new HttpResponseMessage();

            client.BaseAddress = webApi == null ? BaseAddress : new Uri(webApi.ToString());
            AssignToken(response, ref client);
            response = client.GetAsync(route + "?id=" + id ?? "").Result;
            if (response.IsSuccessStatusCode)
            {
                return(JsonConvert.DeserializeObject <T>(response.Content.ReadAsStringAsync().Result));
            }

            throw new HttpRequestException(response.Headers.ToString());
        }
예제 #3
0
        public T Get <T>(WebApiRoute route, ServerConfiguration webApi, object id)
        {
            if (MockValues.ContainsKey(route))
            {
                return((T)MockValues[route]);
            }

            var client = new HttpClient {
                BaseAddress = webApi == null ? BaseAddress : new Uri(webApi.ToString())
            };

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Token);
            var response = client.GetAsync(WebApiRoutes[route] + "?id=" + id ?? "").Result;

            if (response.IsSuccessStatusCode)
            {
                return(JsonConvert.DeserializeObject <T>(response.Content.ReadAsStringAsync().Result));
            }

            throw new HttpRequestException(response.Headers.ToString());
        }
예제 #4
0
        public T Patch <T>(string route, object data, ServerConfiguration webApi = null)
        {
            if (MockValues.ContainsKey(route))
            {
                return((T)MockValues[route]);
            }

            var client   = new HttpClient();
            var response = new HttpResponseMessage();

            client.BaseAddress = webApi == null ? BaseAddress : new Uri(webApi.ToString());
            AssignToken(response, ref client);
            var content      = new StringContent(JsonConvert.SerializeObject(data), Encoding.Default, "application/json");
            var postResponse = client.PatchAsync(route, content).Result;

            if (postResponse.IsSuccessStatusCode)
            {
                return(JsonConvert.DeserializeObject <T>(postResponse.Content.ReadAsStringAsync().Result));
            }

            throw new HttpRequestException(postResponse.Headers.ToString());
        }
예제 #5
0
        public T Patch <T>(WebApiRoute route, object id, object data, ServerConfiguration webApi)
        {
            if (MockValues.ContainsKey(route))
            {
                return((T)MockValues[route]);
            }

            var client = new HttpClient
            {
                BaseAddress = webApi == null ? BaseAddress : new Uri(webApi.ToString())
            };

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Token);
            var content      = new StringContent(JsonConvert.SerializeObject(data), Encoding.Default, "application/json");
            var postResponse = client.PatchAsync(WebApiRoutes[route] + "?id=" + id ?? "", content).Result;

            if (postResponse.IsSuccessStatusCode)
            {
                return(JsonConvert.DeserializeObject <T>(postResponse.Content.ReadAsStringAsync().Result));
            }

            throw new HttpRequestException(postResponse.Headers.ToString());
        }