-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRestTemplate.cs
133 lines (115 loc) · 4.25 KB
/
RestTemplate.cs
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
using System;
using System.Collections.Generic;
using System.Net;
using Newtonsoft.Json;
using RestSharp;
using RestSharp.Authenticators;
namespace boss.client.win
{
public class RestTemplate
{
private readonly string serviceUrl;
private readonly RestClient rest;
private readonly RestJsonSerializer jsonSerializer = new RestJsonSerializer();
public RestTemplate(CookieContainer cookieContainer)
{
rest = new RestClient() { CookieContainer = cookieContainer };
}
public RestTemplate(string serviceUrl, CookieContainer cookieContainer)
{
this.serviceUrl = serviceUrl;
rest = new RestClient(serviceUrl) { CookieContainer = cookieContainer };
}
public bool FollowRedirects
{
get { return rest.FollowRedirects; }
set { rest.FollowRedirects = value; }
}
public Func<bool> UnauthenticatedHandler
{
get; set;
}
public IAuthenticator Authenticator
{
get { return rest.Authenticator; }
set { rest.Authenticator = value; }
}
public Uri BaseUrl
{
get { return rest.BaseUrl; }
set { rest.BaseUrl = value; }
}
public void AddDefaultHeader(string header, string value)
{
rest.AddDefaultHeader(header, value);
}
public T PostForObject<T>(string url) where T : new()
{
return PostForObject<T>(url, null);
}
public T PostForObject<T>(string url, object body) where T : new()
{
var request = new RestRequest(url, Method.POST) { JsonSerializer = jsonSerializer };
request.AddJsonBody(body);
return JsonConvert.DeserializeObject<T>(Exchange(request).Content);
}
public T GetForObject<T>(string url) where T : new()
{
return JsonConvert.DeserializeObject<T>(Exchange(new RestRequest(url, Method.GET)).Content);
}
public T PutForObject<T>(string url, object body) where T : new()
{
var request = new RestRequest(url, Method.PUT) { JsonSerializer = jsonSerializer };
request.AddJsonBody(body);
return JsonConvert.DeserializeObject<T>(Exchange(request).Content);
}
public T GetForObject<T>(string url, IDictionary<string, object> parameters) where T : new()
{
var request = new RestRequest(url, Method.GET);
foreach (var parameter in parameters)
{
request.AddParameter(parameter.Key, parameter.Value);
}
return JsonConvert.DeserializeObject<T>(Exchange(request).Content);
}
public IRestResponse GetForEntity(string url, IDictionary<string, object> parameters)
{
var request = new RestRequest(url, Method.GET);
foreach (var parameter in parameters)
{
request.AddParameter(parameter.Key, parameter.Value);
}
return Exchange(request);
}
public T Exchange<T>(RestRequest request)
{
return JsonConvert.DeserializeObject<T>(Exchange(request).Content);
}
public IRestResponse Exchange(RestRequest request)
{
try
{
return TryExchange(request);
}
catch (UnauthorizedAccessException)
{
if (UnauthenticatedHandler == null) throw;
if (!UnauthenticatedHandler()) throw;
return TryExchange(request);
}
}
private IRestResponse TryExchange(RestRequest request)
{
var response = rest.Execute(request);
CheckHttpStatusCode(response);
return response;
}
private void CheckHttpStatusCode(IRestResponse response)
{
if (response.ErrorException != null) throw response.ErrorException;
if (response.StatusCode < HttpStatusCode.BadRequest) return;
if (response.StatusCode.Equals(HttpStatusCode.Unauthorized)) throw new UnauthorizedAccessException();
throw new RestException("error.error-from-server", response.Content);
}
}
}