Skip to content

StrapiSharpNet is a lightweight C# library for easy integration with Strapi CMS, allowing seamless communication with Strapi's RESTful API.

License

Notifications You must be signed in to change notification settings

muammerkeles/StrapiSharpNet

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

78 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

StrapiSharpNet is a lightweight C# library for easy integration with Strapi CMS, allowing seamless communication with Strapi's RESTful API.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%