Created
August 30, 2023 09:00
-
-
Save ashraf267/b0c74b3baa254cc98029211def216df5 to your computer and use it in GitHub Desktop.
Shows "Api call failed" to the screen
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
short makeApiCall(char *url, unsigned char *requestData, unsigned short requestDataLength) | |
{ | |
short sRC; | |
sRC = UIWireHttpSend(url, requestData, requestDataLength, NULL); | |
// sRC = UIWireHttpSend(url, requestData, requestDataLength, "Content-type: application/json; charset=UTF-8"); | |
return sRC; | |
// if (sRC != TY_OK) | |
// { | |
// return sRC; // failed, I think | |
// } | |
// else | |
// { | |
// return TY_OK; // went well! | |
// } | |
} | |
short receiveApiResponse(unsigned char *responseData, unsigned short *responseLength) | |
{ | |
short sRC; | |
sRC = UIWireHttpRecv(responseData, responseLength); | |
return sRC; | |
} | |
// const char *FetchWallet() | |
char *FetchWallet() | |
{ | |
// const char *json = "{\"name\": \"John\", \"age\": 30, \"address\": {\"street\": \"123 Main St\", \"city\": \"Example City\"}}"; | |
// const char *json = "{\"isSuccessful\": true, \"requestModel\": {\"requestHash\": \"363P2a88274de10b4bfc8ddc94e7a57b62b5\", \"emailAddress\": null}, \"message\": \"A verification OTP has been sent to your mobile number 345728.\", \"statusCode\": null}"; | |
// my code | |
// char url[] = "https://devapi.ucard.store/identity/Agent/login/wallet/initate"; | |
char url[] = "https://jsonplaceholder.typicode.com/posts/1"; | |
// unsigned char requestData[] = "{\"walletId\": \"5149\"}"; | |
// unsigned char requestData[] = "{\"title\": \"foo\", \"body\": \"bar\", \"userId\": 1}"; | |
// unsigned short requestDataLength = strlen((char *)requestData); | |
// call API | |
short sRC = makeApiCall(url, NULL, 4000); | |
if (sRC != TY_OK) | |
{ | |
return "Api call failed!"; | |
TRACE("Unable to make call to API. sRC is: %d", sRC); | |
} | |
// unsigned char responseBuffer[4096]; | |
const char responseBuffer[4096]; | |
unsigned short responseLength = sizeof(responseBuffer); | |
// recv data | |
sRC = receiveApiResponse((uint8_t *)responseBuffer, &responseLength); | |
if (sRC == TY_OK) | |
{ | |
// responseBuffer[responseLength] = '\0'; | |
// I think jsmn op should start here... | |
// Initialize jsmn parser | |
jsmn_parser parser; | |
jsmn_init(&parser); | |
// Estimate the number of tokens needed for parsing | |
int num_tokens = 20; | |
jsmntok_t tokens[num_tokens]; | |
// Parse the JSON input | |
int ret = jsmn_parse(&parser, responseBuffer, strlen(responseBuffer), tokens, num_tokens); | |
if (ret < 0) | |
{ | |
printf("Error parsing JSON: %d\n", ret); | |
return NULL; | |
} | |
jsmntok_t *value = NULL; // Declare the value variable outside the inner loop | |
// char *myName = NULL; // Declare the variable to store the name value | |
char *myHash = NULL; | |
// Iterate through the JSON tokens | |
for (int i = 0; i < ret; i++) | |
{ | |
jsmntok_t *token = &tokens[i]; | |
// Check if token is an object | |
if (token->type == JSMN_OBJECT) | |
{ | |
// Iterate through the object's key-value pairs | |
for (int j = i + 1; j < ret; j++) | |
{ | |
jsmntok_t *key = &tokens[j]; | |
value = &tokens[j + 1]; // Assign value here | |
// Check if key is a string | |
if (key->type == JSMN_STRING) | |
{ | |
char key_str[key->end - key->start + 1]; | |
strncpy(key_str, responseBuffer + key->start, key->end - key->start); | |
key_str[key->end - key->start] = '\0'; | |
// Check if value is a string | |
if (value->type == JSMN_STRING) | |
{ | |
char value_str[value->end - value->start + 1]; | |
strncpy(value_str, responseBuffer + value->start, value->end - value->start); | |
value_str[value->end - value->start] = '\0'; | |
// Check if the key is "name" | |
if (strcmp(key_str, "title") == 0) | |
{ | |
myHash = strdup(value_str); // Allocate memory and copy value | |
break; // Exit the inner loop once we find the name | |
} | |
} | |
} | |
// Skip the value token | |
j++; | |
} | |
// Move the main index to the end of the object | |
i = value->end; | |
} | |
} | |
return myHash; | |
} | |
// another way | |
// char *pBuf = "{\"walletId\": \"5149\"}"; | |
// uint16_t uLen = strlen(pBuf); | |
// char *pExtHeader = "Content-Type: application/json\r\n"; | |
// short sRC = UIWireHttpSend(url, (uint8_t *)pBuf, uLen, pExtHeader); | |
// if (!(sRC == 0)) | |
// { | |
// return NULL; | |
// } | |
// int u_len = 250; | |
// const char response[u_len]; | |
// sRC = UIWireHttpRecv((uint8_t *)response, (uint16_t *)u_len); | |
// // Initialize jsmn parser | |
// jsmn_parser parser; | |
// jsmn_init(&parser); | |
// // Estimate the number of tokens needed for parsing | |
// int num_tokens = 20; | |
// jsmntok_t tokens[num_tokens]; | |
// // Parse the JSON input | |
// int ret = jsmn_parse(&parser, response, strlen(response), tokens, num_tokens); | |
// if (ret < 0) | |
// { | |
// printf("Error parsing JSON: %d\n", ret); | |
// return NULL; | |
// } | |
// jsmntok_t *value = NULL; // Declare the value variable outside the inner loop | |
// // char *myName = NULL; // Declare the variable to store the name value | |
// char *myHash = NULL; | |
// // Iterate through the JSON tokens | |
// for (int i = 0; i < ret; i++) | |
// { | |
// jsmntok_t *token = &tokens[i]; | |
// // Check if token is an object | |
// if (token->type == JSMN_OBJECT) | |
// { | |
// // Iterate through the object's key-value pairs | |
// for (int j = i + 1; j < ret; j++) | |
// { | |
// jsmntok_t *key = &tokens[j]; | |
// value = &tokens[j + 1]; // Assign value here | |
// // Check if key is a string | |
// if (key->type == JSMN_STRING) | |
// { | |
// char key_str[key->end - key->start + 1]; | |
// strncpy(key_str, response + key->start, key->end - key->start); | |
// key_str[key->end - key->start] = '\0'; | |
// // Check if value is a string | |
// if (value->type == JSMN_STRING) | |
// { | |
// char value_str[value->end - value->start + 1]; | |
// strncpy(value_str, response + value->start, value->end - value->start); | |
// value_str[value->end - value->start] = '\0'; | |
// // Check if the key is "name" | |
// // if (strcmp(key_str, "name") == 0) { | |
// // myName = strdup(value_str); // Allocate memory and copy value | |
// // break; // Exit the inner loop once we find the name | |
// // } | |
// // Check if the key is "name" | |
// if (strcmp(key_str, "requestHash") == 0) | |
// { | |
// myHash = strdup(value_str); // Allocate memory and copy value | |
// break; // Exit the inner loop once we find the name | |
// } | |
// } | |
// } | |
// // Skip the value token | |
// j++; | |
// } | |
// // Move the main index to the end of the object | |
// i = value->end; | |
// } | |
// } | |
// return myName; | |
return "jkkash180402"; // as reqHash if it fails | |
} | |
short TestScreen(int iTimeout) | |
{ | |
short sRC; | |
const char *myReqHash = FetchWallet(); | |
Lib_LcdDrowArea(DEFAULT_FORG_COLOR, 0, 30, 320, 210); | |
while (1) | |
{ | |
// DRAW_TITLE("Test API"); | |
if (myReqHash == NULL) | |
{ | |
DRAW_TITLE("No Response!"); | |
} | |
else | |
{ | |
DRAW_TITLE(myReqHash); | |
} | |
sRC = UIWaitKey(iTimeout, 1); | |
if (UI_INPUT_TIMEOUT == sRC || UI_INPUT_CANCEL == sRC) | |
{ | |
return sRC; | |
} | |
else | |
{ | |
continue; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment