This repository has been archived by the owner on Mar 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJCDecauxItem.cs
112 lines (91 loc) · 3.02 KB
/
JCDecauxItem.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
using System.Collections.Generic;
using System.Net.Http;
using System.Runtime.Serialization;
using System.Text.Json;
using System.Threading.Tasks;
namespace Proxy.Models
{
[DataContract]
public class JCDecauxItem
{
private static readonly string URL = "https://api.jcdecaux.com/vls/v2/";
private static readonly string DATA = "stations";
private static readonly string API_KEY = "ff987c28b1313700e2c97651cec164bd6cb4ed76";
private static readonly HttpClient client = new HttpClient();
private string request;
[DataMember]
public Station station { get; set; }
public JCDecauxItem()
{
// Nothing to do
}
public JCDecauxItem(Dictionary<string, string> dictionary)
{
request = URL + DATA + "/" + dictionary["number"] + "?contract=" + dictionary["city"] + "&apiKey=" + API_KEY;
station = CallAPI(request).Result;
}
private static async Task<Station> CallAPI(string request)
{
try
{
HttpResponseMessage response = await client.GetAsync(request);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<Station>(responseBody);
}
catch (HttpRequestException)
{
return new Station();
}
}
}
[DataContract]
public class Station
{
[DataMember]
public string contract_name { get; set; }
[DataMember]
public string name { get; set; }
[DataMember]
public string address { get; set; }
[DataMember]
public int number { get; set; }
[DataMember]
public Position position { get; set; }
[DataMember]
public int bike_stands { get; set; }
[DataMember]
public int available_bike_stands { get; set; }
[DataMember]
public int available_bikes { get; set; }
[DataMember]
public string status { get; set; }
public override string ToString()
{
return
"Contract Name: " + contract_name + "\n" +
"Address: " + address + "\n" +
"Name: " + name + "\n" +
"Number: " + number + "\n" +
position.ToString() +
"Bike Stands: " + bike_stands + "\n" +
"Available Bike Stands: " + available_bike_stands + "\n" +
"Available Bikes: " + available_bikes + "\n" +
"Status: " + status + "\n";
}
}
[DataContract]
public class Position
{
[DataMember]
public double latitude { get; set; }
[DataMember]
public double longitude { get; set; }
public override string ToString()
{
return
"Latitude: " + latitude + "\n" +
"Longitude: " + longitude + "\n";
}
}
}