//-----------------------------------------------------------------------
//
// Copyright (c) 2016 Mapbox. All rights reserved.
//
//-----------------------------------------------------------------------
namespace Mapbox.Platform
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
#if UNITY_IOS
using UnityEngine;
#endif
/// Abstract class representing a Mapbox resource URL.
public abstract class Resource
{
/// Gets the API endpoint, which is a partial URL path.
public abstract string ApiEndpoint { get; }
/// Builds a complete, valid URL string.
/// Returns URL string.
public abstract string GetUrl();
/// Encodes a URI with a querystring.
/// Querystring values.
/// Encoded URL.
protected static String EncodeQueryString(IEnumerable> values)
{
if (values != null)
{
// we are seeing super weird crashes on some iOS devices:
// see 'ForwardGeocodeResource' for more details
var encodedValues = from p in values
#if UNITY_IOS
#if UNITY_2017_1_OR_NEWER
let k = UnityEngine.Networking.UnityWebRequest.EscapeURL(p.Key.Trim())
let v = UnityEngine.Networking.UnityWebRequest.EscapeURL(p.Value)
#else
let k = WWW.EscapeURL(p.Key.Trim())
let v = WWW.EscapeURL(p.Value)
#endif
#else
let k = Uri.EscapeDataString(p.Key.Trim())
let v = Uri.EscapeDataString(p.Value)
#endif
orderby k
select string.IsNullOrEmpty(v) ? k : string.Format("{0}={1}", k, v);
if (encodedValues.Count() == 0)
{
return string.Empty;
}
else
{
return "?" + string.Join(
"&", encodedValues.ToArray());
}
}
return string.Empty;
}
/// Builds a string from an array of options for use in URLs.
/// Array of option strings.
/// Character to use for separating items in arry. Defaults to ",".
/// Comma-separated string of options.
/// Type in the array.
protected static string GetUrlQueryFromArray(U[] items, string separator = ",")
{
return string.Join(separator, items.Select(item => item.ToString()).ToArray());
}
}
}