-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.cs
127 lines (104 loc) · 4.92 KB
/
Client.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
using RestSharp;
using RestSharp.Authenticators;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
namespace TripItExport
{
public record Airport(string Code, double Latitude, double Longitude);
public record Flight(Airport Origin, Airport Destination);
public class Client : IDisposable
{
readonly RestClient _client;
public Client(
string clientToken,
string clientTokenSecret,
string accessToken,
string accessTokenSecret)
{
var options = new RestClientOptions("https://api.tripit.com");
_client = new RestClient(options)
{
Authenticator = OAuth1Authenticator.ForProtectedResource(
clientToken,
clientTokenSecret,
accessToken,
accessTokenSecret),
};
}
public async Task<Guid?> GetUserUUID()
{
var url = $"/v1/get/profile?format=json".ToLower();
RestResponse? response = await _client.GetAsync(new RestRequest(url));
if (response is null || response.Content is null) return null;
JsonDocument? jsonDocument = JsonDocument.Parse(response.Content);
if (jsonDocument is null) return null;
JsonElement rootElement = jsonDocument.RootElement;
if (!rootElement.TryGetProperty("Profile", out JsonElement profile) ||
profile.ValueKind != JsonValueKind.Object ||
!profile.TryGetPropertyAsString("screen_name", out string screenName) ||
!profile.TryGetPropertyAsGuid("uuid", out Guid uuid)) return null;
return uuid;
}
public async Task<Flight[]> GetFlights()
{
var objects = true;
var page_size = 10;
var format = "json";
var traveler = true;
var past = true;
var page = 1;
var result = new HashSet<Flight>();
while (true)
{
var url = $"/v1/list/trip/traveler/{traveler}/past/{past}/include_objects/{objects}?format={format}&page_size={page_size}&page_num={page}".ToLower();
RestResponse? response = await _client.GetAsync(new RestRequest(url));
if (response is null || response.Content is null) break;
JsonDocument? jsonDocument = JsonDocument.Parse(response.Content);
if (jsonDocument is null) break;
JsonElement rootElement = jsonDocument.RootElement;
if (rootElement.TryGetPropertyAsArray("AirObject", out JsonElement[] airs))
{
foreach (var air in airs)
{
if (air.TryGetPropertyAsArray("Segment", out JsonElement[] segments))
{
foreach (var segment in segments)
{
if (segment.TryGetPropertyAsString("start_airport_code", out string startAirportCode) &&
segment.TryGetPropertyAsDouble("start_airport_latitude", out double startAirportLatitude) &&
segment.TryGetPropertyAsDouble("start_airport_longitude", out double startAirportLongitude) &&
segment.TryGetPropertyAsString("end_airport_code", out string endAirportCode) &&
segment.TryGetPropertyAsDouble("end_airport_latitude", out double endAirportLatitude) &&
segment.TryGetPropertyAsDouble("end_airport_longitude", out double endAirportLongitude))
{
result.Add(new Flight(
new Airport(
startAirportCode,
startAirportLatitude,
startAirportLongitude),
new Airport(
endAirportCode,
endAirportLatitude,
endAirportLongitude)
));
}
}
}
}
}
if (!rootElement.TryGetPropertyAsInt("max_page", out int maxPage)) break;
if (page >= maxPage) break;
page++;
}
return result.ToArray();
}
public void Dispose()
{
_client?.Dispose();
GC.SuppressFinalize(this);
}
}
}