ResgateIO.Service 0.4.2

RES service library for building next gen realtime APIs with synchronized clients using Resgate.

Install-Package ResgateIO.Service -Version 0.4.2
dotnet add package ResgateIO.Service --version 0.4.2
<PackageReference Include="ResgateIO.Service" Version="0.4.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ResgateIO.Service --version 0.4.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

RES Service for .NET : Synchronize Your Clients

Library for .NET used to create next generation REST, real time, and RPC APIs, where all your reactive web clients are synchronized seamlessly through Resgate.

Visit Resgate.io for more information on the project.
Visit the GitHub repository for complete examples with both client and server.

As easy as

ResService service = new ResService("example");
service.AddHandler("model", new DynamicHandler()
    .Get(r => r.Model(new {
        message = "Hello, World!"
    }))
    .Access(r => r.AccessGranted()));
service.Serve("nats://127.0.0.1:4222");

Basic usage

Create a new service
ResService service = new ResService("myservice");
Define a handler class for a model resource
[ResourcePattern("mymodel")]
class MyModelHandler : BaseHandler
{
    private readonly object model = new
    {
        message = "Hello, .NET World!"
    };

    public void Get(IModelRequest request)
    {
        request.Model(model);
    }
}
Define a handler class for a collection resource
[ResourcePattern("mycollection")]
class MyCollectionHandler : BaseHandler
{
    private readonly object[] collection = new object[]{
        "first", "second", "third"
    };

    public void Get(ICollectionRequest request)
    {
        request.Collection(collection);
    }
}
Define methods on a handler class
[ResourcePattern("math")]
class MyResourceHandler : BaseHandler
{
    [CallMethod("double")]
    public void Double(ICallRequest r)
    {
        r.Ok(2 * (double)r.Params["value"]);
    }
}
Add/register handler for a resource
service.AddHandler(new MyResourceHandler());
Add handlers for parameterized resources
service.AddHandler("article.$id", new DynamicHandler()
    .Access(r => r.AccessGranted())
    .ModelGet(r =>
    {
        if (DB.TryGetArticle(r.PathParams["id"], out Article article))
            r.Model(article);
        else
            r.NotFound();
    }));
Send change event on model update

A change event will update the model on all subscribing clients.

MyModel mymodel = new MyModel { Name = "foo" };
MyModel mymodel = new MyModel { Name = "foo" };
service.With("example.mymodel", resource =>
{
    mymodel.Name = "bar";
    resource.ChangeEvent(new Dictionary<string, object> {
        { "name", "bar" }
    });
});
Send add event on collection update:

An add event will update the collection for all subscribing clients.

var mycollection = new List<string> { "first", "second" };
service.With("example.mycollection", resource =>
{
    resource.AddEvent("third", mycollection.Count);
    mycollection.Add("third");
});
Add handlers for authentication
service.AddHandler("myauth", new DynamicHandler()
    .AuthMethod("login", r =>
    {
        if ((string)r.Params["password"] == "mysecret")
        {
            r.TokenEvent(new { user = "admin" });
            r.Ok();
        }
        else
        {
            r.InvalidParams("Wrong password");
        }
    }));
Add handlers for access control (with wildcard ">")
service.AddHandler(">", new DynamicHandler()
    .Access(r =>
    {
        if (r.Token != null && (string)r.Token["user"] == "admin")
            r.AccessGranted();
        else
            r.AccessDenied();
    }));
Add async handler
service.AddHandler("store.users", new DynamicHandler()
    .Get(async r =>
    {
        var users = await DB.QueryAsync("SELECT id FROM users");
        r.Collection(users.Select(u => new Ref("store.user." + u.Id)));
    }));
Start service
service.Serve("nats://127.0.0.1:4222");

Contributing

The ResgateIO.Service library is still under development, and minor versions may still contain breaking changes in the API. Any feedback on the package API or its implementation is highly appreciated!

If you find any issues, feel free to report them as an issue on GitHub.

RES Service for .NET : Synchronize Your Clients

Library for .NET used to create next generation REST, real time, and RPC APIs, where all your reactive web clients are synchronized seamlessly through Resgate.

Visit Resgate.io for more information on the project.
Visit the GitHub repository for complete examples with both client and server.

As easy as

ResService service = new ResService("example");
service.AddHandler("model", new DynamicHandler()
    .Get(r => r.Model(new {
        message = "Hello, World!"
    }))
    .Access(r => r.AccessGranted()));
service.Serve("nats://127.0.0.1:4222");

Basic usage

Create a new service
ResService service = new ResService("myservice");
Define a handler class for a model resource
[ResourcePattern("mymodel")]
class MyModelHandler : BaseHandler
{
    private readonly object model = new
    {
        message = "Hello, .NET World!"
    };

    public void Get(IModelRequest request)
    {
        request.Model(model);
    }
}
Define a handler class for a collection resource
[ResourcePattern("mycollection")]
class MyCollectionHandler : BaseHandler
{
    private readonly object[] collection = new object[]{
        "first", "second", "third"
    };

    public void Get(ICollectionRequest request)
    {
        request.Collection(collection);
    }
}
Define methods on a handler class
[ResourcePattern("math")]
class MyResourceHandler : BaseHandler
{
    [CallMethod("double")]
    public void Double(ICallRequest r)
    {
        r.Ok(2 * (double)r.Params["value"]);
    }
}
Add/register handler for a resource
service.AddHandler(new MyResourceHandler());
Add handlers for parameterized resources
service.AddHandler("article.$id", new DynamicHandler()
    .Access(r => r.AccessGranted())
    .ModelGet(r =>
    {
        if (DB.TryGetArticle(r.PathParams["id"], out Article article))
            r.Model(article);
        else
            r.NotFound();
    }));
Send change event on model update

A change event will update the model on all subscribing clients.

MyModel mymodel = new MyModel { Name = "foo" };
MyModel mymodel = new MyModel { Name = "foo" };
service.With("example.mymodel", resource =>
{
    mymodel.Name = "bar";
    resource.ChangeEvent(new Dictionary<string, object> {
        { "name", "bar" }
    });
});
Send add event on collection update:

An add event will update the collection for all subscribing clients.

var mycollection = new List<string> { "first", "second" };
service.With("example.mycollection", resource =>
{
    resource.AddEvent("third", mycollection.Count);
    mycollection.Add("third");
});
Add handlers for authentication
service.AddHandler("myauth", new DynamicHandler()
    .AuthMethod("login", r =>
    {
        if ((string)r.Params["password"] == "mysecret")
        {
            r.TokenEvent(new { user = "admin" });
            r.Ok();
        }
        else
        {
            r.InvalidParams("Wrong password");
        }
    }));
Add handlers for access control (with wildcard ">")
service.AddHandler(">", new DynamicHandler()
    .Access(r =>
    {
        if (r.Token != null && (string)r.Token["user"] == "admin")
            r.AccessGranted();
        else
            r.AccessDenied();
    }));
Add async handler
service.AddHandler("store.users", new DynamicHandler()
    .Get(async r =>
    {
        var users = await DB.QueryAsync("SELECT id FROM users");
        r.Collection(users.Select(u => new Ref("store.user." + u.Id)));
    }));
Start service
service.Serve("nats://127.0.0.1:4222");

Contributing

The ResgateIO.Service library is still under development, and minor versions may still contain breaking changes in the API. Any feedback on the package API or its implementation is highly appreciated!

If you find any issues, feel free to report them as an issue on GitHub.

Release Notes

https://github.com/jirenius/csharp-res/releases/tag/v0.4.2

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
0.4.2 68 11/28/2019
0.4.1 62 10/21/2019
0.3.1 78 8/29/2019
0.3.0 68 8/27/2019
0.2.0 76 8/22/2019
0.1.0 78 8/15/2019