Created
August 28, 2023 23:01
-
-
Save ashraf267/e97273be461e0455d37b02335ec93243 to your computer and use it in GitHub Desktop.
This really looks promising
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
| #include "Ui.h" | |
| #include "Common.h" | |
| #include "Communication.h" | |
| #include "jsmn.h" | |
| 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\r\n"); | |
| 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); | |
| if (sRC != TY_OK) | |
| { | |
| return sRC; // failed, I think | |
| } | |
| else | |
| { | |
| responseData[sizeof(responseLength)] = '\0'; // Null-terminate the response for string operations | |
| return TY_OK; // went well! | |
| } | |
| } | |
| const 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 | |
| short sRC; | |
| char url[] = "https://devapi.ucard.store/identity/Agent/login/wallet/initate"; | |
| unsigned char requestData[] = "{\"walletId\": \"5149\"}"; | |
| unsigned short requestDataLength = strlen((char *)requestData); | |
| // call API | |
| sRC = makeApiCall(url, requestData, requestDataLength); | |
| unsigned char responseBuffer[4096]; | |
| unsigned short responseLength = sizeof(responseBuffer); | |
| // recv data | |
| sRC = receiveApiResponse(responseBuffer, &responseLength); | |
| // 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, "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 myHash; | |
| } | |
| 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