SourceCrafter.HttpServiceClientGenerator 0.23.144.75

There is a newer version of this package available.
See the version list below for details.
dotnet add package SourceCrafter.HttpServiceClientGenerator --version 0.23.144.75                
NuGet\Install-Package SourceCrafter.HttpServiceClientGenerator -Version 0.23.144.75                
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="SourceCrafter.HttpServiceClientGenerator" Version="0.23.144.75" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SourceCrafter.HttpServiceClientGenerator --version 0.23.144.75                
#r "nuget: SourceCrafter.HttpServiceClientGenerator, 0.23.144.75"                
#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 SourceCrafter.HttpServiceClientGenerator as a Cake Addin
#addin nuget:?package=SourceCrafter.HttpServiceClientGenerator&version=0.23.144.75

// Install SourceCrafter.HttpServiceClientGenerator as a Cake Tool
#tool nuget:?package=SourceCrafter.HttpServiceClientGenerator&version=0.23.144.75                

🍰 SourceCrafter.HttpClientGenerator: Web API source generator for API interfaces.

You just can build | update | generate* your API calls

Under these premises

  • Properties and indexers will generate path segments, encoded to url components
  • Methods are HTTP verbs invokers for GET, DELETE, POST, PUT and PATCH requests (e.g.:  GetAsync(...)), with the following parameters (all of them are optional):
    • TResponse?: Return type for the async method
    • TContent? content: For POST, PUT and PATCH requests content.
    • TQuery? query: Generic parameter to be serialized into URL query components
    • Action<HttpClient, HttpRequestMessage>? requestHandler: A handler for requests before send it to the server

      Usage: Set some information to headers, transform

    • Action<HttpClient, HttpResponseMessage>? requestHandler: A handler for after get the response and before serialize the possible content coming from the server

      Usage: Collect information from response headers, evaluate response composition

    • CancellationToken? cancelToken: A cancellation token for this task

Operation metadata (comments before)

Key Description
queryParamName Sets the query parameter name. Defaults to query. E.g.: ?status=pending whereas status is the queryParamName for a value type parameter
contentParamName Sets the content parameter name. Defaults to content. For MultipartFormData or FormUrlEncoded content types creates a named item for value type parameters
contentFormatType Determines the content format type. MultipartFormData and FormUrlEncoded. For Xml, a StreamContent is created to transport the serialized data. Json uses the same System.Net.Http.Json.JsonContent are available
responseFormatType Determines the response format type. Just Xml and Json

Rest of {key}: {value} pairs parsed on comments will be trated as request headers


The client

// Generates a client class with the following name convention
// Takes Name from I{Name}Api and adds 'Client' to the end
var _petStoreClient = new PetStoreClient();

The usage

//will produce a call to https://petstore.swagger.io/v2/store/inventory
var inventory = await _petStoreClient.Store.Inventory.GetAsync();

The structure (*generated)

[HttpOptions(
    //API Url
    "https://petstore.swagger.io/v2/", 
    // Url Encode properties are formatted specific casing (CamelCase, PascalCase, Upper and Lower snake casing)
    QueryCasing = Casing.CamelCase, 
    // Path segments casing format
    PathCasing = Casing.CamelCase, 
    // Enum values casing format on query and path. None for its underlying value
    EnumQueryCasing = Casing.CamelCase, 
    // Enum values casing format on content serialization
    EnumSerializationCasing = Casing.CamelCase, 
    // Properties casing format on content serialization
    PropertyCasing = Casing.CamelCase
)]
public interface IPetStoreApi
{
    IStore Store { get; }
    IPet Pet { get; }
    IUser User { get; }
}

public interface IStore
{
    IOrder Order { get; }
    IStoreInventory Inventory { get; }
}

public interface IPet
{
    IPetActionsByPetId this[long petId] { get; }
    IPetActionsByStatus FindByStatus { get; }
    IOrderActions Order { get; }
}

public interface IOrder:
    IPost<User, User>
{
    IOrderActionsByOrderId this[int orderId] { get; }
}

public interface IOrderActionsByOrderId : 
    IGet<Order>, 
    IDelete<ApiResponse>
{
}

public interface IPetActionsByStatus :
    // queryParamName: status 
    IGet<PetStatus, List<Pet>>
{
}
public interface IOrderActions: 
    IPost<Order, Order>,
    IPut<Order, Order>
{                               
}

public interface IPetActionsByPetId : 
    IGet<Pet>, 
    IDelete<ApiResponse>, 
    IPost<Pet, Pet>
{
    IPetActionsByPetIdUploadImage UploadImage { get; }
}
public interface IPetActionsByPetIdUploadImage :
    // contentParamName: file
    IPost<FileInfo, ApiResponse>
{
}

public interface IStoreInventory: IGet<Dictionary<string, long>>
{
}

public interface IUser:IPost<User, ApiResponse>
{
    IUserActionsByUserName this[string userName] { get; }
}

public interface IUserActionsByUserName :
    IGet<User>,
    IDelete<ApiResponse>,
    IPut<User, User>
{

}

The models

public enum PetStatus
{
    Available, Pending, Sold
}

public enum OrderStatus
{
    Placed, Approved, Delivered
}

public class Order
{
    public int Id { get; set; }
    public int PetId { get; set; }
    public int Quantity { get; set; }
    public DateTime ShipDate { get; set; }
    public OrderStatus Status { get; set; }
    public bool Complete { get; set; }
}

public class ApiResponse
{
    public int Code { get; set; }
    public string Type { get; set; }
    public string Message { get; set; }
}

public class Category
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class Pet
{
    public long Id { get; set; }
    public Category Category { get; set; }
    public string Name { get; set; }
    public string[] PhotoUrls { get; set; }
    public Tag[] Tags { get; set; }
    public PetStatus Status { get; set; }
}

public class Tag
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class User
{
    public long Id { get; set; }
    public string Username { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public string Phone { get; set; }
    public int UserStatus { get; set; }
    public OrderStatus Status { get; set; }
}

Generated content

The following interface

public interface IPetActionsByPetIdUploadImage :
    // contentParamName: file
    IPost<FileInfo, ApiResponse>
{
}

would generate the following service class (based on the previous definition example):

//<auto generated>
using static SourceCrafter.HttpServiceClient.GeneratorHelpers;
using System.Net.Http.Json;
using System.IO;
using System;
using System.Net.Http;
using System.Threading.Tasks;
using System.Threading;
using Domain.Service.Models;
using SourceCrafter.HttpServiceClient;

namespace Domain.Service
{
    public class PetActionsByPetIdUploadImageService : IPetActionsByPetIdUploadImage 
    {
        private readonly PetStoreAgent _agent;
        private readonly string _path;
        
        internal PetActionsByPetIdUploadImageService(PetStoreAgent agent, string path)
        {
            _agent = agent;
            _path = path;            
        }

        public async Task<ApiResponse> PostAsync(FileInfo file, Func<HttpRequestMessage, Task> beforeSend = default, Func<HttpResponseMessage, Task> afterSend = default, CancellationToken cancellationToken = default)
        {
            var request = new HttpRequestMessage(HttpMethod.Post, new Uri(_path, UriKind.Relative)) {
                Content = GeneratorHelpers.CreateMultipartFormData(ArrayFrom((file.ToByteArrayContent(), "file", file.Name)))
            };
            var response = await _agent._httpClient.SendAsync(request, cancellationToken);

            return response switch 
            {
                { IsSuccessStatusCode: true, Content: {} responseContent } => 
                    await responseContent.ReadFromJsonAsync<ApiResponse>(_agent._jsonOptions, cancellationToken),

                { IsSuccessStatusCode: false } => 
                    throw new HttpRequestException(response.ReasonPhrase),

                _ => default(ApiResponse)
            };
        }
    }
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.24.71.5 152 3/11/2024
0.23.155.7 199 6/4/2023
0.23.149.66 166 5/29/2023
0.23.149.53 168 5/29/2023
0.23.148.16 178 5/28/2023
0.23.147.89 175 5/27/2023
0.23.144.75 196 5/24/2023
0.23.144.56 168 5/24/2023
0.23.144.52 182 5/24/2023
0.23.144.25 195 5/24/2023
0.23.122.13 162 5/2/2023

Trying to fix analysis packages