Nancy.Rest.Client
1.4.4
dotnet add package Nancy.Rest.Client --version 1.4.4
NuGet\Install-Package Nancy.Rest.Client -Version 1.4.4
<PackageReference Include="Nancy.Rest.Client" Version="1.4.4" />
paket add Nancy.Rest.Client --version 1.4.4
#r "nuget: Nancy.Rest.Client, 1.4.4"
// Install Nancy.Rest.Client as a Cake Addin #addin nuget:?package=Nancy.Rest.Client&version=1.4.4 // Install Nancy.Rest.Client as a Cake Tool #tool nuget:?package=Nancy.Rest.Client&version=1.4.4
Nancy.Rest.Client
Dynamic proxy client generation for Nancy using Nancy.Rest.Module.
Prerequisites
A server using Nancy & Nancy.Rest.Module.
It is recommended you read the Nancy.Rest.Module documentation to understand how the server mount the services before continuing reading.
Installation
- Add Nancy.Rest.Client and Nancy.Rest.Annotations to your client project.
Or
- Add the Nuget package Nancy.Rest.Client
Add your server models and interface with the method signatures to use.
Basic Usage
####Your Server signatures:
namespace Nancy.Rest.ExampleServer
{
[RestBasePath("/api")]
public interface IExample
{
[Rest("Person", Verbs.Get)]
List<Person> GetAllPersons();
[Rest("Person/{personid}", Verbs.Get)]
Person GetPerson(int personid);
[Rest("Person", Verbs.Post)]
bool SavePerson(Person person);
[Rest("Person/{personid}", Verbs.Delete)]
bool DeletePerson(int personid);
}
}
####Your Server Models
namespace Nancy.Rest.ExampleServer
{
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
[Level(1)]
public bool IsProgrammer { get; set; }
[Tags("Attr")]
public List<string> Attributes { get; set; }
}
}
Your Client
namespace Nancy.Rest.ExampleClient
{
public class Example
{
public void Run()
{
IExample server=ClientFactory.Create<IExample>("http://yourserver/api"); `
List<Person> persons=server.GetPersons();
}
}
}
##Advanced Usage
###Transversal Filtering
Nancy.Rest.Client includes this interface.
using System.Collections.Generic;
namespace Nancy.Rest.Client.Interfaces
{
public interface IFilter<T>
{
T FilterWithLevel(int level);
T FilterWithTags(IEnumerable<string> tags);
T FilterWithLevelAndTags(int level, IEnumerable<string> tags);
}
}
Create a new interface in your client that includes, both, IFilter interface and your server interface.
namespace Nancy.Rest.ExampleClient
{
public interface IExampleWithFiltering : IExample, IFilter<IExample>
{
}
}
Then you can use the transversal filtering capabilities of the server like this:
namespace Nancy.Rest.ExampleClient
{
public class Example
{
public void Run()
{
IExampleWithFiltering server=ClientFactory.Create<IExampleWithFiltering>("http://yourserver/api"); `
//Per example, you can ask the server to not serialize any property with level bigger than the number provided.
//Here we can filter the IsProgrammer property using levels.
List<Person> persons=server.FilterWithLevel(0).GetPersons();
//Or remove the Attributes property using Tags.
List<Person> persons=server.FilterWithTags(new string[] { "Attr"}).GetPersons();
}
}
}
###Controlable deserialization
Imagine you have your poco models from the server, but you need to add some properties, methods or INotifyPropertyChanged to that objects, you create a child class from the model, and add all those things. The problem is, the deserialzer will deserialize your poco model, so you have to create a new child class, and copy all properties to your child. Nancy.Rest.Client have the capability of deserializing the objects to child objects.
####Client model
namespace Nancy.Rest.ExampleClient
{
public class ClientPerson : Person
{
public bool IsSuperman { get; set; }
public void DoSomeNastyThings()
{
//Kidding
}
}
}
####Example
namespace Nancy.Rest.ExampleClient
{
public class Example
{
public void Run()
{
Dictionary<Type,Type> mappings=new Dictionary<Type,Type>();
//Here we add the mapping, we want every person to be deserialized as ClientPerson
mappings.Add(typeof(Person), typeof(ClientPerson));
IExample server=ClientFactory.Create<IExample>("http://yourserver/api", mappings); `
//Here is your list of client persons
List<ClientPerson> persons=server.GetPersons().Cast<ClientPerson>.ToList();
}
}
}
##WARNING
THIS IS AN BETA VERSION, so far it works on my machine 😉
TODO
- Fill the blanks 😛
History
1.4.3-Beta: Removed bugs, published nugets. 1.4.3-Alpha: First Release
Built With
Credits
- Maximo Piva - maxpiva
License
This project is licensed under the MIT License - see the LICENSE.md file for details
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. 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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 is compatible. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 4.7.1
- ImpromptuInterface (>= 7.0.1)
- Nancy.Rest.Annotations (>= 1.4.4)
- Newtonsoft.Json (>= 12.0.3)
- System.Net.Http (>= 4.3.4)
- System.Runtime (>= 4.3.1)
- System.Runtime.InteropServices (>= 4.3.0)
-
.NETStandard 2.0
- ImpromptuInterface (>= 7.0.1)
- Nancy.Rest.Annotations (>= 1.4.4)
- Newtonsoft.Json (>= 12.0.3)
- System.Net.Http (>= 4.3.4)
- System.Runtime (>= 4.3.1)
- System.Runtime.InteropServices (>= 4.3.0)
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 |
---|---|---|
1.4.4 | 874 | 5/31/2020 |
1.4.3.9-beta | 967 | 12/5/2017 |