Skip to content

Commit

Permalink
Rename manifest fields, add example setting (#5)
Browse files Browse the repository at this point in the history
* rename manifest fields, add example setting

* fix manifest version

* parse settings

---------

Co-authored-by: Clement Bouvet <clement@cbouvet.fr>
  • Loading branch information
alexcasalboni and CLEMENTINATOR authored Feb 14, 2025
1 parent 4224f73 commit b48d1e1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 25 deletions.
60 changes: 38 additions & 22 deletions dc_component.c
Original file line number Diff line number Diff line change
@@ -1,67 +1,83 @@
#include "internal/data_collection.h"
#include <stdlib.h>
#include <string.h>

/*
These are the functions that you will need to implement in order to create your component.
These functions are called by the Edgee runtime to get the HTTP request to make to the provider's API for each event type.
You should set your request parameters in the `ret` struct and return true if the request was successful.
*/
struct component_settings {
data_collection_string_t example;
};

bool exports_edgee_protocols_data_collection_page(exports_edgee_protocols_data_collection_event_t *e, exports_edgee_protocols_data_collection_dict_t *settings, exports_edgee_protocols_data_collection_edgee_request_t *ret, data_collection_string_t *err) {
/*
If your settings are set as:
[[components.data_collection]]
id = "my_component"
component = "output.wasm"
settings.test_project_id = "123456789"
settings.test_write_key = "abcdefg"
Then the following code:
for (int i = 0; i < settings.len; i++) {
printf("%s: %s\n", settings.ptr[i].f0.ptr, settings.ptr[i].f1.ptr);
}
Will print:
test_project_id: 123456789
test_write_key: abcdefg
*/
struct component_settings parse_settings(exports_edgee_protocols_data_collection_dict_t *settings) {
struct component_settings ret;
for (int i = 0; i < settings->len; i++) {
if (settings->ptr[i].f0.len == 0) {
continue;
}
if (strncmp((const char*)settings->ptr[i].f0.ptr, "example", settings->ptr[i].f0.len) == 0) {
ret.example = settings->ptr[i].f1;
}
}
return ret;
}

bool exports_edgee_protocols_data_collection_page(exports_edgee_protocols_data_collection_event_t *e, exports_edgee_protocols_data_collection_dict_t *settings, exports_edgee_protocols_data_collection_edgee_request_t *ret, data_collection_string_t *err) {
struct component_settings parsed_settings = parse_settings(settings);
data_collection_string_dup(&ret->url, "https://example.com");

data_collection_string_dup(&ret->body, "{\"key\":\"value\"}");

ret->method = EXPORTS_EDGEE_PROTOCOLS_DATA_COLLECTION_HTTP_METHOD_POST;

ret->headers.ptr = malloc(sizeof(data_collection_tuple2_string_string_t));
ret->headers.len = 1;
ret->headers.ptr = malloc(2 * sizeof(data_collection_tuple2_string_string_t));
ret->headers.len = 2;
data_collection_string_dup(&ret->headers.ptr[0].f0, "Content-Type");
data_collection_string_dup(&ret->headers.ptr[0].f1, "application/json");

data_collection_string_dup(&ret->headers.ptr[1].f0, "Example");
ret->headers.ptr[1].f1 = parsed_settings.example;

ret->forward_client_headers = true;
return true;
}
bool exports_edgee_protocols_data_collection_track(exports_edgee_protocols_data_collection_event_t *e, exports_edgee_protocols_data_collection_dict_t *settings, exports_edgee_protocols_data_collection_edgee_request_t *ret, data_collection_string_t *err) {
struct component_settings parsed_settings = parse_settings(settings);
data_collection_string_dup(&ret->url, "https://example.com");

data_collection_string_dup(&ret->body, "{\"key\":\"value\"}");

ret->method = EXPORTS_EDGEE_PROTOCOLS_DATA_COLLECTION_HTTP_METHOD_POST;

ret->headers.ptr = malloc(sizeof(data_collection_tuple2_string_string_t));
ret->headers.len = 1;
ret->headers.ptr = malloc(2 * sizeof(data_collection_tuple2_string_string_t));
ret->headers.len = 2;
data_collection_string_dup(&ret->headers.ptr[0].f0, "Content-Type");
data_collection_string_dup(&ret->headers.ptr[0].f1, "application/json");

data_collection_string_dup(&ret->headers.ptr[1].f0, "Example");
ret->headers.ptr[1].f1 = parsed_settings.example;

ret->forward_client_headers = true;
return true;
}
bool exports_edgee_protocols_data_collection_user(exports_edgee_protocols_data_collection_event_t *e, exports_edgee_protocols_data_collection_dict_t *settings, exports_edgee_protocols_data_collection_edgee_request_t *ret, data_collection_string_t *err) {
struct component_settings parsed_settings = parse_settings(settings);
data_collection_string_dup(&ret->url, "https://example.com");

data_collection_string_dup(&ret->body, "{\"key\":\"value\"}");

ret->method = EXPORTS_EDGEE_PROTOCOLS_DATA_COLLECTION_HTTP_METHOD_POST;

ret->headers.ptr = malloc(sizeof(data_collection_tuple2_string_string_t));
ret->headers.len = 1;
ret->headers.ptr = malloc(2 * sizeof(data_collection_tuple2_string_string_t));
ret->headers.len = 2;
data_collection_string_dup(&ret->headers.ptr[0].f0, "Content-Type");
data_collection_string_dup(&ret->headers.ptr[0].f1, "application/json");

data_collection_string_dup(&ret->headers.ptr[1].f0, "Example");
ret->headers.ptr[1].f1 = parsed_settings.example;

ret->forward_client_headers = true;
return true;
}
10 changes: 7 additions & 3 deletions edgee-component.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
manifest_version = 1
manifest-version = 1

[package]
[component]
name = "example-c-component"
version = "1.0.0"
category = "data-collection"
Expand All @@ -10,6 +10,10 @@ documentation = "https://github.com/edgee-cloud/example-c-component"
repository = "https://github.com/edgee-cloud/example-c-component"
wit-world-version = "0.4.0"

[package.build]
[component.build]
command = "$(CC) dc_component.c internal/data_collection.c internal/data_collection_component_type.o -o /dev/stdout -mexec-model=reactor -Os | wasm-tools component new -o dc_component.wasm"
output_path = "./dc_component.wasm"

[component.settings.example]
title = "Example Config Field"
type = "string"

0 comments on commit b48d1e1

Please sign in to comment.