Skip to content

AndreasReitberger/RestApiClientSharp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

173 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RestApiClientSharp

Is a common library for a REST API client solution.

Support me

If you want to support me, you can order over following affilate links (I'll get a small share from your purchase from the corresponding store).

(*) Affiliate link Thank you very much for supporting me!

Nuget

Get the latest version from nuget.org
NuGet NuGet

How to use

Available REST functions

public virtual async Task<IRestApiRequestRespone?> SendRestApiRequestAsync(...);
public virtual async Task<IRestApiRequestRespone?> SendMultipartFormDataFileRestApiRequestAsync(...);
public virtual async Task<byte[]?> DownloadFileFromUriAsync(...);

SendRestApiRequestAsync()

This function allows you to send a simple REST request to your REST-API-Server endpoint. If succeeded, you can access the received string and byte[] as followed.

IRestApiRequestRespone.Result
IRestApiRequestRespone.RawBytes

SendMultipartFormDataFileRestApiRequestAsync()

This function allows you to send a multi form data REST request to your REST-API-Server endpoint. If succeeded, you can access the received string and byte[], the same as with the SendRestApiRequestAsync() function.

DownloadFileFromUriAsync()

This function allows you to download a file as byte[].

Integration

You just can inherit from the RestApiClient class to get access to all common REST functions of our client.

public partial class MyCustomRestClient : RestApiClient
{
    #region Constructor
    public MyCustomRestClient() : base() 
    { }
    
    public MyCustomRestClient(string apiKey, string tokenName = "token") : base(
        new AuthenticationHeader() { Token = apiKey, Target = AuthenticationHeaderTarget.UrlSegment},
        tokenName) 
    { }
    
    public MyCustomRestClient(string webAddress, string apiKey, string tokenName = "token") : base(
        new AuthenticationHeader() { Token = apiKey, Target = AuthenticationHeaderTarget.UrlSegment },
        tokenName, webAddress)
    { }
    #endregion
}

Example of communication

public async Task<StockQuoteResult?> GetStockQuoteAsync(string symbol)
{
    IRestApiRequestRespone? result = null;
    StockQuoteResult? resultObject = null;
    Dictionary<string, string> parameters = [];
    try
    {
        if (!string.IsNullOrEmpty(symbol)) parameters.Add("symbol", symbol);
        string targetUri = $"";
        result = await SendRestApiRequestAsync(
               requestTargetUri: targetUri,
               method: Method.Get,
               command: "quote",
               jsonObject: null,
               authHeaders: AuthHeaders,
               // Parameters
               urlSegments: parameters,
               cts: default
               )
            .ConfigureAwait(false);
        resultObject = GetObjectFromJson<StockQuoteResult>(result?.Result, NewtonsoftJsonSerializerSettings);
        return resultObject;
    }
    catch (Exception exc)
    {
        OnError(new UnhandledExceptionEventArgs(exc, false));
        return resultObject;
    }
}

You will find some complete, production examples here:

About

A common library for a REST API client solution.

Resources

License

Stars

Watchers

Forks

Sponsor this project

  •  

Packages

 
 
 

Contributors

Languages