StrapiSharp 0.1.4

dotnet add package StrapiSharp --version 0.1.4
                    
NuGet\Install-Package StrapiSharp -Version 0.1.4
                    
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="StrapiSharp" Version="0.1.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="StrapiSharp" Version="0.1.4" />
                    
Directory.Packages.props
<PackageReference Include="StrapiSharp" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add StrapiSharp --version 0.1.4
                    
#r "nuget: StrapiSharp, 0.1.4"
                    
#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.
#addin nuget:?package=StrapiSharp&version=0.1.4
                    
Install StrapiSharp as a Cake Addin
#tool nuget:?package=StrapiSharp&version=0.1.4
                    
Install StrapiSharp as a Cake Tool

Build Status Nuget Buy Me A Coffee

StrapiSharp

This project builds a binding around the Strapi API to ease the communication between a .NET project and a Strapi backend; attempting to eliminate the need to write any HttpClient code yourself. A decision has been made at this time to only provide string results from requests; this allows you to bring your own JSON Serializer logic.

Tech Stack

C# 7.0, NUnit, Fluent Assertions

Documentation

At this time there is no formal documentation. I have tried to provide fairly detailed documentation comments throughout, but have not generated anything further at this point.

Usage/Examples

Add the StrapiSharp package to your project by means of dotnet add package StrapiSharp, this will pull from NuGet.

If desired, StrapiSharp can be registered with the dependency injection service builder. You can do that like so.

using StrapiSharp.Extensions;

// Below builder code already in place.
builder.Services.AddStrapiSharp("http://localhost:1337/api");

Basic example of querying a posts resource from Strapi.

using StrapiSharp;
using StrapiSharp.Requests;

var strapi = new Strapi("http://localhost:1337/api");
var request = new QueryRequest("posts");
var result = await strapi.ExecuteAsync(request);
Console.WriteLine(result);

Example of querying and filtering a posts example resource from Strapi.

using StrapiSharp;
using StrapiSharp.Enums;
using StrapiSharp.Requests;

var strapi = new Strapi("http://localhost:1337/api");
var request = new QueryRequest("posts");
request.Filter(FilterType.EqualTo, "id", "42");

var result = await strapi.ExecuteAsync(request);
Console.WriteLine(result);

Example of querying, filtering, and sorting a posts example resource from Strapi.

using StrapiSharp;
using StrapiSharp.Enums;
using StrapiSharp.Requests;

var strapi = new Strapi("http://localhost:1337/api");
var request = new QueryRequest("posts");
request.Filter(FilterType.GreaterThan, "id", "2");
request.Sort("slug", SortDirection.Descending);
request.LimitTo(3);

var result = await strapi.ExecuteAsync(request);
Console.WriteLine(result);

Example of catching a StrapiRequestException and extracting data from within.

using System.Text.Json;
using System.Text.Json.Serialization;
using StrapiSharp;
using StrapiSharp.Requests;
using StrapiSharp.Requests.Convenience;

try
{
	var login = new LoginRequest("username", "password");
	await strapi.ExecuteAsync(login);
}
catch(StrapiRequestException ex)
{
	Console.WriteLine(ex.Message);
	var error = JsonSerializer.Deserialize<ResponseModels.Response>(ex.Response)!.Error;
	Console.WriteLine($"{error!.Name} with status {error.Status}. {error!.Message}");
	Console.ReadLine();
}

namespace ResponseModels
{
	public partial class Response
	{
		[JsonPropertyName("data")]
		public object? Data { get; set; }

		[JsonPropertyName("error")]
		public Error? Error { get; set; }
	}

	public partial class Error
	{
		[JsonPropertyName("status")]
		public long Status { get; set; }

		[JsonPropertyName("name")]
		public string? Name { get; set; }

		[JsonPropertyName("message")]
		public string? Message { get; set; }
	}
}

Running Tests

To run the test suite, run the following command

  dotnet test

Acknowledgements

License

MIT

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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows 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
0.1.4 3,232 7/12/2023
0.1.3 322 4/27/2023
0.1.2 206 4/11/2023
0.1.1 309 4/11/2023
0.1.0 234 3/27/2023