//----------------------------------------------------------------------- // // Copyright (c) 2016 Mapbox. All rights reserved. // //----------------------------------------------------------------------- namespace Mapbox.Directions { using System; using System.Text; using Mapbox.Json; using Mapbox.Platform; using Mapbox.Utils.JsonConverters; /// /// Wrapper around the /// Mapbox Directions API. The Mapbox Directions API will show you how to get where /// you're going. /// public sealed class Directions { private readonly IFileSource fileSource; /// Initializes a new instance of the class. /// Network access abstraction. public Directions(IFileSource fileSource) { this.fileSource = fileSource; } /// Performs asynchronously a directions lookup. /// Direction resource. /// Callback to be called after the request is completed. /// /// Returns a that can be used for canceling a pending /// request. This handle can be completely ignored if there is no intention of ever /// canceling the request. /// public IAsyncRequest Query(DirectionResource direction, Action callback) { return this.fileSource.Request( direction.GetUrl(), (Response response) => { var str = Encoding.UTF8.GetString(response.Data); var data = Deserialize(str); callback(data); }); } /// /// Deserialize the geocode response string into a . /// /// JSON String. /// A . public DirectionsResponse Deserialize(string str) { return JsonConvert.DeserializeObject(str, JsonConverters.Converters); } public string Serialize(DirectionsResponse response) { return JsonConvert.SerializeObject(response, JsonConverters.Converters); } } }