Client-side API consumption
This tutorial will teach you the basics of using client-side libraries.
Prerequisites
- Windows 10/11
- Visual studio 2019/2022
- Phoesion Glow Blaze , with Reactor running (see getting started guide)
Sample Code
You can download the Sample Code archive from the downloads section.
This tutorial follows the 1_REST sample in the archive. (View code on GitHub)
Introduction
If your services provide an API class, you can consume this API from client-side using strong-typed method calls. This allows you to easily consume the api almost like if it was a normal C# method. Changes in the API that break compatibility, will give build-time errors to the client api consumers.
Create a client API consumers
1. Sample Module API
Assuming a service with a module API as so : ( in an assembly Foompany.Services.API.SampleService1.dll )
namespace Foompany.Services.API.SampleService1.Modules.SampleModule1
{
public abstract class Actions
{
[Action(Methods.GET)]
public static string DoTheThing(string username) => null;
}
}
2. Client App
We create a simple WinForm application with a textbox and a button that will be our client app, named Foompany.SampleClient.MyApp (View code on GitHub)
3. Add References
To use the module api, we need to reference
- The module api assembly
- The Phoesion.Glow.SDK.Client.REST package (from NuGet)
4. Button Code
Next we need to add the following code to the Button's handler
private async void button1_Click(object sender, EventArgs e)
{
using (var client = new Phoesion.Glow.SDK.Client.REST.GlowRestClient("localhost:16000", IsSecure: false)) //create glow rest client
{
//call service/module action
var rsp = await client.Call(Foompany.Services.API.SampleService1.Modules.SampleModule1.Actions.DoTheThing, txt_SampleInput.Text).InvokeAsync();
if (rsp == null)
MessageBox.Show($"Could not reach firefly service");
else
MessageBox.Show($"Got Response : {rsp}");
}
}
The code first creates a new GlowRestClient instance, and then we use client.Call() with the first argument the api method for the action (DoTheThing), followed by the parameters (username). The function will return the response's result object (will be deserialized automatically) or null if failed (default value) .