Skip to content

Commit

Permalink
Add logic to retrieve an environment variable for Client ID (#108)
Browse files Browse the repository at this point in the history
* Pull client ID from environment variable

* Update client ID error checking

* Allow options for setting client id value
  • Loading branch information
jmakhack authored Jun 20, 2023
1 parent f7d22de commit dfc8d60
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/mya.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#define MAX_USERNAME_LENGTH 16
#define MAX_ENDPOINT_LENGTH 15
#define PAGE_SIZE 1000
#define CLIENT_ID_ENV getenv("MYANIMELIST_CLIENT_ID")
#define CLIENT_ID "YOUR TOKEN HERE"

/* define constants for program options */
Expand Down Expand Up @@ -218,7 +219,13 @@ CURLcode curl_fetch_url (CURL *curl, const char *url, struct curl_fetch_st *fetc
size_t client_id_header_size = 50;
char client_id_header[client_id_header_size];
strlcpy(client_id_header, "X-MAL-CLIENT-ID:", client_id_header_size);
strlcat(client_id_header, CLIENT_ID, client_id_header_size);

/* if environment variable doesn't exist, use hard coded client id value */
if (CLIENT_ID_ENV == NULL) {
strlcat(client_id_header, CLIENT_ID, client_id_header_size);
} else {
strlcat(client_id_header, CLIENT_ID_ENV, client_id_header_size);
}

/* add client id header to request */
chunk = curl_slist_append(chunk, client_id_header);
Expand Down Expand Up @@ -439,8 +446,8 @@ void get_new_uri (char *uri, size_t uri_size, struct json_object *json) {
* returns: 0 if success, otherwise error number
*/
int main (int argc, char *argv[]) {
/* exit early if Client ID is not provided */
if (strcmp(CLIENT_ID, "YOUR TOKEN HERE") == 0) {
/* exit early if client id is not provided */
if (CLIENT_ID_ENV == NULL && strcmp(CLIENT_ID, "YOUR TOKEN HERE") == 0) {
fprintf(stderr, "Client ID has not been provided\n");
exit(EXIT_FAILURE);
}
Expand Down

0 comments on commit dfc8d60

Please sign in to comment.