This repository has been archived by the owner on Apr 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
83 lines (75 loc) · 2.48 KB
/
Program.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
using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
using System.Net;
using System.Net.Http;
using Newtonsoft.Json;
using System.Threading.Tasks;
namespace PTV_Demo
{
class Program
{
static string baseURL = "http://timetableapi.ptv.vic.gov.au";
static string url = "/v3/disruptions/route/8";
static void Main(string[] args)
{
//load api key and user ID
Settings settings = new Helpers().loadSettings("settings.json");
Console.WriteLine("PTV example.. Beginning ");
url = $"{url}?devid={settings.userID}";//add the user is to the URL to create
ASCIIEncoding encoding = new ASCIIEncoding();
// encode key
byte[] keyBytes = encoding.GetBytes(settings.apiKey);
byte[] urlBytes = encoding.GetBytes(url);
byte[] tokenBytes = new HMACSHA1(keyBytes).ComputeHash(urlBytes);
var sb = new StringBuilder();
// convert signature to string
Array.ForEach<byte>(tokenBytes, x => sb.Append(x.ToString("X2")));
// add signature to url
url = $"{url}&signature={sb.ToString()}";
Console.WriteLine("Url generated by the encoding: " + url);
Console.WriteLine(new Helpers().getJson(baseURL + url).Result);
}
}
class Helpers
{
public Settings loadSettings(string fileName)
{
try
{
using (StreamReader sr = new StreamReader(fileName))
{
string json = sr.ReadToEnd();
return JsonConvert.DeserializeObject<Settings>(json);
}
}
catch (Exception ex)
{
Console.WriteLine("Failed to load settings file: " + ex);
return null;
}
}
public Task<string> getJson(string URL)
{
try {
using(WebClient wc = new WebClient())
{
return wc.DownloadStringTaskAsync(URL);
}
}
catch (Exception ex)
{
Console.WriteLine("Failed to retrieve json file" + ex);
return null;
}
}
}
public class Settings
{
[JsonProperty("user_id")]
public int userID { get; set; }
[JsonProperty("api_key")]
public string apiKey { get; set; }
}
}