SweetMeSoft Connectivity Library is a compilation of easy to use tools to connect with ThirdParties services and APIs
$ dotnet add package SweetMeSoft.ConnectivityLibrary to simplify and standardize requests to APIs and web services.
SweetMeSoft.Connectivity is a library for .NET Standard 2.1 that provides an ApiReq class as a wrapper over HttpClient. Its goal is to facilitate making HTTP requests (GET, POST, PUT, DELETE), handling different content types, cookie management and response deserialization in a structured and reusable way.
HttpClient into a simpler API.GET, POST, PUT and DELETE.application/json, application/x-www-form-urlencoded and multipart/form-data.GenericReq<T> for requests and GenericRes<T> for responses, allowing strong typing.DownloadFile method to get files as a StreamFile.ApiReq.Instance for easy access.dotnet add package SweetMeSoft.Connectivity
The library is used through the singleton instance ApiReq.Instance.
GenericReq<T>: The RequestThis object is used to configure all request details.
| Property | Type | Description |
|---|---|---|
Url | string | Required. The endpoint URL. |
Data | T | The object with data to send in the request body (for POST and PUT). |
HeaderType | HeaderType | The content type (json, xwwwunlercoded, formdata). |
Authentication | Authentication | Object to configure authentication (e.g. Bearer Token). |
Cookies | string | Cookies to send in the request. |
Headers | HttpHeaders | Additional HTTP headers. |
AdditionalParams | List<KeyValuePair<string, string>> | Additional parameters for xwwwunlercoded. |
GenericRes<T>: The ResponseThis object contains the request result.
| Property | Type | Description |
|---|---|---|
Object | T | The deserialized response object (if the request was successful). |
HttpResponse | HttpResponseMessage | The original HTTP response. |
Error | ErrorDetails | Error details (if the request failed). |
Cookies | string | The cookies received in the response. |
IsSuccess | bool | Indicates if the request was successful (true/false). |
// Define the expected response model
public class User
{
public int Id { get; set; }
public string Name { get; set; }
}
// Make the request
var response = await ApiReq.Instance.Get<User>("https://api.example.com/users/1");
if (response.IsSuccess)
{
User user = response.Object;
// ... use the 'user' object
}
else
{
// ... handle the error with response.Error
}
// Define the request model
public class CreateUserInput
{
public string Name { get; set; }
public string Email { get; set; }
}
// Define the response model
public class CreateUserOutput
{
public int Id { get; set; }
public string CreatedAt { get; set; }
}
// Prepare the request
var requestData = new CreateUserInput { Name = "John Doe", Email = "john.doe@example.com" };
var request = new GenericReq<CreateUserInput>
{
Url = "https://api.example.com/users",
Data = requestData,
HeaderType = HeaderType.json // or xwwwunlercoded, formdata
};
// Make the request
var response = await ApiReq.Instance.Post<CreateUserInput, CreateUserOutput>(request);
if (response.IsSuccess)
{
CreateUserOutput newUser = response.Object;
// ...
}
This project is under the MIT license.