Skip to content

DarkenSoda/RestHandler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RestHandler

A simple and flexible library for handling RESTful API requests using pure C# HttpClient and Newtonsoft.Json. This library allows you to easily send GET, POST, PUT, and DELETE requests with support for headers, authentication, and custom content types.

Jump to

Features

  • Ability to send requests and retrieve responses as raw JSON or deserialized objects.
  • Lazy initialization of HttpClient to ensure memory efficiency.
  • Support for adding and removing headers.
  • Authorization support (e.g., Bearer tokens, custom schemes).
  • Easy timeout configuration.
  • Chainable HttpRequestMessageBuilder for constructing custom requests.

Roadmap

Here is the current roadmap for future updates.

  • Exception Handling: Currently, this library does not handle exception handling. Future updates may include built-in support for managing exceptions to improve usability and robustness.
  • JsonName Attribute: Attribute to change the name of a field in the object scheme
      public class Student {
        [JsonName("fullName")]
        string Name { get; set; }
        
        [JsonName("age")]
        int Age { get; set; }
      }
    this makes it so you can Parse a Json with a scheme { "fullName": "MyName", "age": 23 } to the Student class even if the fields are named differently.

Installation

  1. Download the source code or clone the repository.

    git clone https://github.com/DarkenSoda/RestHandler.git
  2. Ensure that you have Newtonsoft.Json installed in your project. You can install it via NuGet:

    Install-Package Newtonsoft.Json

    or using CLI

    dotnet add package Newtonsoft.Json
  3. Add the DarkenSoda.RestHandler namespace to your project.

Usage

Sending a GET Request

You can send a GET request and get the response as a deserialized object or as a raw string.

using DarkenSoda.RestHandler;
using System.Threading.Tasks;

// make sure this has the same scheme as the api
public class MyResponseType
{
    public int TestNumber { get; set; }
}

public async Task GetExample()
{
    MyResponseType response = await ApiRequest.GetAsync<MyResponseType>("https://api.example.com/data");

    // Raw JSON response
    string jsonResponse = await ApiRequest.GetRawStringAsync("https://api.example.com/data");
}

Sending a POST Request

To send a POST request with content:

using DarkenSoda.RestHandler;
using System.Threading.Tasks;

public async Task PostExample()
{
    MyContent requestData = new MyContent() { Name = "John", Age = 30 };

    MyResponseType response = await ApiRequest.PostAsync<MyResponseType, MyContent>("https://api.example.com/data", requestData);

    // Raw JSON response
    string jsonResponse = await ApiRequest.PostRawStringAsync<MyContent>("https://api.example.com/data", requestData);
}

Setting up Base URL and Headers

Before sending any requests, you can configure the base URL and headers.

using DarkenSoda.RestHandler;

// Set the base address for all requests
HttpClientManager.SetBaseAddress("https://api.example.com");

// Add custom headers
HttpClientManager.AddHeader("Custom-Header", "HeaderValue");

Adding Authorization

If your API requires an authorization token, you can include it in the request:

using DarkenSoda.RestHandler;

HttpClientManager.AddAuthorizationHeader("Bearer", "your-token");

Removing Headers or Authorization

To remove a specific header or authorization from your request:

HttpClientManager.RemoveHeader("Custom-Header");
// or
HttpClientManager.ClearHeaders();   // clear all headers

HttpClientManager.RemoveAuthorizationHeader();

Custom Timeout

To configure the request timeout:

HttpClientManager.SetTimeout(30); // Timeout in seconds

Example Using All Features

This example shows how you can use everything together.

Note you can set the Headers and Authorization for the entire library using the HttpClientManager or send them per use as shown below:

static async Task Main(string[] args)
{
    // Set the base address for the HttpClient
    HttpClientManager.SetBaseAddress("https://api.example.com/");

    // Define the headers, scheme, and token
    var headers = new Dictionary<string, string>
    {
        { "Custom-Header", "HeaderValue" },
        { "Another-Header", "AnotherValue" }
    };
    string token = "your-jwt-token";
    string scheme = "Bearer";

    // Example: Sending a GET request with optional headers and authorization
    string getUrl = "resource/get";
    var response = await ApiRequest.GetAsync<MyResponseType>(getUrl, scheme, token, headers);
    Console.WriteLine("GET Response: " + response);

    // Example: Sending a POST request with headers, authorization, and content
    string postUrl = "resource/post";
    MyContent postContent = new MyContent()
    {
        Name = "Example",
        Value = 123
    };
    var postResponse = await ApiRequest.PostAsync<MyResponseType2, MyContent>(postUrl, postContent, scheme, token, headers);
    Console.WriteLine("POST Response: " + postResponse);
}

Contributing

Contributions are welcome! If you'd like to contribute to this project, please follow these steps:

  1. Fork the repository.
  2. Create a new branch for your feature or bugfix (git checkout -b feature-name).
  3. Add your feature or fix and update the CONTRIBUTORS file with your name.
  4. Commit your changes (git commit -m 'Add feature').
  5. Push to the branch (git push origin feature-name).
  6. Open a pull request.

By following these steps, your contribution and name will be recognized once your pull request is merged.


License

This project is licensed under the MIT License - see the LICENSE file for details.