-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
MapsGeolocationClient.cs
181 lines (161 loc) · 10.6 KB
/
MapsGeolocationClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#nullable disable
using System;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Azure;
using Azure.Core;
using Azure.Core.Pipeline;
using Azure.Maps.Geolocation;
namespace Azure.Maps.Geolocation
{
/// <summary> The Geolocation service client. </summary>
public partial class MapsGeolocationClient
{
/// <summary> The HTTP pipeline for sending and receiving REST requests and responses. </summary>
private readonly HttpPipeline _pipeline;
/// <summary> The restClient is used to access Geolocation REST client. </summary>
internal GeolocationRestClient RestClient { get; }
/// <summary> The ClientDiagnostics is used to provide tracing support for the client library. </summary>
internal ClientDiagnostics _clientDiagnostics { get; }
/// <summary> Initializes a new instance of GeolocationClient. </summary>
protected MapsGeolocationClient()
{
_clientDiagnostics = null;
_pipeline = null;
RestClient = null;
}
/// <summary> Initializes a new instance of MapsGeolocationClient. </summary>
/// <param name="credential"> Shared key credential used to authenticate to an Azure Maps Geolocation Service. </param>
/// <exception cref="ArgumentNullException"> <paramref name="credential"/> is null. </exception>
public MapsGeolocationClient(AzureKeyCredential credential)
{
Argument.AssertNotNull(credential, nameof(credential));
var endpoint = new Uri("https://atlas.microsoft.com");
var options = new MapsGeolocationClientOptions();
_clientDiagnostics = new ClientDiagnostics(options);
_pipeline = HttpPipelineBuilder.Build(options, new AzureKeyCredentialPolicy(credential, "subscription-key"));
RestClient = new GeolocationRestClient(_clientDiagnostics, _pipeline, endpoint, null, options.Version);
}
/// <summary> Initializes a new instance of MapsGeolocationClient. </summary>
/// <param name="credential"> Shared key credential used to authenticate to an Azure Maps Geolocation Service. </param>
/// <param name="options"> The options for configuring the client. </param>
public MapsGeolocationClient(AzureKeyCredential credential, MapsGeolocationClientOptions options)
{
Argument.AssertNotNull(credential, nameof(credential));
var endpoint = options.Endpoint;
options ??= new MapsGeolocationClientOptions();
_clientDiagnostics = new ClientDiagnostics(options);
_pipeline = HttpPipelineBuilder.Build(options, new AzureKeyCredentialPolicy(credential, "subscription-key"));
RestClient = new GeolocationRestClient(_clientDiagnostics, _pipeline, endpoint, null, options.Version);
}
/// <summary> Initializes a new instance of MapsGeolocationClient. </summary>
/// <param name="credential"> A credential used to authenticate to an Azure Maps Geolocation Service. </param>
/// <param name="clientId"> Specifies which account is intended for usage in conjunction with the Azure AD security model. It represents a unique ID for the Azure Maps account and can be retrieved from the Azure Maps management plane Account API. To use Azure AD security in Azure Maps see the following <see href="https://aka.ms/amauthdetails">articles</see> for guidance. </param>
/// <exception cref="ArgumentNullException"> <paramref name="credential"/> or <paramref name="clientId"/> is null. </exception>
public MapsGeolocationClient(TokenCredential credential, string clientId)
{
Argument.AssertNotNull(credential, nameof(credential));
var endpoint = new Uri("https://atlas.microsoft.com");
var options = new MapsGeolocationClientOptions();
_clientDiagnostics = new ClientDiagnostics(options);
string[] scopes = { "https://atlas.microsoft.com/.default" };
_pipeline = HttpPipelineBuilder.Build(options, new BearerTokenAuthenticationPolicy(credential, scopes), new AzureKeyCredentialPolicy(new AzureKeyCredential(clientId), "x-ms-client-id"));
RestClient = new GeolocationRestClient(_clientDiagnostics, _pipeline, endpoint, clientId, options.Version);
}
/// <summary> Initializes a new instance of MapsGeolocationClient. </summary>
/// <param name="credential"> A credential used to authenticate to an Azure Maps Geolocation Service. </param>
/// <param name="clientId"> Specifies which account is intended for usage in conjunction with the Azure AD security model. It represents a unique ID for the Azure Maps account and can be retrieved from the Azure Maps management plane Account API. To use Azure AD security in Azure Maps see the following <see href="https://aka.ms/amauthdetails">articles</see> for guidance. </param>
/// <param name="options"> The options for configuring the client. </param>
/// <exception cref="ArgumentNullException"> <paramref name="credential"/> or <paramref name="clientId"/> is null. </exception>
public MapsGeolocationClient(TokenCredential credential, string clientId, MapsGeolocationClientOptions options)
{
Argument.AssertNotNull(credential, nameof(credential));
var endpoint = new Uri("https://atlas.microsoft.com");
options ??= new MapsGeolocationClientOptions();
_clientDiagnostics = new ClientDiagnostics(options);
string[] scopes = { "https://atlas.microsoft.com/.default" };
_pipeline = HttpPipelineBuilder.Build(options, new BearerTokenAuthenticationPolicy(credential, scopes), new AzureKeyCredentialPolicy(new AzureKeyCredential(clientId), "x-ms-client-id"));
RestClient = new GeolocationRestClient(_clientDiagnostics, _pipeline, endpoint, clientId, options.Version);
}
/// <summary> Initializes a new instance of MapsGeolocationClient. </summary>
/// <param name="credential"> The Shared Access Signature credential used to connect to Azure. This signature
/// can be constructed using the <see cref="AzureSasCredential"/>.</param>
public MapsGeolocationClient(AzureSasCredential credential)
{
Argument.AssertNotNull(credential, nameof(credential));
var endpoint = new Uri("https://atlas.microsoft.com");
var options = new MapsGeolocationClientOptions();
_clientDiagnostics = new ClientDiagnostics(options);
_pipeline = HttpPipelineBuilder.Build(options, new MapsSasCredentialPolicy(credential));
RestClient = new GeolocationRestClient(_clientDiagnostics, _pipeline, endpoint, null, options.Version);
}
/// <summary> Initializes a new instance of MapsGeolocationClient. </summary>
/// <param name="credential"> The Shared Access Signature credential used to connect to Azure. This signature
/// can be constructed using the <see cref="AzureSasCredential"/>.</param>
/// <param name="options"> The options for configuring the client. </param>
public MapsGeolocationClient(AzureSasCredential credential, MapsGeolocationClientOptions options)
{
Argument.AssertNotNull(credential, nameof(credential));
var endpoint = options.Endpoint;
options ??= new MapsGeolocationClientOptions();
_clientDiagnostics = new ClientDiagnostics(options);
_pipeline = HttpPipelineBuilder.Build(options, new MapsSasCredentialPolicy(credential));
RestClient = new GeolocationRestClient(_clientDiagnostics, _pipeline, endpoint, null, options.Version);
}
/// <summary>
/// This service will return the ISO country code for the provided IP address. Developers can use this information to block or alter certain content based on geographical locations where the application is being viewed from.
/// </summary>
/// <param name="ipAddress"> The IP address. Both IPv4 and IPv6 are allowed. </param>
/// <param name="cancellationToken"> The cancellation token to use. </param>
/// <exception cref="ArgumentException"> is an empty string, and was expected to be non-empty. </exception>
/// <exception cref="RequestFailedException"> Service returned a non-success status code. </exception>
/// <returns> The response returned from the service. Details of the response body schema are in the Remarks section below. </returns>
public virtual async Task<Response<CountryRegionResult>> GetCountryCodeAsync(IPAddress ipAddress, CancellationToken cancellationToken = default)
{
using var scope = _clientDiagnostics.CreateScope("MapsGeolocationClient.GetCountryCode");
scope.Start();
try
{
return await RestClient.GetLocationAsync(
JsonFormat.Json,
ipAddress.ToString(),
cancellationToken
).ConfigureAwait(false);
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
/// <summary>
/// This service will return the ISO country code for the provided IP address. Developers can use this information to block or alter certain content based on geographical locations where the application is being viewed from.
/// </summary>
/// <param name="ipAddress"> The IP address. Both IPv4 and IPv6 are allowed. </param>
/// <param name="cancellationToken"> The cancellation token to use. </param>
/// <exception cref="ArgumentException"> is an empty string, and was expected to be non-empty. </exception>
/// <exception cref="RequestFailedException"> Service returned a non-success status code. </exception>
/// <returns> The response returned from the service. Details of the response body schema are in the Remarks section below. </returns>
public virtual Response<CountryRegionResult> GetCountryCode(IPAddress ipAddress, CancellationToken cancellationToken = default)
{
using var scope = _clientDiagnostics.CreateScope("MapsGeolocationClient.GetCountryCode");
scope.Start();
try
{
return RestClient.GetLocation(
JsonFormat.Json,
ipAddress.ToString(),
cancellationToken
);
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
}
}