Skip to content

Commit

Permalink
Got rid of property ::stream in struct discord_api_request_t
Browse files Browse the repository at this point in the history
  • Loading branch information
abobija committed Aug 14, 2022
1 parent b62d572 commit a83945c
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 30 deletions.
8 changes: 3 additions & 5 deletions include/discord/private/_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ typedef struct {
} discord_api_multipart_t;

typedef struct {
esp_http_client_method_t method;
char* uri;
discord_api_multipart_t** multiparts;
uint8_t multiparts_len;
bool stream_response;
bool disable_auto_uri_free;
bool disable_auto_payload_free;
} discord_api_request_t;
Expand All @@ -51,19 +49,19 @@ esp_err_t dcapi_download(discord_handle_t client, const char* url, discord_downl
*
* @note data will be automatically freed
*/
esp_err_t dcapi_get(discord_handle_t client, char* uri, char* data, bool stream, discord_api_response_t** out_response);
esp_err_t dcapi_get(discord_handle_t client, char* uri, char* data, discord_api_response_t** out_response);
/**
* POST request
*
* @note data will be automatically freed
*/
esp_err_t dcapi_post(discord_handle_t client, char* uri, char* data, bool stream, discord_api_response_t** out_response);
esp_err_t dcapi_post(discord_handle_t client, char* uri, char* data, discord_api_response_t** out_response);
/**
* PUT request
*
* @note data will be automatically freed
*/
esp_err_t dcapi_put(discord_handle_t client, char* uri, char* data, bool stream, discord_api_response_t** out_response);
esp_err_t dcapi_put(discord_handle_t client, char* uri, char* data, discord_api_response_t** out_response);

esp_err_t dcapi_destroy(discord_handle_t client);

Expand Down
2 changes: 1 addition & 1 deletion src/discord/guild.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ esp_err_t discord_guild_get_channels(discord_handle_t client, discord_guild_t* g
esp_err_t err = ESP_OK;
discord_api_response_t* res = NULL;

if((err = dcapi_get(client, estr_cat("/guilds/", guild->id, "/channels"), NULL, true, &res)) != ESP_OK) {
if((err = dcapi_get(client, estr_cat("/guilds/", guild->id, "/channels"), NULL, &res)) != ESP_OK) {
DISCORD_LOGE("Fail to fetch channels");
return err;
}
Expand Down
1 change: 0 additions & 1 deletion src/discord/member.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ esp_err_t discord_member_get(discord_handle_t client, char* guild_id, char* user
client,
estr_cat("/guilds/", guild_id, "/members/", user_id),
NULL,
true,
&res
)) != ESP_OK) {
return err;
Expand Down
3 changes: 1 addition & 2 deletions src/discord/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ esp_err_t discord_message_send(discord_handle_t client, discord_message_t* messa
client,
estr_cat("/channels/", message->channel_id, "/messages"),
discord_json_serialize(message),
out_result != NULL,
&res
);

Expand Down Expand Up @@ -51,7 +50,7 @@ esp_err_t discord_message_react(discord_handle_t client, discord_message_t* mess
}

char* _emoji = estr_url_encode(emoji);
esp_err_t err = dcapi_put(client, estr_cat("/channels/", message->channel_id, "/messages/", message->id, "/reactions/", _emoji, "/@me"), NULL, false, NULL);
esp_err_t err = dcapi_put(client, estr_cat("/channels/", message->channel_id, "/messages/", message->id, "/reactions/", _emoji, "/@me"), NULL, NULL);
free(_emoji);

return err;
Expand Down
32 changes: 13 additions & 19 deletions src/discord/private/_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,10 @@ static int dcapi_calculate_request_length(discord_api_request_t* request)
return length;
}

static esp_err_t dcapi_request(discord_handle_t client, discord_api_request_t* request, discord_api_response_t** out_response) {
static esp_err_t dcapi_request(discord_handle_t client, esp_http_client_method_t method, discord_api_request_t* request, discord_api_response_t** out_response) {
DISCORD_LOG_FOO();

bool stream_response = request->stream_response;

if(! out_response) { // overwrite stream_response if response reference is null
stream_response = false;
}
bool stream_response = out_response != NULL;

esp_err_t err;

Expand Down Expand Up @@ -247,7 +243,7 @@ static esp_err_t dcapi_request(discord_handle_t client, discord_api_request_t* r
// todo: error check
free(url);

esp_http_client_set_method(http, request->method);
esp_http_client_set_method(http, method);
// todo: error check

int len = dcapi_calculate_request_length(request);
Expand Down Expand Up @@ -443,12 +439,10 @@ static void discord_api_request_free(discord_api_request_t* request)
free(request);
}

static discord_api_request_t* dcapi_create_request(esp_http_client_method_t method, char* uri, char* payload, bool stream)
static discord_api_request_t* dcapi_create_request(char* uri, char* payload)
{
discord_api_request_t* request = cu_ctor(discord_api_request_t,
.method = method,
.uri = uri,
.stream_response = stream,
);

if(payload) {
Expand All @@ -463,25 +457,25 @@ static discord_api_request_t* dcapi_create_request(esp_http_client_method_t meth
return request;
}

esp_err_t dcapi_get(discord_handle_t client, char* uri, char* payload, bool stream, discord_api_response_t** out_response) {
discord_api_request_t* request = dcapi_create_request(HTTP_METHOD_GET, uri, payload, stream);
esp_err_t err = dcapi_request(client, request, out_response);
esp_err_t dcapi_get(discord_handle_t client, char* uri, char* payload, discord_api_response_t** out_response) {
discord_api_request_t* request = dcapi_create_request(uri, payload);
esp_err_t err = dcapi_request(client, HTTP_METHOD_GET, request, out_response);
discord_api_request_free(request);

return err;
}

esp_err_t dcapi_post(discord_handle_t client, char* uri, char* payload, bool stream, discord_api_response_t** out_response) {
discord_api_request_t* request = dcapi_create_request(HTTP_METHOD_POST, uri, payload, stream);
esp_err_t err = dcapi_request(client, request, out_response);
esp_err_t dcapi_post(discord_handle_t client, char* uri, char* payload, discord_api_response_t** out_response) {
discord_api_request_t* request = dcapi_create_request(uri, payload);
esp_err_t err = dcapi_request(client, HTTP_METHOD_POST, request, out_response);
discord_api_request_free(request);

return err;
}

esp_err_t dcapi_put(discord_handle_t client, char* uri, char* payload, bool stream, discord_api_response_t** out_response) {
discord_api_request_t* request = dcapi_create_request(HTTP_METHOD_PUT, uri, payload, stream);
esp_err_t err = dcapi_request(client, request, out_response);
esp_err_t dcapi_put(discord_handle_t client, char* uri, char* payload, discord_api_response_t** out_response) {
discord_api_request_t* request = dcapi_create_request(uri, payload);
esp_err_t err = dcapi_request(client, HTTP_METHOD_PUT, request, out_response);
discord_api_request_free(request);

return err;
Expand Down
2 changes: 1 addition & 1 deletion src/discord/role.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ esp_err_t discord_role_get_all(discord_handle_t client, const char* guild_id, di
esp_err_t err = ESP_OK;
discord_api_response_t* res = NULL;

if((err = dcapi_get(client, estr_cat("/guilds/", guild_id, "/roles"), NULL, true, &res)) != ESP_OK) {
if((err = dcapi_get(client, estr_cat("/guilds/", guild_id, "/roles"), NULL, &res)) != ESP_OK) {
DISCORD_LOGE("Fail to fetch roles");
return err;
}
Expand Down
2 changes: 1 addition & 1 deletion src/discord/user.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ esp_err_t discord_user_get_my_guilds(discord_handle_t client, discord_guild_t***
esp_err_t err = ESP_OK;
discord_api_response_t* res = NULL;

if((err = dcapi_get(client, strdup("/users/@me/guilds"), NULL, true, &res)) != ESP_OK) {
if((err = dcapi_get(client, strdup("/users/@me/guilds"), NULL, &res)) != ESP_OK) {
DISCORD_LOGE("Fail to fetch guilds");
return err;
}
Expand Down

0 comments on commit a83945c

Please sign in to comment.