This repository was archived by the owner on Feb 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathTeamCityCaller.cs
More file actions
295 lines (236 loc) · 11.1 KB
/
TeamCityCaller.cs
File metadata and controls
295 lines (236 loc) · 11.1 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
using System;
using System.IO;
using System.Net;
using System.Security.Authentication;
using EasyHttp.Http;
using TeamCitySharp.DomainEntities;
using File = System.IO.File;
using HttpException = EasyHttp.Infrastructure.HttpException;
using HttpResponse = EasyHttp.Http.HttpResponse;
namespace TeamCitySharp.Connection
{
internal class TeamCityCaller : ITeamCityCaller
{
public int httpTimeOut { get; set; }
private readonly Credentials _configuration = new Credentials();
public TeamCityCaller(string hostName, bool useSsl)
{
if (string.IsNullOrEmpty(hostName))
throw new ArgumentNullException("hostName");
httpTimeOut = 10000;
_configuration.UseSSL = useSsl;
_configuration.HostName = hostName;
}
public void Connect(string userName, string password, bool actAsGuest)
{
_configuration.Password = password;
_configuration.UserName = userName;
_configuration.ActAsGuest = actAsGuest;
}
public T GetFormat<T>(string urlPart, params object[] parts)
{
return Get<T>(string.Format(urlPart, parts));
}
public void GetFormat(string urlPart, params object[] parts)
{
Get(string.Format(urlPart, parts));
}
public T PostFormat<T>(object data, string contenttype, string accept, string urlPart, params object[] parts)
{
return Post<T>(data.ToString(), contenttype, string.Format(urlPart, parts), accept);
}
public void PostFormat(object data, string contenttype, string urlPart, params object[] parts)
{
Post(data.ToString(), contenttype, string.Format(urlPart, parts), string.Empty);
}
public void PutFormat(object data, string contenttype, string urlPart, params object[] parts)
{
Put(data.ToString(), contenttype, string.Format(urlPart, parts), string.Empty);
}
public void DeleteFormat(string urlPart, params object[] parts)
{
Delete(string.Format(urlPart, parts));
}
public void GetDownloadFormat(Action<string> downloadHandler, string urlPart, params object[] parts)
{
if (CheckForUserNameAndPassword())
{
throw new ArgumentException("If you are not acting as a guest you must supply userName and password");
}
if (string.IsNullOrEmpty(urlPart))
{
throw new ArgumentException("Url must be specfied");
}
if (downloadHandler == null)
{
throw new ArgumentException("A download handler must be specfied.");
}
string tempFileName = Path.GetRandomFileName();
var url = CreateUrl(string.Format(urlPart, parts));
try
{
CreateHttpClient(_configuration.UserName, _configuration.Password, HttpContentTypes.ApplicationJson).GetAsFile(url, tempFileName);
downloadHandler.Invoke(tempFileName);
}
finally
{
if (File.Exists(tempFileName))
{
File.Delete(tempFileName);
}
}
}
public string StartBackup(string urlPart)
{
if (CheckForUserNameAndPassword())
throw new ArgumentException("If you are not acting as a guest you must supply userName and password");
if (string.IsNullOrEmpty(urlPart))
throw new ArgumentException("Url must be specfied");
var url = CreateUrl(urlPart);
var httpClient = CreateHttpClient(_configuration.UserName, _configuration.Password, HttpContentTypes.TextPlain);
var response = httpClient.Post(url, null, HttpContentTypes.TextPlain);
ThrowIfHttpError(response, url);
if (response.StatusCode == HttpStatusCode.OK)
{
return response.RawText;
}
return string.Empty;
}
public T Get<T>(string urlPart)
{
var response = GetResponse(urlPart);
return response.StaticBody<T>();
}
public void Get(string urlPart)
{
GetResponse(urlPart);
}
private HttpResponse GetResponse(string urlPart)
{
if (CheckForUserNameAndPassword())
throw new ArgumentException("If you are not acting as a guest you must supply userName and password");
if (string.IsNullOrEmpty(urlPart))
throw new ArgumentException("Url must be specfied");
var url = CreateUrl(urlPart);
var response = CreateHttpClient(_configuration.UserName, _configuration.Password, HttpContentTypes.ApplicationJson).Get(url);
ThrowIfHttpError(response, url);
return response;
}
public T Post<T>(string data, string contenttype, string urlPart, string accept)
{
return Post(data, contenttype, urlPart, accept).StaticBody<T>();
}
public bool Authenticate(string urlPart)
{
try
{
var httpClient = CreateHttpClient(_configuration.UserName, _configuration.Password, HttpContentTypes.TextPlain);
httpClient.ThrowExceptionOnHttpError = true;
httpClient.Get(CreateUrl(urlPart));
var response = httpClient.Response;
return response.StatusCode == HttpStatusCode.OK;
}
catch (HttpException exception)
{
throw new AuthenticationException(exception.StatusDescription);
}
}
public HttpResponse Post(object data, string contenttype, string urlPart, string accept)
{
var client = MakePostRequest(data, contenttype, urlPart, accept);
return client.Response;
}
public HttpResponse Put(object data, string contenttype, string urlPart, string accept)
{
var client = MakePutRequest(data, contenttype, urlPart, accept);
return client.Response;
}
public void Delete(string urlPart)
{
MakeDeleteRequest(urlPart);
}
private void MakeDeleteRequest(string urlPart)
{
var client = CreateHttpClient(_configuration.UserName, _configuration.Password, HttpContentTypes.TextPlain);
client.Delete(CreateUrl(urlPart));
ThrowIfHttpError(client.Response, client.Request.Uri);
}
private HttpClient MakePostRequest(object data, string contenttype, string urlPart, string accept)
{
var client = CreateHttpClient(_configuration.UserName, _configuration.Password, string.IsNullOrWhiteSpace(accept) ? GetContentType(data.ToString()) : accept);
client.Request.Accept = accept;
client.Post(CreateUrl(urlPart), data, contenttype);
ThrowIfHttpError(client.Response, client.Request.Uri);
return client;
}
private HttpClient MakePutRequest(object data, string contenttype, string urlPart, string accept)
{
var client = CreateHttpClient(_configuration.UserName, _configuration.Password, string.IsNullOrWhiteSpace(accept) ? GetContentType(data.ToString()) : accept);
client.Request.Accept = accept;
client.Put(CreateUrl(urlPart), data, contenttype);
ThrowIfHttpError(client.Response, client.Request.Uri);
return client;
}
private static bool IsHttpError(HttpResponse response)
{
var num = (int)response.StatusCode / 100;
return (num == 4 || num == 5);
}
/// <summary>
/// <para>If the <paramref name="response"/> is OK (see <see cref="IsHttpError"/> for definition), does nothing.</para>
/// <para>Otherwise, throws an exception which includes also the response raw text.
/// This would often contain a Java exception dump from the TeamCity REST Plugin, which reveals the cause of some cryptic cases otherwise showing just "Bad Request" in the HTTP error.
/// Also this comes in handy when TeamCity goes into maintenance, and you get back the banner in HTML instead of your data.</para>
/// </summary>
private static void ThrowIfHttpError(HttpResponse response, string url)
{
if(!IsHttpError(response))
return;
throw new HttpException(response.StatusCode, string.Format("Error: {0}\nHTTP: {3}\nURL: {1}\n{2}", response.StatusDescription, url, response.RawText, response.StatusCode));
}
private string CreateUrl(string urlPart)
{
var protocol = _configuration.UseSSL ? "https://" : "http://";
var authType = _configuration.ActAsGuest ? "/guestAuth" : "/httpAuth";
return string.Format("{0}{1}{2}{3}", protocol, _configuration.HostName, authType, urlPart);
}
private HttpClient CreateHttpClient(string userName, string password, string accept)
{
var httpClient = new HttpClient(new TeamcityJsonEncoderDecoderConfiguration());
httpClient.Request.Accept = accept;
httpClient.Request.Timeout = httpTimeOut;
if (!_configuration.ActAsGuest)
{
httpClient.Request.SetBasicAuthentication(userName, password);
httpClient.Request.ForceBasicAuth = true;
}
return httpClient;
}
// only used by the artifact listing methods since i havent found a way to deserialize them into a domain entity
public string GetRaw(string urlPart)
{
if (CheckForUserNameAndPassword())
throw new ArgumentException("If you are not acting as a guest you must supply userName and password");
if (string.IsNullOrEmpty(urlPart))
throw new ArgumentException("Url must be specfied");
var url = CreateUrl(urlPart);
var httpClient = CreateHttpClient(_configuration.UserName, _configuration.Password, HttpContentTypes.TextPlain);
var response = httpClient.Get(url);
if (IsHttpError(response))
{
throw new HttpException(response.StatusCode, string.Format("Error {0}: Thrown with URL {1}", response.StatusDescription, url));
}
return response.RawText;
}
private bool CheckForUserNameAndPassword()
{
return !_configuration.ActAsGuest && string.IsNullOrEmpty(_configuration.UserName) && string.IsNullOrEmpty(_configuration.Password);
}
private string GetContentType(string data)
{
if (data.StartsWith("<"))
return HttpContentTypes.ApplicationXml;
return HttpContentTypes.TextPlain;
}
}
}