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
4 changes: 4 additions & 0 deletions src/Axios.Net.Extensions.DependencyInjection/NamespaceDoc.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace AxiosNet.Extensions.DependencyInjection;

/// <summary>Registration of <see cref="AxiosNet.Client.AxiosClient"/> in a dependency injection container.</summary>
internal static class NamespaceDoc;
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,23 @@

namespace AxiosNet.Extensions.DependencyInjection;

/// <summary>Dependency injection extensions for <see cref="AxiosClient"/>.</summary>
public static class ServiceCollectionExtensions
{
/// <summary>
/// Registers <see cref="AxiosClient"/> as a singleton. Its requests go through the <c>Axios.Net</c> named
/// <see cref="IHttpClientFactory"/> client, so handlers can be chained with
/// <c>AddHttpClient("Axios.Net").AddHttpMessageHandler&lt;T&gt;()</c>.
/// </summary>
/// <remarks>
/// The handler comes from the factory, so <see cref="AxiosRequestConfig.MaxRedirects"/>,
/// <see cref="AxiosRequestConfig.Decompress"/> and <see cref="AxiosRequestConfig.Proxy"/> are no longer read
/// from the request config: configure them on the primary handler. The singleton keeps the handler it was
/// created with, so it is not recycled and DNS changes are not picked up.
/// </remarks>
/// <param name="services">The service collection to add the client to.</param>
/// <param name="configure">Sets the defaults merged into every request of the client.</param>
/// <returns>The same service collection, for chaining.</returns>
public static IServiceCollection AddAxios(this IServiceCollection services, Action<AxiosRequestConfig>? configure = null)
{
services.AddHttpClient("Axios.Net");
Expand Down
51 changes: 51 additions & 0 deletions src/Axios.Net/Client/Axios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,46 +21,97 @@ public static class Axios
typeof(Axios).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion
.Split('+')[0] ?? "0.0.0";

/// <summary>Defaults applied to every request sent through the shared client.</summary>
public static AxiosRequestConfig Defaults => Instance.Defaults;

/// <summary>Interceptors of the shared client.</summary>
public static AxiosInterceptors Interceptors => Instance.Interceptors;

/// <summary>Creates an isolated client with its own defaults and interceptors.</summary>
public static AxiosClient Create(AxiosRequestConfig? config = null, HttpMessageHandler? handler = null) =>
new(config, handler);

/// <summary>Sends a request fully described by <paramref name="config"/>.</summary>
/// <typeparam name="T">Type the response body is read as.</typeparam>
/// <param name="config">The request, merged over the defaults.</param>
public static Task<AxiosResponse<T>> Request<T>(AxiosRequestConfig config) =>
Instance.Request<T>(config);

/// <summary>Sends a GET request.</summary>
/// <typeparam name="T">Type the response body is read as.</typeparam>
/// <param name="url">Absolute URL, or relative to <see cref="AxiosRequestConfig.BaseUrl"/>.</param>
/// <param name="config">Config merged over the defaults for this request.</param>
public static Task<AxiosResponse<T>> Get<T>(string url, AxiosRequestConfig? config = null) =>
Instance.Get<T>(url, config);

/// <summary>Sends a DELETE request.</summary>
/// <typeparam name="T">Type the response body is read as.</typeparam>
/// <param name="url">Absolute URL, or relative to <see cref="AxiosRequestConfig.BaseUrl"/>.</param>
/// <param name="config">Config merged over the defaults for this request.</param>
public static Task<AxiosResponse<T>> Delete<T>(string url, AxiosRequestConfig? config = null) =>
Instance.Delete<T>(url, config);

/// <summary>Sends a HEAD request.</summary>
/// <typeparam name="T">Type the response body is read as.</typeparam>
/// <param name="url">Absolute URL, or relative to <see cref="AxiosRequestConfig.BaseUrl"/>.</param>
/// <param name="config">Config merged over the defaults for this request.</param>
public static Task<AxiosResponse<T>> Head<T>(string url, AxiosRequestConfig? config = null) =>
Instance.Head<T>(url, config);

/// <summary>Sends an OPTIONS request.</summary>
/// <typeparam name="T">Type the response body is read as.</typeparam>
/// <param name="url">Absolute URL, or relative to <see cref="AxiosRequestConfig.BaseUrl"/>.</param>
/// <param name="config">Config merged over the defaults for this request.</param>
public static Task<AxiosResponse<T>> Options<T>(string url, AxiosRequestConfig? config = null) =>
Instance.Options<T>(url, config);

/// <summary>Sends a POST request.</summary>
/// <typeparam name="T">Type the response body is read as.</typeparam>
/// <param name="url">Absolute URL, or relative to <see cref="AxiosRequestConfig.BaseUrl"/>.</param>
/// <param name="data">Request body, serialized according to its type.</param>
/// <param name="config">Config merged over the defaults for this request.</param>
public static Task<AxiosResponse<T>> Post<T>(string url, object? data = null, AxiosRequestConfig? config = null) =>
Instance.Post<T>(url, data, config);

/// <summary>Sends a PUT request.</summary>
/// <typeparam name="T">Type the response body is read as.</typeparam>
/// <param name="url">Absolute URL, or relative to <see cref="AxiosRequestConfig.BaseUrl"/>.</param>
/// <param name="data">Request body, serialized according to its type.</param>
/// <param name="config">Config merged over the defaults for this request.</param>
public static Task<AxiosResponse<T>> Put<T>(string url, object? data = null, AxiosRequestConfig? config = null) =>
Instance.Put<T>(url, data, config);

/// <summary>Sends a PATCH request.</summary>
/// <typeparam name="T">Type the response body is read as.</typeparam>
/// <param name="url">Absolute URL, or relative to <see cref="AxiosRequestConfig.BaseUrl"/>.</param>
/// <param name="data">Request body, serialized according to its type.</param>
/// <param name="config">Config merged over the defaults for this request.</param>
public static Task<AxiosResponse<T>> Patch<T>(string url, object? data = null, AxiosRequestConfig? config = null) =>
Instance.Patch<T>(url, data, config);

/// <summary>Sends a POST request with <c>Content-Type: multipart/form-data</c>.</summary>
/// <typeparam name="T">Type the response body is read as.</typeparam>
/// <param name="url">Absolute URL, or relative to <see cref="AxiosRequestConfig.BaseUrl"/>.</param>
/// <param name="data">Request body, serialized according to its type.</param>
/// <param name="config">Config merged over the defaults for this request.</param>
public static Task<AxiosResponse<T>>
PostForm<T>(string url, object? data = null, AxiosRequestConfig? config = null) =>
Instance.PostForm<T>(url, data, config);

/// <summary>Sends a PUT request with <c>Content-Type: multipart/form-data</c>.</summary>
/// <typeparam name="T">Type the response body is read as.</typeparam>
/// <param name="url">Absolute URL, or relative to <see cref="AxiosRequestConfig.BaseUrl"/>.</param>
/// <param name="data">Request body, serialized according to its type.</param>
/// <param name="config">Config merged over the defaults for this request.</param>
public static Task<AxiosResponse<T>>
PutForm<T>(string url, object? data = null, AxiosRequestConfig? config = null) =>
Instance.PutForm<T>(url, data, config);

/// <summary>Sends a PATCH request with <c>Content-Type: multipart/form-data</c>.</summary>
/// <typeparam name="T">Type the response body is read as.</typeparam>
/// <param name="url">Absolute URL, or relative to <see cref="AxiosRequestConfig.BaseUrl"/>.</param>
/// <param name="data">Request body, serialized according to its type.</param>
/// <param name="config">Config merged over the defaults for this request.</param>
public static Task<AxiosResponse<T>> PatchForm<T>(string url, object? data = null,
AxiosRequestConfig? config = null) =>
Instance.PatchForm<T>(url, data, config);
Expand Down
47 changes: 47 additions & 0 deletions src/Axios.Net/Client/AxiosClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

namespace AxiosNet.Client;

/// <summary>
/// HTTP client with its own defaults and interceptors, the equivalent of an axios instance.
/// Created with <see cref="Axios.Create"/>.
/// </summary>
public class AxiosClient
{
private readonly HttpClient _http;
Expand All @@ -23,8 +27,10 @@ public class AxiosClient
/// <summary>Defaults applied to every request of this client.</summary>
public AxiosRequestConfig Defaults { get; }

/// <summary>Request and response interceptors of this client.</summary>
public AxiosInterceptors Interceptors { get; } = new();

/// <summary>Options used to serialize JSON bodies and to deserialize JSON responses.</summary>
public JsonSerializerOptions JsonOptions { get; set; } = new(JsonSerializerDefaults.Web);

/// <param name="defaults">Defaults merged into every request.</param>
Expand All @@ -44,34 +50,75 @@ public AxiosClient(AxiosRequestConfig? defaults = null, HttpMessageHandler? hand

#region Request aliases

/// <summary>Sends a GET request.</summary>
/// <typeparam name="T">Type the response body is read as.</typeparam>
/// <param name="url">Absolute URL, or relative to <see cref="AxiosRequestConfig.BaseUrl"/>.</param>
/// <param name="config">Config merged over the defaults for this request.</param>
public Task<AxiosResponse<T>> Get<T>(string url, AxiosRequestConfig? config = null) =>
Send<T>(HttpMethod.Get, url, null, config);

/// <summary>Sends a DELETE request.</summary>
/// <typeparam name="T">Type the response body is read as.</typeparam>
/// <param name="url">Absolute URL, or relative to <see cref="AxiosRequestConfig.BaseUrl"/>.</param>
/// <param name="config">Config merged over the defaults for this request.</param>
public Task<AxiosResponse<T>> Delete<T>(string url, AxiosRequestConfig? config = null) =>
Send<T>(HttpMethod.Delete, url, null, config);

/// <summary>Sends a HEAD request.</summary>
/// <typeparam name="T">Type the response body is read as.</typeparam>
/// <param name="url">Absolute URL, or relative to <see cref="AxiosRequestConfig.BaseUrl"/>.</param>
/// <param name="config">Config merged over the defaults for this request.</param>
public Task<AxiosResponse<T>> Head<T>(string url, AxiosRequestConfig? config = null) =>
Send<T>(HttpMethod.Head, url, null, config);

/// <summary>Sends an OPTIONS request.</summary>
/// <typeparam name="T">Type the response body is read as.</typeparam>
/// <param name="url">Absolute URL, or relative to <see cref="AxiosRequestConfig.BaseUrl"/>.</param>
/// <param name="config">Config merged over the defaults for this request.</param>
public Task<AxiosResponse<T>> Options<T>(string url, AxiosRequestConfig? config = null) =>
Send<T>(HttpMethod.Options, url, null, config);

/// <summary>Sends a POST request.</summary>
/// <typeparam name="T">Type the response body is read as.</typeparam>
/// <param name="url">Absolute URL, or relative to <see cref="AxiosRequestConfig.BaseUrl"/>.</param>
/// <param name="data">Request body, serialized according to its type.</param>
/// <param name="config">Config merged over the defaults for this request.</param>
public Task<AxiosResponse<T>> Post<T>(string url, object? data = null, AxiosRequestConfig? config = null) =>
Send<T>(HttpMethod.Post, url, data, config);

/// <summary>Sends a PUT request.</summary>
/// <typeparam name="T">Type the response body is read as.</typeparam>
/// <param name="url">Absolute URL, or relative to <see cref="AxiosRequestConfig.BaseUrl"/>.</param>
/// <param name="data">Request body, serialized according to its type.</param>
/// <param name="config">Config merged over the defaults for this request.</param>
public Task<AxiosResponse<T>> Put<T>(string url, object? data = null, AxiosRequestConfig? config = null) =>
Send<T>(HttpMethod.Put, url, data, config);

/// <summary>Sends a PATCH request.</summary>
/// <typeparam name="T">Type the response body is read as.</typeparam>
/// <param name="url">Absolute URL, or relative to <see cref="AxiosRequestConfig.BaseUrl"/>.</param>
/// <param name="data">Request body, serialized according to its type.</param>
/// <param name="config">Config merged over the defaults for this request.</param>
public Task<AxiosResponse<T>> Patch<T>(string url, object? data = null, AxiosRequestConfig? config = null) =>
Send<T>(HttpMethod.Patch, url, data, config);

/// <summary>POST with <c>Content-Type: multipart/form-data</c>.</summary>
public Task<AxiosResponse<T>> PostForm<T>(string url, object? data = null, AxiosRequestConfig? config = null) =>
Send<T>(HttpMethod.Post, url, data, AsForm(config));

/// <summary>Sends a PUT request with <c>Content-Type: multipart/form-data</c>.</summary>
/// <typeparam name="T">Type the response body is read as.</typeparam>
/// <param name="url">Absolute URL, or relative to <see cref="AxiosRequestConfig.BaseUrl"/>.</param>
/// <param name="data">Request body, serialized according to its type.</param>
/// <param name="config">Config merged over the defaults for this request.</param>
public Task<AxiosResponse<T>> PutForm<T>(string url, object? data = null, AxiosRequestConfig? config = null) =>
Send<T>(HttpMethod.Put, url, data, AsForm(config));

/// <summary>Sends a PATCH request with <c>Content-Type: multipart/form-data</c>.</summary>
/// <typeparam name="T">Type the response body is read as.</typeparam>
/// <param name="url">Absolute URL, or relative to <see cref="AxiosRequestConfig.BaseUrl"/>.</param>
/// <param name="data">Request body, serialized according to its type.</param>
/// <param name="config">Config merged over the defaults for this request.</param>
public Task<AxiosResponse<T>> PatchForm<T>(string url, object? data = null, AxiosRequestConfig? config = null) =>
Send<T>(HttpMethod.Patch, url, data, AsForm(config));

Expand Down
4 changes: 4 additions & 0 deletions src/Axios.Net/Client/NamespaceDoc.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace AxiosNet.Client;

/// <summary>The shared <see cref="Axios"/> client and the <see cref="AxiosClient"/> instances created from it.</summary>
internal static class NamespaceDoc;
1 change: 1 addition & 0 deletions src/Axios.Net/Config/AxiosFormSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ public class AxiosFormSerializer
/// <summary>Array format, same meaning as <see cref="AxiosParamsSerializer.Indexes"/>.</summary>
public bool? Indexes { get; set; } = false;

/// <summary>Maximum nesting depth of an object body; going deeper fails with <see cref="AxiosNet.Errors.AxiosErrorCodes.FormDataDepthExceeded"/>.</summary>
public int MaxDepth { get; set; } = 100;
}
5 changes: 5 additions & 0 deletions src/Axios.Net/Config/AxiosProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@

namespace AxiosNet.Config;

/// <summary>Proxy settings of a request.</summary>
public class AxiosProxy
{
/// <summary>Pass as <see cref="AxiosRequestConfig.Proxy"/> to ignore system and environment proxies.</summary>
public static readonly AxiosProxy Disabled = new();

/// <summary>Proxy scheme, such as <c>http</c>.</summary>
public string Protocol { get; set; } = "http";

/// <summary>Proxy host name or address.</summary>
public string Host { get; set; } = string.Empty;

/// <summary>Proxy port.</summary>
public int Port { get; set; }

/// <summary>User name and password sent to the proxy, when it requires them.</summary>
public (string Username, string Password)? Auth { get; set; }

internal WebProxy ToWebProxy() => new($"{Protocol}://{Host}:{Port}")
Expand Down
5 changes: 5 additions & 0 deletions src/Axios.Net/Config/AxiosRequestConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class AxiosRequestConfig
/// </summary>
public Dictionary<string, object?> Params { get; set; } = [];

/// <summary>Controls how <see cref="Params"/> are turned into a query string.</summary>
public AxiosParamsSerializer? ParamsSerializer { get; set; }

/// <summary>
Expand Down Expand Up @@ -92,15 +93,19 @@ public class AxiosRequestConfig
/// </summary>
public AxiosProxy? Proxy { get; set; }

/// <summary>Called as the request body is sent.</summary>
public Action<AxiosProgressEvent>? OnUploadProgress { get; set; }

/// <summary>Called as the response body is received.</summary>
public Action<AxiosProgressEvent>? OnDownloadProgress { get; set; }

/// <summary>Config keys masked (case-insensitive, recursive) by <see cref="AxiosException.ToJSON"/>.</summary>
public List<string>? Redact { get; set; }

/// <summary>Behaviors kept for compatibility with older axios versions.</summary>
public AxiosTransitional? Transitional { get; set; }

/// <summary>Controls how object bodies are serialized as <c>multipart/form-data</c> or <c>application/x-www-form-urlencoded</c>.</summary>
public AxiosFormSerializer? FormSerializer { get; set; }

/// <summary>Cancels the request (axios' <c>signal</c>).</summary>
Expand Down
2 changes: 2 additions & 0 deletions src/Axios.Net/Config/AxiosResponseType.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
namespace AxiosNet.Config;

/// <summary>How the response body is read.</summary>
public enum AxiosResponseType
{
/// <summary>Body is deserialized from JSON into <c>T</c>.</summary>
Json,
/// <summary>Body is returned as a <c>string</c>.</summary>
Text,

/// <summary>Body is returned as <c>byte[]</c>.</summary>
Expand Down
1 change: 1 addition & 0 deletions src/Axios.Net/Config/AxiosTransitional.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace AxiosNet.Config;

/// <summary>Behaviors kept for compatibility with older axios versions.</summary>
public class AxiosTransitional
{
/// <summary
Expand Down
4 changes: 4 additions & 0 deletions src/Axios.Net/Config/NamespaceDoc.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace AxiosNet.Config;

/// <summary>Request configuration, with its serializers, proxy and response type.</summary>
internal static class NamespaceDoc;
15 changes: 15 additions & 0 deletions src/Axios.Net/Errors/AxiosErrorCodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,28 @@ namespace AxiosNet.Errors;
/// <summary>Values of <see cref="AxiosException.Code"/>, same identifiers as axios.</summary>
public static class AxiosErrorCodes
{
/// <summary>A config option is not valid.</summary>
public const string BadOption = "ERR_BAD_OPTION";

/// <summary>A config option has an invalid value.</summary>
public const string BadOptionValue = "ERR_BAD_OPTION_VALUE";

/// <summary>The server answered with a 4xx status that was rejected.</summary>
public const string BadRequest = "ERR_BAD_REQUEST";

/// <summary>The status was rejected, a size limit was exceeded or the body could not be read.</summary>
public const string BadResponse = "ERR_BAD_RESPONSE";

/// <summary>The request could not reach the server.</summary>
public const string Network = "ERR_NETWORK";

/// <summary>The request was canceled.</summary>
public const string Canceled = "ERR_CANCELED";

/// <summary>The URL is invalid or not absolute.</summary>
public const string InvalidUrl = "ERR_INVALID_URL";

/// <summary>Params or a form object are nested too deeply.</summary>
public const string FormDataDepthExceeded = "ERR_FORM_DATA_DEPTH_EXCEEDED";

/// <summary>Request timed out (default).</summary>
Expand Down
Loading
Loading