From c98562bd19d2c753c566fee9244d01a5632aa439 Mon Sep 17 00:00:00 2001
From: FlareLine <101133651@swin.edu.au>
Date: Thu, 16 Nov 2017 14:28:53 +1100
Subject: [PATCH] Finished the Request class, should be able to make requests
and receive data using the library now.
---
PTVWrapper/API/Request.cs | 57 +++++++++++++++++++++++++++++++-----
PTVWrapper/Models/Payload.cs | 24 +++++++++++++++
PTVWrapper/PTVWrapper.csproj | 7 +++++
PTVWrapper/packages.config | 4 +++
4 files changed, 84 insertions(+), 8 deletions(-)
create mode 100644 PTVWrapper/Models/Payload.cs
create mode 100644 PTVWrapper/packages.config
diff --git a/PTVWrapper/API/Request.cs b/PTVWrapper/API/Request.cs
index 319e155..3ff5d89 100644
--- a/PTVWrapper/API/Request.cs
+++ b/PTVWrapper/API/Request.cs
@@ -1,6 +1,12 @@
-using System;
+using Newtonsoft.Json;
+using Newtonsoft.Json.Serialization;
+using PTVWrapper.Models;
+using System;
using System.Diagnostics;
+using System.IO;
+using System.Net.Http;
using System.Text;
+using System.Threading.Tasks;
namespace PTVWrapper.Request
{
@@ -28,7 +34,7 @@ public static string GenerateSignature(string re)
ASCIIEncoding encoding = new ASCIIEncoding();
//Encode both the developer key and the request url
- byte[] kb = encoding.GetBytes(DEV_ID);
+ byte[] kb = encoding.GetBytes(DEV_KEY);
byte[] ub = encoding.GetBytes(url);
// Generate the url token hash for authenticating the API request
@@ -41,13 +47,48 @@ public static string GenerateSignature(string re)
// Output the string from the StringBuilder
url = String.Format("{0}{1}&signature={2}", API_URL, url, sb.ToString());
- // Write the url to the console if debugging
-#if DEBUG
- Debug.Write(url);
-#endif
-
// Return the API string
return url;
}
+
+ ///
+ /// Request data from the API using a pre-encoded signature string
+ ///
+ ///
+ ///
+ public static async Task RequestEncFromAPI(string re)
+ {
+ try
+ {
+ using (var client = new HttpClient())
+ {
+ return JsonConvert.DeserializeObject(await client.GetStringAsync(re), new JsonSerializerSettings
+ {
+ MissingMemberHandling = MissingMemberHandling.Ignore,
+ NullValueHandling = NullValueHandling.Ignore,
+ Error = HandleDeserializationError
+ });
+ }
+ } catch (HttpRequestException e)
+ {
+ // Couldn't retreive data from the API, either the API or your internet connection is not responding
+ Debug.Write(e.StackTrace);
+
+ // Return a blank Payload object
+ return await Task.FromResult(null);
+ }
+ }
+
+ public static async Task RequestUnencFromAPI(string re) => await RequestEncFromAPI(GenerateSignature(re));
+
+ ///
+ /// Handle any errors regarding receiving a null objects from the API
+ /// This stops the program trying to put the null object into the results
+ ///
+ static void HandleDeserializationError(object sender, Newtonsoft.Json.Serialization.ErrorEventArgs errorArgs)
+ {
+ var currentError = errorArgs.ErrorContext.Error.Message;
+ errorArgs.ErrorContext.Handled = true;
+ }
}
-}
+}
\ No newline at end of file
diff --git a/PTVWrapper/Models/Payload.cs b/PTVWrapper/Models/Payload.cs
new file mode 100644
index 0000000..cfde6ca
--- /dev/null
+++ b/PTVWrapper/Models/Payload.cs
@@ -0,0 +1,24 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace PTVWrapper.Models
+{
+ ///
+ /// API payload class, containing all the possible objects the PTV API can respond with
+ ///
+ public class Payload
+ {
+ public List Departures { get; set; }
+ public List Routes { get; set; }
+ public List Stops { get; set; }
+ public List Runs { get; set; }
+ public List Patterns { get; set; }
+ public List Locations { get; set; }
+ public List Disruptions { get; set; }
+ public List RouteTypes { get; set; }
+ public List Directions { get; set; }
+ }
+}
diff --git a/PTVWrapper/PTVWrapper.csproj b/PTVWrapper/PTVWrapper.csproj
index 7c6aaf2..4051fe6 100644
--- a/PTVWrapper/PTVWrapper.csproj
+++ b/PTVWrapper/PTVWrapper.csproj
@@ -30,6 +30,9 @@
4
+
+ packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll
+
@@ -40,6 +43,7 @@
+
@@ -52,6 +56,9 @@
+
+
+