using Adrenak.Unex;
using System.Text;
using System.Threading.Tasks;
using System;
using UnityEngine;
using RestSharp;
namespace Adrenak.UniMap {
public class StreetViewMetaRequest {
///
/// The base URL of the Street View API
///
const string k_BaseURL = "https://maps.googleapis.com/maps/api/streetview/metadata?";
///
/// The options for the meta data request
///
public StreetView.Options options = new StreetView.Options();
///
/// Gets the request URL for the given face given the API parameters.
///
/// The whose texture is to be downloaded.
/// The URL for downloading the texture of this
public string GetURL() {
var sb = new StringBuilder(k_BaseURL).Append("key=").Append(options.key);
switch (options.mode) {
case StreetView.Mode.Coordinates:
sb.Append("&location=").Append(options.location.lat).Append(",").Append(options.location.lng);
break;
case StreetView.Mode.PanoID:
sb.Append("&pano=").Append(options.panoID);
break;
case StreetView.Mode.Location:
sb.Append("&location=").Append(options.place);
break;
}
sb.Append("&radius=").Append(options.radius)
.Append("&source=").Append(EnumToString.From(options.source));
return sb.ToString();
}
///
/// Send the API request and returns the response or exception
///
/// Action that returns the response as a C# object
/// Action that returns the exception encountered in case of an error
public void Send(Action onResponse, Action onException) {
string url = string.Empty;
try {
url = GetURL();
}
catch (Exception e) {
onException(e);
}
var client = new RestClient();
var request = new RestRequest(url, Method.GET);
client.ExecuteAsync(request, response => {
Dispatcher.Enqueue(() => {
if (response.IsSuccessful()) {
try {
var result = JsonUtility.FromJson(response.Content);
onResponse?.Invoke(result);
}
catch (Exception e) {
onException?.Invoke(e);
}
}
else
onException?.Invoke(response.GetException());
});
});
}
///
/// Send the API request and return a task for the response
///
public Task Send() {
var source = new TaskCompletionSource();
Send(
response => source.SetResult(response),
exception => source.SetException(exception)
);
return source.Task;
}
}
}