Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename manifest fields, add example setting #5

Merged
merged 3 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Loading