Skip to content

Commit

Permalink
feat(configuration): updating to allow services to pass a key when se…
Browse files Browse the repository at this point in the history
…nding requests and changing how option information is configured and stored (#98)

BREAKING CHANGE: removed all key container types in favour of using `IOptions<T>`, changed how configuration is done for options during service configuration, changed the namespace of multiple classes/extension methods, changed the name of the extension methods to add the geocoding services
  • Loading branch information
JustinCanton authored Jan 28, 2024
1 parent 86adae2 commit 659288f
Show file tree
Hide file tree
Showing 114 changed files with 1,677 additions and 1,648 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ All notable changes to this project will be documented in this file. See [Conve
- removed native support for net5.0 since it is an out of support item, and dropped netstandard2.1 since this supports netstandard2.0
- removed the usage of Newtonsoft.Json and moved to use System.Text.Json
- changed the exceptions returned from all services to GeoNETException instead of individual exceptions per API, as well as removed some now-deprecated interface types and implementations
- removed all key container types in favour of using `IOptions<T>`
- changed how configuration is done for options during service configuration
- changed the namespace of multiple classes/extension methods
- changed the name of the extension methods to add the geocoding services

### Features
- **runtime**: updating the .net version support for net8.0, and removing native support for netstandard2.1 and net5.0 ([#58](https://github.com/JustinCanton/Geo.NET/issues/58)) ([4398a10](https://github.com/JustinCanton/Geo.NET/commit/4398a10afb21d3e7e86fba0fa4052adb67ca1faa))
- **serialization**: updating to use System.Text.Json instead of Newtonsoft.Json ([#40](https://github.com/JustinCanton/Geo.NET/issues/40)) ([e108ec6](https://github.com/JustinCanton/Geo.NET/commit/e108ec65d895d8d7fa792845973f14cdcc7335ae))
- **exceptions**: updating how exceptions are handled and removing unused interfaces ([#95](https://github.com/JustinCanton/Geo.NET/issues/95)) ([61a3e5f](https://github.com/JustinCanton/Geo.NET/commit/61a3e5f1b8bb2bd74cd7b11e92c91bcaf1e691ac))
- **configuration**: updating to allow services to pass a key when sending requests and changing how option information is configured and stored ([#80](https://github.com/JustinCanton/Geo.NET/issues/80)) ([61a3e5f](https://github.com/JustinCanton/Geo.NET/commit/61a3e5f1b8bb2bd74cd7b11e92c91bcaf1e691ac))

## [1.6.0](https://github.com/JustinCanton/Geo.NET/compare/1.5.2...1.6.0) (2023-12-29)
### Features
Expand Down
21 changes: 0 additions & 21 deletions src/Geo.ArcGIS/Abstractions/IArcGISCredentialsContainer.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Geo.ArcGIS/Abstractions/IArcGISGeocoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Licensed under the MIT license. See the LICENSE file in the solution root for full license information.
// </copyright>

namespace Geo.ArcGIS.Abstractions
namespace Geo.ArcGIS
{
using System.Threading;
using System.Threading.Tasks;
Expand Down
23 changes: 0 additions & 23 deletions src/Geo.ArcGIS/Abstractions/IArcGISTokenContainer.cs

This file was deleted.

25 changes: 25 additions & 0 deletions src/Geo.ArcGIS/Abstractions/IArcGISTokenProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// <copyright file="IArcGISTokenProvider.cs" company="Geo.NET">
// Copyright (c) Geo.NET.
// Licensed under the MIT license. See the LICENSE file in the solution root for full license information.
// </copyright>

namespace Geo.ArcGIS
{
using System.Threading;
using System.Threading.Tasks;

/// <summary>
/// A class for providing an ArcGIS API token.
/// </summary>
public interface IArcGISTokenProvider
{
/// <summary>
/// Gets a token for the ArcGIS API using the provided client id and client secret.
/// </summary>
/// <param name="clientId">The client id to use to fetch the token.</param>
/// <param name="clientSecret">The client secret to use to fetch the token.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> used to cancel the request.</param>
/// <returns>A <see cref="string"/> with the token for accessing the ArcGIS API, or null if a token could not be retrieved.</returns>
Task<string> GetTokenAsync(string clientId, string clientSecret, CancellationToken cancellationToken);
}
}
24 changes: 0 additions & 24 deletions src/Geo.ArcGIS/Abstractions/IArcGISTokenRetrevial.cs

This file was deleted.

43 changes: 14 additions & 29 deletions src/Geo.ArcGIS/DependencyInjection/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,45 @@
// Licensed under the MIT license. See the LICENSE file in the solution root for full license information.
// </copyright>

namespace Geo.ArcGIS.DependencyInjection
namespace Geo.Extensions.DependencyInjection
{
using System;
using System.Net.Http;
using Geo.ArcGIS.Abstractions;
using Geo.ArcGIS.Models;
using Geo.ArcGIS;
using Geo.ArcGIS.Services;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

/// <summary>
/// Extension methods for the <see cref="IServiceCollection"/> class.
/// </summary>
public static class ServiceCollectionExtensions
{
/// <summary>
/// Adds the ArcGIS services to the service collection.
/// Adds the ArcGIS geocoding services to the service collection.
/// <para>
/// Adds the services:
/// <list type="bullet">
/// <item><see cref="IOptions{TOptions}"/> of <see cref="IArcGISGeocoding"/></item>
/// <item><see cref="IArcGISTokenProvider"/></item>
/// <item><see cref="IArcGISGeocoding"/></item>
/// </list>
/// </para>
/// </summary>
/// <param name="services">An <see cref="IServiceCollection"/> to add the ArcGIS services to.</param>
/// <param name="optionsBuilder">A <see cref="Action{ArcGISOptionsBuilder}"/> with the options to add to the ArcGIS configuration.</param>
/// <param name="configureClient">Optional. A delegate that is used to configure the <see cref="HttpClient"/>.</param>
/// <returns>An <see cref="IHttpClientBuilder"/> to configure the http client.</returns>
/// <returns>An <see cref="ClientCredentialsBuilder{T}"/> to configure the ArcGIS geocoding.</returns>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="services"/> is null.</exception>
public static IHttpClientBuilder AddArcGISServices(
this IServiceCollection services,
Action<ArcGISOptionsBuilder> optionsBuilder,
Action<HttpClient> configureClient = null)
public static ClientCredentialsBuilder<IArcGISGeocoding> AddArcGISGeocoding(this IServiceCollection services)
{
if (optionsBuilder != null)
if (services == null)
{
var options = new ArcGISOptionsBuilder();
optionsBuilder(options);

services.AddSingleton<IArcGISCredentialsContainer>(new ArcGISCredentialsContainer(options.ClientId, options.ClientSecret));
}
else
{
services.AddSingleton<IArcGISCredentialsContainer>(new ArcGISCredentialsContainer(string.Empty, string.Empty));
throw new ArgumentNullException(nameof(services));
}

services.AddHttpClient<IArcGISTokenRetrevial, ArcGISTokenRetrevial>();
services.AddSingleton<IArcGISTokenContainer, ArcGISTokenContainer>();
services
.AddClientCredentialsOptions<IArcGISGeocoding>()
.AddHttpClient<IArcGISTokenProvider, ArcGISTokenProvider>();

return services.AddHttpClient<IArcGISGeocoding, ArcGISGeocoding>(configureClient ?? DefaultHttpClientConfiguration);
}

private static void DefaultHttpClientConfiguration(HttpClient client)
{
// No-op
return new ClientCredentialsBuilder<IArcGISGeocoding>(services.AddHttpClient<IArcGISGeocoding, ArcGISGeocoding>());
}
}
}
52 changes: 0 additions & 52 deletions src/Geo.ArcGIS/Models/ArcGISOptionsBuilder.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Geo.ArcGIS.Models.Parameters
/// <summary>
/// A parameters object for the address candidates ArcGIS request.
/// </summary>
public class AddressCandidateParameters : StorageParameters
public class AddressCandidateParameters : StorageParameters, IClientCredentialsParameters
{
/// <summary>
/// Gets or sets the address you want to geocode as a single line of text.
Expand All @@ -19,5 +19,11 @@ public class AddressCandidateParameters : StorageParameters
/// Gets or sets an ID attribute value that, along with the text attribute, links a suggestion to an address or place.
/// </summary>
public string MagicKey { get; set; }

/// <inheritdoc/>
public string ClientId { get; set; }

/// <inheritdoc/>
public string ClientSecret { get; set; }
}
}
8 changes: 7 additions & 1 deletion src/Geo.ArcGIS/Models/Parameters/GeocodingParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Geo.ArcGIS.Models.Parameters
/// <summary>
/// A parameters object for the geocoding ArcGIS request.
/// </summary>
public class GeocodingParameters
public class GeocodingParameters : IClientCredentialsParameters
{
/// <summary>
/// Gets a list of address attributes.
Expand Down Expand Up @@ -73,5 +73,11 @@ public class GeocodingParameters
/// The default value is postal city.
/// </summary>
public PreferredLabelValue PreferredLabelValue { get; set; } = PreferredLabelValue.PostalCity;

/// <inheritdoc/>
public string ClientId { get; set; }

/// <inheritdoc/>
public string ClientSecret { get; set; }
}
}
8 changes: 7 additions & 1 deletion src/Geo.ArcGIS/Models/Parameters/PlaceCandidateParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Geo.ArcGIS.Models.Parameters
/// <summary>
/// A parameters object for the place candidates ArcGIS request.
/// </summary>
public class PlaceCandidateParameters : StorageParameters
public class PlaceCandidateParameters : StorageParameters, IClientCredentialsParameters
{
/// <summary>
/// Gets or sets the address of the location.
Expand Down Expand Up @@ -111,5 +111,11 @@ public class PlaceCandidateParameters : StorageParameters
/// The default value is rooftop.
/// </summary>
public LocationType LocationType { get; set; } = LocationType.Rooftop;

/// <inheritdoc/>
public string ClientId { get; set; }

/// <inheritdoc/>
public string ClientSecret { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Geo.ArcGIS.Models.Parameters
/// <summary>
/// A parameters object for the reverse geocoding ArcGIS request.
/// </summary>
public class ReverseGeocodingParameters : StorageParameters
public class ReverseGeocodingParameters : StorageParameters, IClientCredentialsParameters
{
/// <summary>
/// Gets or sets the point from which to search for the closest address.
Expand Down Expand Up @@ -53,5 +53,11 @@ public class ReverseGeocodingParameters : StorageParameters
/// The default value is postal city.
/// </summary>
public PreferredLabelValue PreferredLabelValue { get; set; } = PreferredLabelValue.PostalCity;

/// <inheritdoc/>
public string ClientId { get; set; }

/// <inheritdoc/>
public string ClientSecret { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/Geo.ArcGIS/Models/Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Geo.ArcGIS.Models
/// <summary>
/// A token information class for the ArcGIS API.
/// </summary>
public class Token
internal class Token
{
/// <summary>
/// Gets or sets the access token for the ArcGIS API.
Expand Down
8 changes: 5 additions & 3 deletions src/Geo.ArcGIS/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ This allows the simple calling of ArcGIS geocoding APIs. The supported ArcGIS ge

In the startup `ConfigureServices` method, add the configuration for the ArcGIS service:
```
using Geo.ArcGIS.DependencyInjection;
using Geo.Extensions.DependencyInjection;
.
.
.
Expand All @@ -20,7 +20,9 @@ public void ConfigureServices(IServiceCollection services)
.
.
.
services.AddArcGISServices(options => options.UseClientCredentials(your_arcgis_client_id_here, your_arcgis_client_secret_here));
var builder = services.AddArcGISGeocoding();
builder.AddClientCredentials(your_arcgis_client_id_here, your_arcgis_client_secret_here);
builder.HttpClientBuilder.ConfigureHttpClient(configure_client);
.
.
.
Expand All @@ -45,7 +47,7 @@ The endpoints where it is not required at all:

## Sample Usage

By calling `AddArcGISServices`, the `IArcGISGeocoding` interface has been added to the IOC container. Just request it as a DI item:
By calling `AddArcGISGeocoding`, the `IArcGISGeocoding` interface has been added to the IOC container. Just request it as a DI item:
```
public MyService(IArcGISGeocoding arcgisGeocoding)
{
Expand Down
Loading

0 comments on commit 659288f

Please sign in to comment.