Skip to content

Commit

Permalink
Finished the Request class, should be able to make requests and recei…
Browse files Browse the repository at this point in the history
…ve data using the library now.
  • Loading branch information
FlareLine committed Nov 16, 2017
1 parent b4b8190 commit c98562b
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 8 deletions.
57 changes: 49 additions & 8 deletions PTVWrapper/API/Request.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -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
Expand All @@ -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;
}

/// <summary>
/// Request data from the API using a pre-encoded signature string
/// </summary>
/// <param name="re"></param>
/// <returns></returns>
public static async Task<Payload> RequestEncFromAPI(string re)
{
try
{
using (var client = new HttpClient())
{
return JsonConvert.DeserializeObject<Payload>(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<Payload>(null);
}
}

public static async Task<Payload> RequestUnencFromAPI(string re) => await RequestEncFromAPI(GenerateSignature(re));

/// <summary>
/// Handle any errors regarding receiving a null objects from the API
/// This stops the program trying to put the null object into the results
/// </summary>
static void HandleDeserializationError(object sender, Newtonsoft.Json.Serialization.ErrorEventArgs errorArgs)
{
var currentError = errorArgs.ErrorContext.Error.Message;
errorArgs.ErrorContext.Handled = true;
}
}
}
}
24 changes: 24 additions & 0 deletions PTVWrapper/Models/Payload.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PTVWrapper.Models
{
/// <summary>
/// API payload class, containing all the possible objects the PTV API can respond with
/// </summary>
public class Payload
{
public List<Departure> Departures { get; set; }
public List<Route> Routes { get; set; }
public List<Stop> Stops { get; set; }
public List<Run> Runs { get; set; }
public List<Pattern> Patterns { get; set; }
public List<Location> Locations { get; set; }
public List<Disruption> Disruptions { get; set; }
public List<RouteType> RouteTypes { get; set; }
public List<Direction> Directions { get; set; }
}
}
7 changes: 7 additions & 0 deletions PTVWrapper/PTVWrapper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
Expand All @@ -40,6 +43,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Models\Payload.cs" />
<Compile Include="Models\Departure.cs" />
<Compile Include="Models\Disruption.cs" />
<Compile Include="Models\Location.cs" />
Expand All @@ -52,6 +56,9 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="API\Request.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
4 changes: 4 additions & 0 deletions PTVWrapper/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net452" />
</packages>

0 comments on commit c98562b

Please sign in to comment.