-
Notifications
You must be signed in to change notification settings - Fork 9
/
TripIt.cs
503 lines (421 loc) · 18 KB
/
TripIt.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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/*
* Copyright 2008-2018 Concur Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
using System;
using System.Linq;
using System.Net;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Xml.Serialization;
namespace TripIt {
/// <summary>
/// The TripIt API. Create an instance of this with a credential
/// and start calling methods. The API methods generally return
/// Response objects, which are parsed versions of the XML.
/// See the TripIt API documentation for details of the objects
/// available.
/// </summary>
public class TripIt {
private const string API_VERSION = "v1";
private Credential _credential;
private string _apiUrl;
/// <summary>
/// Create a TripIt API object given a credential.
/// </summary>
/// <param name="credential">
/// A <see cref="Credential"/>
/// </param>
public TripIt(Credential credential)
: this(credential, "https://api.tripit.com")
{}
/// <summary>
/// Create a TripIt API object given a credential and an
/// API URL. For developers external to TripIt, the API
/// URL should generally be omitted. The default points at the
/// public API URL.
/// </summary>
/// <param name="credential">
/// A <see cref="Credential"/>
/// </param>
/// <param name="apiUrl">
/// An API URL.
/// </param>
public TripIt(Credential credential, string apiUrl) {
_credential = credential;
_apiUrl = apiUrl;
}
/// <value>
/// The current Credential.
/// </value>
public Credential Credential {
get {
return _credential;
}
}
/// <summary>
/// For OAuth clients, retrieve a request token. If this is
/// successful, it will set the current credential to include the
/// new request token. It will also return the new credential.
/// </summary>
/// <returns>
/// The new <see cref="OAuthCredential"/>, which includes the request
/// token.
/// </returns>
public OAuthCredential GetRequestToken() {
return GetOAuthToken("request");
}
/// <summary>
/// For OAuth clients, retrieve an access token, given a
/// valid and authorized request token. If this is successful,
/// it will set the current credential to include the new access
/// token. It will also return the new credential.
/// </summary>
/// <returns>
/// The new <see cref="OAuthCredential"/>, which includes the access
/// token.
/// </returns>
public OAuthCredential GetAccessToken() {
return GetOAuthToken("access");
}
private OAuthCredential GetOAuthToken(string type) {
OAuthCredential credential = _credential as OAuthCredential;
if (credential == null) {
return null;
}
Dictionary<string, string> token = DoQueryStringRequest(
"/oauth/" + type + "_token", null, null, null);
credential = new OAuthCredential(
credential.ConsumerKey,
credential.ConsumerSecret,
token["oauth_token"],
token["oauth_token_secret"]);
_credential = credential;
return credential;
}
/// <summary>
/// Get a Trip with the given ID.
/// </summary>
/// <param name="id">
/// The ID of the Trip to get.
/// </param>
/// <returns>
/// A <see cref="TravelObject.Response"/> containing the requested Trip.
/// </returns>
public TravelObject.Response GetTrip(long id) {
return GetTrip(id, new Dictionary<string, string>());
}
/// <summary>
/// Get a Trip with the given ID.
/// </summary>
/// <param name="id">
/// The ID of the Trip to get.
/// </param>
/// <param name="filter">
/// A <see cref="Dictionary"/> containing any additional filter
/// parameters. Currently setting "include_objects" to "true" will
/// include all objects that are associated with the requested trip
/// in the response.
/// </param>
/// <returns>
/// A <see cref="TravelObject.Response"/>
/// </returns>
public TravelObject.Response GetTrip(long id, Dictionary<string, string> filter) {
filter.Add("id", id.ToString());
return DoRequest("get", "trip", filter, null);
}
public TravelObject.Response GetAir(long id) {
return DoRequest("get", "air", IdFilter(id), null);
}
public TravelObject.Response GetLodging(long id) {
return DoRequest("get", "lodging", IdFilter(id), null);
}
public TravelObject.Response GetCar(long id) {
return DoRequest("get", "car", IdFilter(id), null);
}
public TravelObject.Response GetProfile() {
return DoRequest("get", "profile", null, null);
}
public TravelObject.Response GetRail(long id) {
return DoRequest("get", "rail", IdFilter(id), null);
}
public TravelObject.Response GetTransport(long id) {
return DoRequest("get", "transport", IdFilter(id), null);
}
public TravelObject.Response GetCruise(long id) {
return DoRequest("get", "cruise", IdFilter(id), null);
}
public TravelObject.Response GetRestaurant(long id) {
return DoRequest("get", "restaurant", IdFilter(id), null);
}
public TravelObject.Response GetActivity(long id) {
return DoRequest("get", "activity", IdFilter(id), null);
}
public TravelObject.Response GetNote(long id) {
return DoRequest("get", "note", IdFilter(id), null);
}
public TravelObject.Response GetMap(long id) {
return DoRequest("get", "map", IdFilter(id), null);
}
public TravelObject.Response GetDirections(long id) {
return DoRequest("get", "directions", IdFilter(id), null);
}
public TravelObject.Response GetPointsProgram(long id) {
return DoRequest("get", "points_program", IdFilter(id), null);
}
public TravelObject.Response DeleteTrip(long id) {
return DoRequest("delete", "trip", IdFilter(id), null);
}
public TravelObject.Response DeleteAir(long id) {
return DoRequest("delete", "air", IdFilter(id), null);
}
public TravelObject.Response DeleteLodging(long id) {
return DoRequest("delete", "lodging", IdFilter(id), null);
}
public TravelObject.Response DeleteCar(long id) {
return DoRequest("delete", "car", IdFilter(id), null);
}
public TravelObject.Response DeleteRail(long id) {
return DoRequest("delete", "rail", IdFilter(id), null);
}
public TravelObject.Response DeleteTransport(long id) {
return DoRequest("delete", "transport", IdFilter(id), null);
}
public TravelObject.Response DeleteCruise(long id) {
return DoRequest("delete", "cruise", IdFilter(id), null);
}
public TravelObject.Response DeleteRestaurant(long id) {
return DoRequest("delete", "restaurant", IdFilter(id), null);
}
public TravelObject.Response DeleteActivity(long id) {
return DoRequest("delete", "activity", IdFilter(id), null);
}
public TravelObject.Response DeleteNote(long id) {
return DoRequest("delete", "note", IdFilter(id), null);
}
public TravelObject.Response DeleteMap(long id) {
return DoRequest("delete", "map", IdFilter(id), null);
}
public TravelObject.Response DeleteDirections(long id) {
return DoRequest("delete", "directions", IdFilter(id), null);
}
public TravelObject.Response ReplaceTrip(long id, string xml) {
Dictionary<string, string> filter = IdFilter(id);
filter.Add("xml", xml);
return DoRequest("replace", "trip", null, filter);
}
public TravelObject.Response ReplaceAir(long id, string xml) {
Dictionary<string, string> filter = IdFilter(id);
filter.Add("xml", xml);
return DoRequest("replace", "air", null, filter);
}
public TravelObject.Response ReplaceLodging(long id, string xml) {
Dictionary<string, string> filter = IdFilter(id);
filter.Add("xml", xml);
return DoRequest("replace", "lodging", null, filter);
}
public TravelObject.Response ReplaceCar(long id, string xml) {
Dictionary<string, string> filter = IdFilter(id);
filter.Add("xml", xml);
return DoRequest("replace", "car", null, filter);
}
public TravelObject.Response ReplaceRail(long id, string xml) {
Dictionary<string, string> filter = IdFilter(id);
filter.Add("xml", xml);
return DoRequest("replace", "rail", null, filter);
}
public TravelObject.Response ReplaceTransport(long id, string xml) {
Dictionary<string, string> filter = IdFilter(id);
filter.Add("xml", xml);
return DoRequest("replace", "transport", null, filter);
}
public TravelObject.Response ReplaceCruise(long id, string xml) {
Dictionary<string, string> filter = IdFilter(id);
filter.Add("xml", xml);
return DoRequest("replace", "cruise", null, filter);
}
public TravelObject.Response ReplaceRestaurant(long id, string xml) {
Dictionary<string, string> filter = IdFilter(id);
filter.Add("xml", xml);
return DoRequest("replace", "restaurant", null, filter);
}
public TravelObject.Response ReplaceActivity(long id, string xml) {
Dictionary<string, string> filter = IdFilter(id);
filter.Add("xml", xml);
return DoRequest("replace", "activity", null, filter);
}
public TravelObject.Response ReplaceNote(long id, string xml) {
Dictionary<string, string> filter = IdFilter(id);
filter.Add("xml", xml);
return DoRequest("replace", "note", null, filter);
}
public TravelObject.Response ReplaceMap(long id, string xml) {
Dictionary<string, string> filter = IdFilter(id);
filter.Add("xml", xml);
return DoRequest("replace", "map", null, filter);
}
public TravelObject.Response ReplaceDirections(long id, string xml) {
Dictionary<string, string> filter = IdFilter(id);
filter.Add("xml", xml);
return DoRequest("replace", "directions", null, filter);
}
public TravelObject.Response ListTrip() {
return ListTrip(null);
}
public TravelObject.Response ListTrip(Dictionary<string, string> filter) {
return DoRequest("list", "trip", filter, null);
}
public TravelObject.Response ListObject() {
return ListObject(null);
}
public TravelObject.Response ListObject(Dictionary<string, string> filter) {
return DoRequest("list", "object", filter, null);
}
public TravelObject.Response ListPointsProgram() {
return DoRequest("list", "points_program", null, null);
}
/// <summary>
/// Create an object.
/// </summary>
/// <param name="xml">
/// A <see cref="System.String"/> containing XML that represents the
/// object to create.
/// </param>
/// <returns>
/// A <see cref="TravelObject.Response"/>
/// </returns>
public TravelObject.Response Create(string xml) {
Dictionary<string, string> xmlArg = new Dictionary<string, string>();
xmlArg.Add("xml", xml);
return DoRequest("create", null, null, xmlArg);
}
public TravelObject.Response CrsLoadReservations(string xml) {
return CrsLoadReservations(xml, null, null);
}
public TravelObject.Response CrsLoadReservations(string xml, string companyKey) {
return CrsLoadReservations(xml, companyKey, null);
}
public TravelObject.Response CrsLoadReservations(string xml, Dictionary<string, string> parameters) {
return CrsLoadReservations(xml, null, parameters);
}
public TravelObject.Response CrsLoadReservations(string xml, string companyKey, Dictionary<string, string> parameters) {
if (parameters == null) {
parameters = new Dictionary<string, string>();
}
parameters.Add("xml", xml);
if (companyKey != null) {
parameters.Add("company_key", companyKey);
}
return DoRequest("crsLoadReservations", null, null, parameters);
}
public TravelObject.Response CrsDeleteReservations(string recordLocator) {
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("record_locator", recordLocator);
return DoRequest("crsDeleteReservations", null, args, null);
}
private Dictionary<string, string> IdFilter(long id) {
Dictionary<string, string> filter = new Dictionary<string, string>();
filter.Add("id", id.ToString());
return filter;
}
private TravelObject.Response DoRequest(
string verb,
string entity,
Dictionary<string, string> urlArgs,
Dictionary<string, string> postArgs)
{
return DeserializeResponse(
DoRawRequest(verb, entity, urlArgs, postArgs));
}
private Dictionary<string, string> DoQueryStringRequest(
string verb,
string entity,
Dictionary<string, string> urlArgs,
Dictionary<string, string> postArgs)
{
return ParseQueryString(DoRawRequest(verb, entity, urlArgs, postArgs).ReadToEnd());
}
private Dictionary<string, string> ParseQueryString(string qs) {
Dictionary<string, string> result = new Dictionary<string, string>();
foreach (string itemStr in qs.Split("&".ToCharArray())) {
string[] item = itemStr.Split("=".ToCharArray(), 2);
if (item.Length > 1) {
result.Add(item[0], item[1]);
}
}
return result;
}
private TextReader DoRawRequest(
string verb,
string entity,
Dictionary<string, string> urlArgs,
Dictionary<string, string> postArgs)
{
string urlBase = null;
if ((new string[] {
"/oauth/request_token",
"/oauth/access_token"} as IList).Contains(verb))
{
urlBase = _apiUrl + verb;
}
else {
if (entity != null) {
urlBase = _apiUrl + "/" + API_VERSION + "/" + verb + "/" +
entity;
}
else {
urlBase = _apiUrl + "/" + API_VERSION + "/" + verb;
}
}
String url;
Dictionary<string, string> args = null;
if (urlArgs != null) {
args = urlArgs;
url = urlBase + "?" + Utility.UrlEncodeArgs(urlArgs);
}
else {
url = urlBase;
}
WebRequest request = WebRequest.Create(url);
if (postArgs != null) {
args = postArgs;
request.Method = "POST";
}
else {
request.Method = "GET";
}
_credential.Authorize(request, _apiUrl, urlBase, args);
if (postArgs != null) {
// Add postArgs as request's post data.
// Must happen after Authorize because we can't write a
// request body before adding the Authorization header.
request.ContentType = "application/x-www-form-urlencoded";
byte[] data = Encoding.UTF8.GetBytes(
Utility.UrlEncodeArgs(postArgs));
request.ContentLength = data.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();
}
WebResponse response = request.GetResponse();
return new StreamReader(response.GetResponseStream(), Encoding.UTF8);
}
private static TravelObject.Response DeserializeResponse(TextReader reader) {
XmlSerializer serializer = new XmlSerializer(typeof(TravelObject.Response));
return serializer.Deserialize(reader) as TravelObject.Response;
}
}
}