StrapiSharp 0.1.4
dotnet add package StrapiSharp --version 0.1.4
NuGet\Install-Package StrapiSharp -Version 0.1.4
<PackageReference Include="StrapiSharp" Version="0.1.4" />
<PackageVersion Include="StrapiSharp" Version="0.1.4" />
<PackageReference Include="StrapiSharp" />
paket add StrapiSharp --version 0.1.4
#r "nuget: StrapiSharp, 0.1.4"
#addin nuget:?package=StrapiSharp&version=0.1.4
#tool nuget:?package=StrapiSharp&version=0.1.4
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
- Ricardo Rauber and his work on StrapiSwift which was leaned upon heavily during development of StrapiSharp.
License
Product | Versions 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. |
-
net7.0
- Microsoft.Extensions.DependencyInjection (>= 7.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.