Skip to content

Commit

Permalink
Merge pull request #6 from kwtc/feature/project-structure-and-offer-s…
Browse files Browse the repository at this point in the history
…earch

Changed naming, extended interface and updated docs
  • Loading branch information
kwtc authored Jul 12, 2024
2 parents 60b7463 + 1d4e4ec commit e22af74
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 26 deletions.
48 changes: 46 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,48 @@
![.NET build and test](https://github.com/kwtc/tjek-client-dotnet/actions/workflows/ci.yml/badge.svg)

# Kwtc.Tjek.Client.Dotnet
Tjek.com API client for dotnet
# Tjek.com .NET API client
As the heading states this is a .NET API client for tjek.com

> **Warning**
> This is work in progress
>
> Minor and patch version bumps may break your code until version 1.0.0 is released
## Supported endpoint status
- [x] /v2/offers/search
- [ ] /v2/offers
- [ ] /v2/offers/{offerId}

## How to use
Add configuration to appsettings.json

```json
{
"TjekClientConfig": {
"ApiKey": "<API_KEY>"
}
}
```
An API key can be requested here: https://etilbudsavis.dk/developers

The client project includes an extension method that makes registering the client and services in the DI container easy.

```csharp
builder.Services.AddTjekClientServices(builder.Configuration);
```

After registering the services you can inject the client and configuration.

```csharp
public class MyService
{
private readonly ITjekClient _tjekClient;
private readonly TjekClientConfig _tjekClientConfig;

public MyService(ITjekClient tjekClient, IOptions<TjekClientConfig> tjekClientConfig)
{
_tjekClient = tjekClient;
_tjekClientConfig = tjekClientConfig.Value;
}
}
```
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Kwtc.Tjek.Client;
namespace Kwtc.Tjek.Client.Abstractions.Config;

public class ClientConfig
public class TjekClientConfig
{
public static string SectionName => "TjekClientConfig";

Expand Down
14 changes: 0 additions & 14 deletions src/Kwtc.Tjek.Client.Abstractions/IClient.cs

This file was deleted.

38 changes: 38 additions & 0 deletions src/Kwtc.Tjek.Client.Abstractions/ITjekClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Kwtc.Tjek.Client.Abstractions.Models;

namespace Kwtc.Tjek.Client.Abstractions;

public interface ITjekClient
{
/// <summary>
/// Search for offers
/// </summary>
public Task<IReadOnlyList<Offer>> Search(
string query,
string? dealerId = null,
string? catalogId = null,
string? publicationType = null,
int? limit = null,
CancellationToken cancellationToken = default);

/// <summary>
/// NOT IMPLEMENTED
///
/// Get list current offers
/// </summary>
public Task<IReadOnlyList<Offer>> List(
string? dealerId = null,
string? catalogId = null,
string? publicationType = null,
int? limit = null,
int? offset = null,
string? orderBy = null,
CancellationToken cancellationToken = default);

/// <summary>
/// NOT IMPLEMENTED
///
/// Get offer by id
/// </summary>
public Task<Offer?> Offer(string id, CancellationToken cancellationToken = default);
}
7 changes: 4 additions & 3 deletions src/Kwtc.Tjek.Client/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Kwtc.Tjek.Client.Abstractions;
using Kwtc.Tjek.Client.Abstractions.Config;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

Expand All @@ -8,13 +9,13 @@ public static class ServiceCollectionExtensions
{
public static IServiceCollection AddTjekClientServices(this IServiceCollection services, ConfigurationManager configuration)
{
var clientConfig = configuration.GetSection(ClientConfig.SectionName).Get<ClientConfig>();
var clientConfig = configuration.GetSection(TjekClientConfig.SectionName).Get<TjekClientConfig>();
if (clientConfig is null)
{
throw new InvalidOperationException("Client configuration is missing");
}

services.Configure<ClientConfig>(configuration.GetSection(ClientConfig.SectionName));
services.Configure<TjekClientConfig>(configuration.GetSection(TjekClientConfig.SectionName));

services.AddHttpClient(Constants.HttpClientName, client =>
{
Expand All @@ -23,7 +24,7 @@ public static IServiceCollection AddTjekClientServices(this IServiceCollection s
client.DefaultRequestHeaders.Add("X-Api-Key", clientConfig.ApiKey);
});

services.AddTransient<IClient, Client>();
services.AddTransient<ITjekClient, TjekClient>();

return services;
}
Expand Down
2 changes: 2 additions & 0 deletions src/Kwtc.Tjek.Client/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ public static class StringExtensions
{
public static string ToValidUri(this string input)
{
// TODO: Improve this method or find alternative

var str = input;
str = System.Text.RegularExpressions.Regex.Replace(str, @"[^a-zA-Z0-9\s-æøåÆØÅ?&=]", "");
str = System.Text.RegularExpressions.Regex.Replace(str, @"\s+", " ").Trim();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

namespace Kwtc.Tjek.Client;

public class Client : IClient
public class TjekClient : ITjekClient
{
private readonly IHttpClientFactory httpClientFactory;

public Client(IHttpClientFactory httpClientFactory)
public TjekClient(IHttpClientFactory httpClientFactory)
{
this.httpClientFactory = httpClientFactory;
}
Expand Down Expand Up @@ -53,6 +53,22 @@ public async Task<IReadOnlyList<Offer>> Search(
return result ?? [];
}

/// <summary>
/// NOT IMPLEMENTED
/// </summary>
public Task<IReadOnlyList<Offer>> List(string? dealerId = null, string? catalogId = null, string? publicationType = null, int? limit = null, int? offset = null, string? orderBy = null, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}

/// <summary>
/// NOT IMPLEMENTED
/// </summary>
public Task<Offer?> Offer(string id, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}

private static string BuildQueryString(IDictionary<string, string> parameters, StringBuilder builder)
{
foreach (var parameter in parameters)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Kwtc.Tjek.Client.Tests.UnitTests;

public class ClientTests
public class TjekClientTests
{
private readonly Mock<IHttpClientFactory> httpClientFactoryMock = new();

Expand Down Expand Up @@ -263,8 +263,8 @@ private static HttpClient GetMockedClient(string uri, HttpStatusCode statusCode
return httpClient;
}

private Client GetSut()
private TjekClient GetSut()
{
return new Client(this.httpClientFactoryMock.Object);
return new TjekClient(this.httpClientFactoryMock.Object);
}
}

0 comments on commit e22af74

Please sign in to comment.