Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added user agent header #286

Merged
merged 5 commits into from
Oct 12, 2024
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
5 changes: 3 additions & 2 deletions src/PinguApps.Appwrite.Client/Internals/IBaseApi.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using Refit;
using PinguApps.Appwrite.Shared;
using Refit;

namespace PinguApps.Appwrite.Client.Internals;

[Headers("content-type: application/json",
"x-sdk-name: .NET",
"x-sdk-platform: client",
"x-sdk-language: dotnet",
"x-sdk-version: 0.0.1",
$"x-sdk-version: {Constants.Version}",
"X-Appwrite-Response-Format: 1.6.0")]
internal interface IBaseApi
{
Expand Down
34 changes: 25 additions & 9 deletions src/PinguApps.Appwrite.Client/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Net.Http;
using System.Runtime.InteropServices;
using Microsoft.Extensions.DependencyInjection;
using PinguApps.Appwrite.Client.Handlers;
using PinguApps.Appwrite.Client.Internals;
Expand Down Expand Up @@ -28,7 +29,7 @@ public static IServiceCollection AddAppwriteClient(this IServiceCollection servi
services.AddTransient<ClientCookieSessionHandler>();

services.AddRefitClient<IAccountApi>(refitSettings)
.ConfigureHttpClient(x => x.BaseAddress = new Uri(endpoint))
.ConfigureHttpClient(x => ConfigureHttpClient(x, endpoint))
.AddHttpMessageHandler<HeaderHandler>()
.AddHttpMessageHandler<ClientCookieSessionHandler>();

Expand All @@ -53,19 +54,34 @@ public static IServiceCollection AddAppwriteClientForServer(this IServiceCollect
services.AddTransient<HeaderHandler>();

services.AddRefitClient<IAccountApi>(refitSettings)
.ConfigureHttpClient(x => x.BaseAddress = new Uri(endpoint))
.ConfigureHttpClient(x => ConfigureHttpClient(x, endpoint))
.AddHttpMessageHandler<HeaderHandler>()
.ConfigurePrimaryHttpMessageHandler((handler, sp) =>
{
if (handler is HttpClientHandler clientHandler)
{
clientHandler.UseCookies = false;
}
});
.ConfigurePrimaryHttpMessageHandler(ConfigurePrimaryHttpMessageHandler);

services.AddSingleton<IAccountClient, AccountClient>();
services.AddSingleton<IAppwriteClient, AppwriteClient>();

return services;
}

private static void ConfigurePrimaryHttpMessageHandler(HttpMessageHandler messageHandler, IServiceProvider serviceProvider)
{
if (messageHandler is HttpClientHandler clientHandler)
{
clientHandler.UseCookies = false;
}
}

private static void ConfigureHttpClient(HttpClient client, string endpoint)
{
client.BaseAddress = new Uri(endpoint);
client.DefaultRequestHeaders.UserAgent.ParseAdd(BuildUserAgent());
}

public static string BuildUserAgent()
{
var dotnetVersion = RuntimeInformation.FrameworkDescription.Replace("Microsoft .NET", ".NET").Trim();

return $"PinguAppsAppwriteDotNetClientSdk/{Constants.Version} (.NET/{dotnetVersion}; {RuntimeInformation.OSDescription.Trim()})";
}
}
8 changes: 4 additions & 4 deletions src/PinguApps.Appwrite.Playground/App.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Microsoft.Extensions.Configuration;
using PinguApps.Appwrite.Shared.Requests.Users;
using PinguApps.Appwrite.Shared.Requests.Account;

namespace PinguApps.Appwrite.Playground;
internal class App
Expand All @@ -17,13 +17,13 @@ public App(Client.IAppwriteClient client, Server.Clients.IAppwriteClient server,

public async Task Run(string[] args)
{
var request = new UpdatePhoneVerificationRequest()
var request = new CreateSessionRequest()
{
UserId = "664aac1a00113f82e620",
PhoneVerification = true
Secret = "80af6605407a3918cd9bb1796b6bfdc5d4b2dc57dad4677432d902e8bef9ba6f"
};

var response = await _server.Users.UpdatePhoneVerification(request);
var response = await _client.Account.CreateSession(request);

Console.WriteLine(response.Result.Match(
result => result.ToString(),
Expand Down
5 changes: 3 additions & 2 deletions src/PinguApps.Appwrite.Server/Internals/IBaseApi.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using Refit;
using PinguApps.Appwrite.Shared;
using Refit;

namespace PinguApps.Appwrite.Server.Internals;

[Headers("content-type: application/json",
"x-sdk-name: .NET",
"x-sdk-platform: server",
"x-sdk-language: dotnet",
"x-sdk-version: 0.0.1",
$"x-sdk-version: {Constants.Version}",
"X-Appwrite-Response-Format: 1.6.0")]
internal interface IBaseApi
{
Expand Down
42 changes: 26 additions & 16 deletions src/PinguApps.Appwrite.Server/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Net.Http;
using System.Runtime.InteropServices;
using Microsoft.Extensions.DependencyInjection;
using PinguApps.Appwrite.Server.Clients;
using PinguApps.Appwrite.Server.Handlers;
Expand Down Expand Up @@ -29,31 +30,40 @@ public static IServiceCollection AddAppwriteServer(this IServiceCollection servi
services.AddTransient<HeaderHandler>();

services.AddRefitClient<IAccountApi>(refitSettings)
.ConfigureHttpClient(x => x.BaseAddress = new Uri(endpoint))
.ConfigureHttpClient(x => ConfigureHttpClient(x, endpoint))
.AddHttpMessageHandler<HeaderHandler>()
.ConfigurePrimaryHttpMessageHandler((handler, sp) =>
{
if (handler is HttpClientHandler clientHandler)
{
clientHandler.UseCookies = false;
}
});
.ConfigurePrimaryHttpMessageHandler(ConfigurePrimaryHttpMessageHandler);

services.AddRefitClient<IUsersApi>(refitSettings)
.ConfigureHttpClient(x => x.BaseAddress = new Uri(endpoint))
.ConfigureHttpClient(x => ConfigureHttpClient(x, endpoint))
.AddHttpMessageHandler<HeaderHandler>()
.ConfigurePrimaryHttpMessageHandler((handler, sp) =>
{
if (handler is HttpClientHandler clientHandler)
{
clientHandler.UseCookies = false;
}
});
.ConfigurePrimaryHttpMessageHandler(ConfigurePrimaryHttpMessageHandler);

services.AddSingleton<IAccountClient, AccountClient>();
services.AddSingleton<IUsersClient, UsersClient>();
services.AddSingleton<IAppwriteClient, AppwriteClient>();

return services;
}

private static void ConfigurePrimaryHttpMessageHandler(HttpMessageHandler messageHandler, IServiceProvider serviceProvider)
{
if (messageHandler is HttpClientHandler clientHandler)
{
clientHandler.UseCookies = false;
}
}

private static void ConfigureHttpClient(HttpClient client, string endpoint)
{
client.BaseAddress = new Uri(endpoint);
client.DefaultRequestHeaders.UserAgent.ParseAdd(BuildUserAgent());
}

public static string BuildUserAgent()
{
var dotnetVersion = RuntimeInformation.FrameworkDescription.Replace("Microsoft .NET", ".NET").Trim();

return $"PinguAppsAppwriteDotNetServerSdk/{Constants.Version} (.NET/{dotnetVersion}; {RuntimeInformation.OSDescription.Trim()})";
}
}
5 changes: 5 additions & 0 deletions src/PinguApps.Appwrite.Shared/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace PinguApps.Appwrite.Shared;
public static class Constants
{
public const string Version = "0.2.0";
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ public async Task AddAuthenticator_ShouldReturnSuccess_WhenApiCallSucceeds()
// Arrange
var request = new AddAuthenticatorRequest();

_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account/mfa/authenticators/totp")
_mockHttp.Expect(HttpMethod.Post, $"{TestConstants.Endpoint}/account/mfa/authenticators/totp")
.ExpectedHeaders(true)
.Respond(Constants.AppJson, Constants.MfaTypeResponse);
.Respond(TestConstants.AppJson, TestConstants.MfaTypeResponse);

_appwriteClient.SetSession(Constants.Session);
_appwriteClient.SetSession(TestConstants.Session);

// Act
var result = await _appwriteClient.Account.AddAuthenticator(request);
Expand All @@ -35,12 +35,12 @@ public async Task AddAuthenticator_ShouldHitDifferentEndpoint_WhenNewTypeIsUsed(
{
Type = type
};
var requestUri = $"{Constants.Endpoint}/account/mfa/authenticators/{type}";
var requestUri = $"{TestConstants.Endpoint}/account/mfa/authenticators/{type}";
var mockRequest = _mockHttp.Expect(HttpMethod.Post, requestUri)
.ExpectedHeaders(true)
.Respond(Constants.AppJson, Constants.MfaTypeResponse);
.Respond(TestConstants.AppJson, TestConstants.MfaTypeResponse);

_appwriteClient.SetSession(Constants.Session);
_appwriteClient.SetSession(TestConstants.Session);

// Act
var result = await _appwriteClient.Account.AddAuthenticator(request);
Expand Down Expand Up @@ -72,11 +72,11 @@ public async Task AddAuthenticator_ShouldHandleException_WhenApiCallFails()
// Arrange
var request = new AddAuthenticatorRequest();

_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account/mfa/authenticators/totp")
_mockHttp.Expect(HttpMethod.Post, $"{TestConstants.Endpoint}/account/mfa/authenticators/totp")
.ExpectedHeaders(true)
.Respond(HttpStatusCode.BadRequest, Constants.AppJson, Constants.AppwriteError);
.Respond(HttpStatusCode.BadRequest, TestConstants.AppJson, TestConstants.AppwriteError);

_appwriteClient.SetSession(Constants.Session);
_appwriteClient.SetSession(TestConstants.Session);

// Act
var result = await _appwriteClient.Account.AddAuthenticator(request);
Expand All @@ -92,11 +92,11 @@ public async Task AddAuthenticator_ShouldReturnErrorResponse_WhenExceptionOccurs
// Arrange
var request = new AddAuthenticatorRequest();

_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account/mfa/authenticators/totp")
_mockHttp.Expect(HttpMethod.Post, $"{TestConstants.Endpoint}/account/mfa/authenticators/totp")
.ExpectedHeaders(true)
.Throw(new HttpRequestException("An error occurred"));

_appwriteClient.SetSession(Constants.Session);
_appwriteClient.SetSession(TestConstants.Session);

// Act
var result = await _appwriteClient.Account.AddAuthenticator(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public async Task Create_ShouldReturnSuccess_WhenApiCallSucceeds()
Name = "name"
};

_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account")
_mockHttp.Expect(HttpMethod.Post, $"{TestConstants.Endpoint}/account")
.ExpectedHeaders()
.WithJsonContent(request)
.Respond(Constants.AppJson, Constants.UserResponse);
.Respond(TestConstants.AppJson, TestConstants.UserResponse);

// Act
var result = await _appwriteClient.Account.Create(request);
Expand All @@ -40,10 +40,10 @@ public async Task Create_ShouldHandleException_WhenApiCallFails()
Name = "name"
};

_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account")
_mockHttp.Expect(HttpMethod.Post, $"{TestConstants.Endpoint}/account")
.ExpectedHeaders()
.WithJsonContent(request)
.Respond(HttpStatusCode.BadRequest, Constants.AppJson, Constants.AppwriteError);
.Respond(HttpStatusCode.BadRequest, TestConstants.AppJson, TestConstants.AppwriteError);

// Act
var result = await _appwriteClient.Account.Create(request);
Expand All @@ -64,7 +64,7 @@ public async Task Create_ShouldReturnErrorResponse_WhenExceptionOccurs()
Name = "name"
};

_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account")
_mockHttp.Expect(HttpMethod.Post, $"{TestConstants.Endpoint}/account")
.ExpectedHeaders()
.WithJsonContent(request)
.Throw(new HttpRequestException("An error occurred"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ public async Task Create2faChallenge_ShouldReturnSuccess_WhenApiCallSucceeds()
var options = new JsonSerializerOptions();
options.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase, false));

_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account/mfa/challenge")
_mockHttp.Expect(HttpMethod.Post, $"{TestConstants.Endpoint}/account/mfa/challenge")
.ExpectedHeaders(true)
.WithJsonContent(request, options)
.Respond(Constants.AppJson, Constants.MfaTypeResponse);
.Respond(TestConstants.AppJson, TestConstants.MfaTypeResponse);

_appwriteClient.SetSession(Constants.Session);
_appwriteClient.SetSession(TestConstants.Session);

// Act
var result = await _appwriteClient.Account.Create2faChallenge(request);
Expand Down Expand Up @@ -56,12 +56,12 @@ public async Task Create2faChallenge_ShouldHandleException_WhenApiCallFails()
var options = new JsonSerializerOptions();
options.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase, false));

_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account/mfa/challenge")
_mockHttp.Expect(HttpMethod.Post, $"{TestConstants.Endpoint}/account/mfa/challenge")
.ExpectedHeaders(true)
.WithJsonContent(request, options)
.Respond(HttpStatusCode.BadRequest, Constants.AppJson, Constants.AppwriteError);
.Respond(HttpStatusCode.BadRequest, TestConstants.AppJson, TestConstants.AppwriteError);

_appwriteClient.SetSession(Constants.Session);
_appwriteClient.SetSession(TestConstants.Session);

// Act
var result = await _appwriteClient.Account.Create2faChallenge(request);
Expand All @@ -80,12 +80,12 @@ public async Task Create2faChallenge_ShouldReturnErrorResponse_WhenExceptionOccu
var options = new JsonSerializerOptions();
options.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase, false));

_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account/mfa/challenge")
_mockHttp.Expect(HttpMethod.Post, $"{TestConstants.Endpoint}/account/mfa/challenge")
.ExpectedHeaders(true)
.WithJsonContent(request, options)
.Throw(new HttpRequestException("An error occurred"));

_appwriteClient.SetSession(Constants.Session);
_appwriteClient.SetSession(TestConstants.Session);

// Act
var result = await _appwriteClient.Account.Create2faChallenge(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ public async Task Create2faChallengeConfirmation_ShouldReturnSuccess_WhenApiCall
Otp = "123456"
};

_mockHttp.Expect(HttpMethod.Put, $"{Constants.Endpoint}/account/mfa/challenge")
_mockHttp.Expect(HttpMethod.Put, $"{TestConstants.Endpoint}/account/mfa/challenge")
.ExpectedHeaders(true)
.WithJsonContent(request)
.Respond(HttpStatusCode.NoContent);

_appwriteClient.SetSession(Constants.Session);
_appwriteClient.SetSession(TestConstants.Session);

// Act
var result = await _appwriteClient.Account.Create2faChallengeConfirmation(request);
Expand Down Expand Up @@ -61,12 +61,12 @@ public async Task Create2faChallengeConfirmation_ShouldHandleException_WhenApiCa
Otp = "123456"
};

_mockHttp.Expect(HttpMethod.Put, $"{Constants.Endpoint}/account/mfa/challenge")
_mockHttp.Expect(HttpMethod.Put, $"{TestConstants.Endpoint}/account/mfa/challenge")
.ExpectedHeaders(true)
.WithJsonContent(request)
.Respond(HttpStatusCode.BadRequest, Constants.AppJson, Constants.AppwriteError);
.Respond(HttpStatusCode.BadRequest, TestConstants.AppJson, TestConstants.AppwriteError);

_appwriteClient.SetSession(Constants.Session);
_appwriteClient.SetSession(TestConstants.Session);

// Act
var result = await _appwriteClient.Account.Create2faChallengeConfirmation(request);
Expand All @@ -86,12 +86,12 @@ public async Task Create2faChallengeConfirmation_ShouldReturnErrorResponse_WhenE
Otp = "123456"
};

_mockHttp.Expect(HttpMethod.Put, $"{Constants.Endpoint}/account/mfa/challenge")
_mockHttp.Expect(HttpMethod.Put, $"{TestConstants.Endpoint}/account/mfa/challenge")
.ExpectedHeaders(true)
.WithJsonContent(request)
.Throw(new HttpRequestException("An error occurred"));

_appwriteClient.SetSession(Constants.Session);
_appwriteClient.SetSession(TestConstants.Session);

// Act
var result = await _appwriteClient.Account.Create2faChallengeConfirmation(request);
Expand Down
Loading