C# wrapper for the IPW Metazo API
Use the extension method AddIpwBridge()
.
builder.Services.AddIpwBridge(options =>
{
options.IpwUrl = "https://your-domain.dk/metazo/api/v1/";
options.IpwUser = "USERNAME";
options.IpwPassword = "PASSWORD";
options.ChecksumSecret = "CHECKSUM-SECRET";
});
Which will setup DI for the IMetazoApiClient
service.
Example:
public class Worker(IMetazoApiClient metazoApiClient) : BackgroundService
{
private readonly IMetazoApiClient _metazoApiClient = metazoApiClient;
}
The IMetazoApiClient
interface exposes a few methods to use:
Task<JsonElement> GetDatatypesAsync();
Task<JsonElement> GetExplanationAsync(string datatype);
Task<JsonElement> GetListAsync(ListRequest dataRequest);
Task<JsonElement> GetItemAsync(int objectId);
Task<JsonElement> SendModelAsync(IpwCrudRequest crudModel);
Task<JsonElement> UploadBinfileAsync(BinfileUploadRequest binfileUploadModel);
Note: Not every single endpoint from the API has been covered. The project is being updated with endpoints as I find a need for them.
var datatypes = await _metazoApiClient.GetDatatypesAsync()
string datatype = "form113622";
var explanation = await _metazoApiClient.GetExplanationAsync(datatype);
// Find all entries in the form, that has an objectId greater than '2604436'.
// The entries we find, we also want to get their fields as specified in 'FieldsToGet'.
ListRequest request = new()
{
DataType = "form121889",
FieldsToGet = "f276474,f1628152,f2605112",
SearchAfter = "2604436",
SearchField = "objectid",
SearchOperation = "GREATER"
};
wait _metazoApiClient.GetListAsync(request)
var item = await _metazoApiClient.GetItemAsync(2605115);
Dictionary<string, string> jsonData = new()
{
{ "state", "2" },
};
var fullJson = JsonSerializer.Serialize(jsonData);
IpwCrudRequest crudRequest = new()
{
Datatype = "form121889",
Model = ModelOptions.Create,
JsonData = fullJson
};
var crudResponse = await _metazoApiClient.SendModelAsync(crudRequest);
Note: You can also use ModelOptions.Update
or ModelOptions.Delete
, however these both require the ObjectId
property to be set.
using var fileStream = File.OpenRead("testimg.png");
BinfileUploadRequest uploadRequest = new()
{
ParentId = 2605115,
Files = new()
{
{ "file_1", fileStream }
}
};
WritePretty(await _metazoApiClient.UploadBinfileAsync(uploadRequest));
Note: While I haven't tested it, it should also be able to handle multiple files being uploaded at once.