Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Description>.Net standard library for Azure Maps Services</Description>
<PackageProjectUrl>https://github.com/perfahlen/AzureMapsRestServices</PackageProjectUrl>
<PackageLicenseUrl>https://github.com/perfahlen/AzureMapsRestServices/blob/master/LICENSE</PackageLicenseUrl>
<RepositoryUrl>https://github.com/perfahlen/AzureMapsRestServices</RepositoryUrl>
<RepositoryUrl>https://github.com/perfahlen/AzureMapsRestServicet</RepositoryUrl>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<IncludeSymbols>true</IncludeSymbols>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<RepositoryType>GIT</RepositoryType>
<PackageTags>Azure Maps REST Toolkit</PackageTags>
<Version>6.0.0</Version>
<AssemblyVersion>6.0.0.0</AssemblyVersion>
<PackageTags>.NET Azure Maps REST API </PackageTags>
<Version>7.0.0</Version>
<AssemblyVersion>7.0.0.0</AssemblyVersion>
<RootNamespace>AzureMapsToolkit</RootNamespace>
<FileVersion>6.0.0.0</FileVersion>
<FileVersion>7.0.0.0</FileVersion>
<Authors>Per Fahlen,Vincent Baaij</Authors>
<Copyright>© 2021</Copyright>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="GeoJSON.Net" Version="1.1.70" />
<PackageReference Include="Azure.Core.Experimental" Version="0.1.0-preview.13" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>

</Project>
223 changes: 88 additions & 135 deletions AzureMapsRestToolkit/AzureMapsRestToolkit/AzureMapsServices.cs

Large diffs are not rendered by default.

136 changes: 64 additions & 72 deletions AzureMapsRestToolkit/AzureMapsRestToolkit/Common/BaseServices.cs
Original file line number Diff line number Diff line change
@@ -1,48 +1,43 @@
using AzureMapsToolkit.GeoJson;
using System;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Globalization;
using AzureMapsToolkit.Spatial;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

using Azure.Core.GeoJson;

namespace AzureMapsToolkit.Common
{
public class BaseServices
{
internal string Key { get; set; }

public BaseServices(string key)
{
Key = key;
}

internal async Task<HttpResponseMessage> GetHttpResponseMessage(string url, string data, HttpMethod method)
internal static async Task<HttpResponseMessage> GetHttpResponseMessage(string url, string data, HttpMethod method)
{

using (var client = new HttpClient())
{
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
using (var request = new HttpRequestMessage(method, url))
{
var content = new StringContent(data); //, Encoding.UTF8, "application/json"))
using var client = new HttpClient();
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
using var request = new HttpRequestMessage(method, url);
var content = new StringContent(data); //, Encoding.UTF8, "application/json"))

content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
request.Content = content;
var response = await client.SendAsync(request); //, HttpCompletionOption.ResponseHeadersRead);
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
request.Content = content;
var response = await client.SendAsync(request); //, HttpCompletionOption.ResponseHeadersRead);

response.EnsureSuccessStatusCode();
return response;
}
}
response.EnsureSuccessStatusCode();
return response;
}

string _url = string.Empty;
Expand All @@ -60,18 +55,19 @@ internal string Url
}


internal MultiPoint GetMultipPoint(IEnumerable<Coordinate> coordinates)
internal static GeoPointCollection GetMultiPoint(IEnumerable<Coordinate> coordinates)
{
double[,] points = new double[coordinates.Count(), 2];
List<GeoPoint> points = new(coordinates.Count());

//double[,] points = new double[coordinates.Count(), 2];
for (int i = 0; i < coordinates.Count(); i++)
{
points[i, 0] = coordinates.ElementAt(i).Longitude;
points[i, 1] = coordinates.ElementAt(i).Latitude;
GeoPoint point = new(coordinates.ElementAt(i).Longitude, coordinates.ElementAt(i).Latitude);
points.Add(point);
}

var multiPoint = new MultiPoint();
multiPoint.Coordinates = points;

GeoPointCollection multiPoint = new GeoPointCollection(points);

return multiPoint;

}
Expand All @@ -86,7 +82,7 @@ internal IEnumerable<string> GetSearchQuery<T>(IEnumerable<T> req) where T : Req
}
}

internal string GetQuery<T>(T request, bool includeApiVersion, bool includeLanguage = true, char firstChar = '&')
internal string GetQuery<T>(T request, bool includeApiVersion, bool includeLanguage = true, char firstChar = '&', bool toCamelCase = true)
where T : RequestBase
{
Type type = request.GetType();
Expand All @@ -106,9 +102,11 @@ internal string GetQuery<T>(T request, bool includeApiVersion, bool includeLangu

if (propertyInfo?.PropertyType.IsEnum == true || underLayingtype?.IsEnum == true)
{
argumentName = Char.ToLower(propertyInfo.Name[0]) + propertyInfo.Name.Substring(1);
argumentName = Char.ToLower(propertyInfo.Name[0]) + propertyInfo.Name[1..];
argumentValue = propertyInfo.GetValue(request).ToString().Replace(" ", "");

argumentValue = propertyInfo.GetValue(request).ToString().Replace(" ", ""); //.ToCamlCase();
if (toCamelCase)
argumentValue = argumentValue.ToCamelCase();
}
//else if (propertyInfo != null && propertyInfo.PropertyType.IsArray)
//{
Expand All @@ -123,15 +121,15 @@ internal string GetQuery<T>(T request, bool includeApiVersion, bool includeLangu

else
{
var jsonAttribute = propertyInfo.GetCustomAttributes(typeof(JsonPropertyAttribute), false);
var jsonAttribute = propertyInfo.GetCustomAttributes(typeof(JsonPropertyNameAttribute), false);
if (jsonAttribute.Length > 0)
{
argumentName = ((JsonPropertyAttribute)jsonAttribute[0]).PropertyName;
argumentName = ((JsonPropertyNameAttribute)jsonAttribute[0]).Name;
}
}

if (argumentName == string.Empty)
argumentName = Char.ToLower(propertyInfo.Name[0]) + propertyInfo.Name.Substring(1);
argumentName = Char.ToLower(propertyInfo.Name[0]) + propertyInfo.Name[1..];


if (string.IsNullOrEmpty(argumentValue))
Expand Down Expand Up @@ -169,56 +167,50 @@ internal string GetQuery<T>(T request, bool includeApiVersion, bool includeLangu



internal async Task<byte[]> GetByteArray(string baseAddress, string url)
internal static async Task<byte[]> GetByteArray(string baseAddress, string url)
{
using (var httpClient = new HttpClient())
{
httpClient.BaseAddress = new Uri(baseAddress);
var content = await httpClient.GetByteArrayAsync(url);
return content;
}
using var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri(baseAddress);
var content = await httpClient.GetByteArrayAsync(url);
return content;
}

internal async Task<Response<T>> ExecuteRequest<T, U>(string baseUrl, U req) where U : RequestBase
internal async Task<Response<T>> ExecuteRequest<T, U>(string baseUrl, U req, bool toCamelCase = true) where U : RequestBase
{
Url = string.Empty;
try
{
if (req != null)
Url += GetQuery<U>(req, true);
Url += GetQuery<U>(req, true, toCamelCase: toCamelCase);

using (var client = GetClient(baseUrl))
{
var data = await GetData<T>(client, Url);
var response = GetResponse<T>(data);
return response;
}
using HttpClient client = GetClient(baseUrl);
T data = await GetData<T>(client, Url);
Response<T> response = GetResponse(data);
return response;
}
catch (AzureMapsException ex)
{
return Response<T>.CreateErrorResponse(ex);
}
}

internal string GetQuerycontent(IEnumerable<string> queryCollection)
internal static string GetQuerycontent(IEnumerable<string> queryCollection)
{
dynamic q = new JObject();
q.batchItems = new JArray();
foreach (var item in queryCollection)
Batch b = new();

b.BatchItems = new List<BatchItem>(queryCollection.Count());
foreach (string item in queryCollection)
{
dynamic query = new JObject();
query.query = item;
q.batchItems.Add(query);
BatchItem bi = new ();
bi.Query = item;
b.BatchItems.Add(bi);
}


var queryContent = Newtonsoft.Json.JsonConvert.SerializeObject(q);
string queryContent = JsonSerializer.Serialize<Batch>(b);
return queryContent;
}



internal HttpClient GetClient(string baseAddress)
internal static HttpClient GetClient(string baseAddress)
{
var client = new HttpClient();
if (!string.IsNullOrEmpty(baseAddress))
Expand All @@ -228,7 +220,7 @@ internal HttpClient GetClient(string baseAddress)
return client;
}

internal Response<T> GetResponse<T>(T t)
internal static Response<T> GetResponse<T>(T t)
{
var res = new Response<T>
{
Expand All @@ -237,27 +229,27 @@ internal Response<T> GetResponse<T>(T t)
return res;
}

internal async Task<T> GetData<T>(HttpClient client, string url)
internal static async Task<T> GetData<T>(HttpClient client, string url)
{
var res = await client.GetAsync(url);
if (res.StatusCode == System.Net.HttpStatusCode.OK)
{
var json = res.Content.ReadAsStringAsync().Result;
var val = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json);
var val = JsonSerializer.Deserialize<T>(json);
return val;
}
else
{
string content = res.Content.ReadAsStringAsync().Result;

var ex = Newtonsoft.Json.JsonConvert.DeserializeObject<ErrorResponse>(content);
var ex = JsonSerializer.Deserialize<ErrorResponse>(content);

throw new AzureMapsException(ex);
}

}

internal string GetUdidFromLocation(string location)
internal static string GetUdidFromLocation(string location)
{
using (var client = GetClient(location))
{
Expand Down
11 changes: 11 additions & 0 deletions AzureMapsRestToolkit/AzureMapsRestToolkit/Common/Batch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace AzureMapsToolkit.Common
{
public class Batch
{
[JsonPropertyName("batchItems")]
public List<BatchItem> BatchItems { get; set; }
}
}
14 changes: 14 additions & 0 deletions AzureMapsRestToolkit/AzureMapsRestToolkit/Common/BatchItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace AzureMapsToolkit.Common
{
public class BatchItem
{
[JsonPropertyName("query")]
public string Query { get; set; }
}
}
3 changes: 3 additions & 0 deletions AzureMapsRestToolkit/AzureMapsRestToolkit/Common/Error.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json.Serialization;

namespace AzureMapsToolkit
{
Expand All @@ -12,11 +13,13 @@ public class Error
/// <summary>
/// The HTTP status code.
/// </summary>
[JsonPropertyName("code")]
public string Code { get; set; }

/// <summary>
/// If available, a human readable description of the error.
/// </summary>
[JsonPropertyName("message")]
public string Message { get; set; }
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json.Serialization;

namespace AzureMapsToolkit
{
/// <summary>
/// This response object is returned when an error occurs in the Maps API
/// </summary>
/// <summary>
/// This response object is returned when an error occurs in the Maps API
/// </summary>
public class ErrorResponse
{
/// <summary>
/// This object is returned when an error occurs in the Maps API
/// </summary>
[JsonPropertyName("error")]
public Error Error { get; set; }



}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace AzureMapsToolkit.Common
{
public static class Extensions
{
public static string ToCamlCase(this string val)
public static string ToCamelCase(this string val)
{
char prevChar = val[0];

Expand Down
Loading