Nexd.Rest 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Nexd.Rest --version 1.0.1
NuGet\Install-Package Nexd.Rest -Version 1.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Nexd.Rest" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Nexd.Rest --version 1.0.1
#r "nuget: Nexd.Rest, 1.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Nexd.Rest as a Cake Addin
#addin nuget:?package=Nexd.Rest&version=1.0.1

// Install Nexd.Rest as a Cake Tool
#tool nuget:?package=Nexd.Rest&version=1.0.1

C# Sync & Async HttpClient Wrapper

Installation

Available on NuGet NuGet version (Nexd.Rest)

dotnet add package Nexd.Rest --version 1.0.1

Note

  • There is two classes that you can use, depends on your usage.
    • If you don't need to maintain constant connection, just want to make a simple HttpRequest you can just use the HttpRequest class.
    • If you plan on 'connecting' to an API and actively doing HttpRequest you should use the HttpAPI class that keeps a HttpClient instance alive and is using the same HttpRequest class with that instance for its lifetime.

Initialization

// import using
using Nexd.Rest;

// random user class that I may send, or get in a result of a `HttpRequest`
class User : IJsonObject // If you have classes that you want to use as return values from a `HttpRequest` (or want to use in a `HttpMethod.Post` request, they should implement this interface)
{
    [JsonPropertyName("id")]
    public int ID;

    [JsonPropertyName("username")]
    public string Username;
}

HttpAPI method

// in case of using `HttpAPI`
HttpAPI api = new HttpAPI("http://api.myserver.domain"); // Note: no slash at the end

User? user = api.SendRequest<User>("/user/get/id/1"); // random route, it depends on your API
Console.WriteLine($"{(user as IJsonObject).ToJSON()}"); // IJsonObject implements default ToJSON method which is a wrapper around `System.Text.Json` serialization method

// you can also make your api wrapper using inheritance like this:
class MyAPI : HttpAPI
{
    public MyAPI(string url) : base(url)
    {
            this.Client.DefaultRequestHeaders.Add("Authorization", $"Bearer {Token}"); // maybe set your security tokens here, or anything that is related to the `HttpClient` itself
            this.Client.Timeout = TimeSpan.FromSeconds(5);
    }

    public IEmptyResponse? CreateUser(User user)
    {
        return this.SendRequest<IEmptyResponse>("/user/create", HttpMethod.Post, user);
    }

    public User? GetUserById(int id)
    {
        return this.SendRequest<User>($"/user/get/id/{id}");
    }
}

HttpRequest method

// Sending a GET request to the api
using (HttpRequest request = new HttpRequest("http://api.myserver.domain/user/get/id/15", HttpMethod.Get))
{
    User? user = request.Send<User>();
}

Async

Note: that every method has its Async version implemented

Documentation is currently unavailable, but its self explanatory and has the same syntax with the sync version, just with Async method names.

TODO

  • CancellationToken
  • Async documentation
  • More detailed documentation in general
Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net7.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.2 72 4/29/2024
1.0.1 229 11/27/2023
1.0.0 135 11/5/2023