Skip to content
This repository has been archived by the owner on Dec 26, 2022. It is now read-only.

Commit

Permalink
feat(api): Add find single txn obj back
Browse files Browse the repository at this point in the history
function `api_find_transaction_object_single()` allows client to use get request to
search for one transacion object with transaction hash each time.
  • Loading branch information
howjmay committed Jul 31, 2019
1 parent b075db0 commit de09698
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 21 deletions.
44 changes: 32 additions & 12 deletions accelerator/apis.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ int apis_logger_release() {
status_t api_get_tips(const iota_client_service_t* const service, char** json_result) {
status_t ret = SC_OK;
get_tips_res_t* res = get_tips_res_new();
char_buffer_t* res_buff = char_buffer_new();

if (res == NULL || res_buff == NULL) {
if (res == NULL) {
ret = SC_CCLIENT_OOM;
log_error(apis_logger_id, "[%s:%d:%s]\n", __func__, __LINE__, "SC_CCLIENT_OOM");
goto done;
Expand All @@ -42,23 +41,15 @@ status_t api_get_tips(const iota_client_service_t* const service, char** json_re
goto done;
}

if (service->serializer.vtable.get_tips_serialize_response(res, res_buff) != RC_OK) {
ret = SC_CCLIENT_JSON_PARSE;
log_error(apis_logger_id, "[%s:%d:%s]\n", __func__, __LINE__, "SC_CCLIENT_JSON_PARSE");
goto done;
}

*json_result = (char*)malloc((res_buff->length + 1) * sizeof(char));
if (*json_result == NULL) {
ret = ta_get_tips_res_serialize(json_result, res);
if (ret != SC_OK) {
ret = SC_CCLIENT_JSON_PARSE;
log_error(apis_logger_id, "[%s:%d:%s]\n", __func__, __LINE__, "SC_CCLIENT_JSON_PARSE");
goto done;
}
snprintf(*json_result, (res_buff->length + 1), "%s", res_buff->data);

done:
get_tips_res_free(&res);
char_buffer_free(res_buff);
return ret;
}

Expand Down Expand Up @@ -168,6 +159,35 @@ status_t api_find_transactions(const iota_client_service_t* const service, const
return ret;
}

status_t api_find_transaction_object_single(const iota_client_service_t* const service, const char* const obj,
char** json_result) {
status_t ret = SC_OK;
flex_trit_t txn_hash[NUM_FLEX_TRITS_HASH];
ta_find_transaction_objects_req_t* req = ta_find_transaction_objects_req_new();
transaction_array_t* res = transaction_array_new();
if (req == NULL || res == NULL) {
ret = SC_TA_OOM;
log_error(apis_logger_id, "[%s:%d:%s]\n", __func__, __LINE__, "SC_TA_OOM");
goto done;
}

flex_trits_from_trytes(txn_hash, NUM_TRITS_HASH, (const tryte_t*)obj, NUM_TRYTES_HASH, NUM_TRYTES_HASH);
hash243_queue_push(&req->hashes, txn_hash);

ret = ta_find_transaction_objects(service, req, res);
if (ret) {
log_error(apis_logger_id, "[%s:%d]\n", __func__, __LINE__);
goto done;
}

ret = ta_find_transaction_object_single_res_serialize(json_result, res);

done:
ta_find_transaction_objects_req_free(&req);
transaction_array_free(res);
return ret;
}

status_t api_find_transaction_objects(const iota_client_service_t* const service, const char* const obj,
char** json_result) {
status_t ret = SC_OK;
Expand Down
27 changes: 22 additions & 5 deletions accelerator/apis.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,23 @@ status_t api_send_transfer(const iota_config_t* const tangle, const iota_client_
*/
status_t api_find_transactions(const iota_client_service_t* const service, const char* const obj, char** json_result);

/**
* @brief Return transaction object with given single transaction hash.
*
* Explore transaction hash information with given single transaction hash. This would
* return whole transaction object details in json format instead of raw trytes.
*
* @param[in] service IRI node end point service
* @param[in] obj transaction hash in trytes
* @param[out] json_result Result containing the only one transaction object in json format
*
* @return
* - SC_OK on success
* - non-zero on error
*/
status_t api_find_transaction_object_single(const iota_client_service_t* const service, const char* const obj,
char** json_result);

/**
* @brief Return transaction object with given transaction hash.
*
Expand All @@ -179,14 +196,14 @@ status_t api_find_transaction_objects(const iota_client_service_t* const service
char** json_result);

/**
* @brief Return list of transaction hash with given tag hash.
* @brief Return list of transaction hash with given tag.
*
* Retreive all transactions that have same given tag. The result is a list of
* transaction hash in json format.
* transaction hashes in json format.
*
* @param[in] service IRI node end point service
* @param[in] obj tag in trytes
* @param[out] json_result Result containing list of transaction hash in json
* @param[out] json_result Result containing list of transaction hashes in json
* format
*
* @return
Expand All @@ -197,14 +214,14 @@ status_t api_find_transactions_by_tag(const iota_client_service_t* const service
char** json_result);

/**
* @brief Return list of transaction object with given tag hash.
* @brief Return list of transaction objects with given tag.
*
* Retreive all transactions that have same given tag. The result is a list of
* transaction objects in json format.
*
* @param[in] service IRI node end point service
* @param[in] obj tag in trytes
* @param[out] json_result Result containing list of transaction object in json
* @param[out] json_result Result containing list of transaction objects in json
* format
*
* @return
Expand Down
2 changes: 1 addition & 1 deletion accelerator/common_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ status_t ta_find_transactions_obj_by_tag(const iota_client_service_t* const serv
find_transactions_res_t* txn_res = find_transactions_res_new();
ta_find_transaction_objects_req_t* obj_req = ta_find_transaction_objects_req_new();
if (txn_res == NULL || obj_req == NULL) {
ret = SC_TA_OOM;
log_error(cc_logger_id, "[%s:%d:%s]\n", __func__, __LINE__, "SC_TA_OOM");
goto done;
}

Expand Down
26 changes: 23 additions & 3 deletions accelerator/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ int main(int argc, char* argv[]) {
});

/**
* @method {post} /transaction/object Find transaction hash
* @method {post} /transaction/hash Find transaction hash
*
* @return {String[]} hash Transaction hash
*/
Expand Down Expand Up @@ -174,6 +174,26 @@ int main(int argc, char* argv[]) {
res << json_result;
});

/**
* @method {get} /transaction/<transaction hash> Find transaction object with get request
*
* @return {String[]} hash Transaction object
*/
mux.handle("/transaction/{hash:[A-Z9]{81}}")
.method(served::method::OPTIONS,
[&](served::response& res, const served::request& req) { set_method_header(res, HTTP_METHOD_OPTIONS); })
.get([&](served::response& res, const served::request& req) {
status_t ret = SC_OK;
char* json_result = NULL;

ret = api_find_transaction_object_single(&ta_core.service, req.params["hash"].c_str(), &json_result);
ret = set_response_content(ret, &json_result);

set_method_header(res, HTTP_METHOD_GET);
res.set_status(ret);
res << json_result;
});

/**
* @method {post} /transaction/object Find transaction object
*
Expand Down Expand Up @@ -244,7 +264,7 @@ int main(int argc, char* argv[]) {
/**
* @method {get} /address Generate an unused address
*
* @return {String} hash of address hashes
* @return {String} address hashes
*/
mux.handle("/address")
.method(served::method::OPTIONS,
Expand All @@ -263,7 +283,7 @@ int main(int argc, char* argv[]) {
/**
* @method {get} /tag/<transaction tag>/hashes Find transaction hash with tag
*
* @return {String} hash of address hashes
* @return {String} address hashes
*/
mux.handle("/tag/{tag:[A-Z9]{27}}/hashes")
.method(served::method::OPTIONS,
Expand Down
1 change: 1 addition & 0 deletions serializer/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ cc_library(
"//response",
"//utils:fill_nines",
"@cJSON",
"@entangled//cclient/response:responses",
"@entangled//common/trinary:flex_trit",
"@entangled//common/trinary:tryte_ascii",
"@entangled//utils:char_buffer",
Expand Down
49 changes: 49 additions & 0 deletions serializer/serializer.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,31 @@ status_t ta_generate_address_res_serialize(char** obj, const ta_generate_address
return ret;
}

status_t ta_get_tips_res_serialize(char** obj, const get_tips_res_t* const res) {
status_t ret = SC_OK;

cJSON* json_root = cJSON_CreateArray();
if (json_root == NULL) {
log_error(seri_logger_id, "[%s:%d:%s]\n", __func__, __LINE__, "SC_SERIALIZER_JSON_CREATE");
return RC_CCLIENT_JSON_CREATE;
}

ret = ta_hash243_stack_to_json_array(res->hashes, json_root);
if (ret) {
goto err;
}

*obj = cJSON_PrintUnformatted(json_root);
if (*obj == NULL) {
log_error(seri_logger_id, "[%s:%d:%s]\n", __func__, __LINE__, "SC_SERIALIZER_JSON_PARSE");
return SC_SERIALIZER_JSON_PARSE;
}

err:
cJSON_Delete(json_root);
return ret;
}

status_t ta_send_transfer_req_deserialize(const char* const obj, ta_send_transfer_req_t* req) {
if (obj == NULL) {
log_error(seri_logger_id, "[%s:%d:%s]\n", __func__, __LINE__, "SC_SERIALIZER_NULL");
Expand Down Expand Up @@ -482,6 +507,30 @@ status_t ta_find_transaction_objects_req_deserialize(const char* const obj,
return ret;
}

status_t ta_find_transaction_object_single_res_serialize(char** obj, transaction_array_t* res) {
status_t ret = SC_OK;
cJSON* json_root = cJSON_CreateObject();
if (json_root == NULL) {
log_error(seri_logger_id, "[%s:%d:%s]\n", __func__, __LINE__, "SC_CCLIENT_JSON_CREATE");
return SC_CCLIENT_JSON_CREATE;
}

ret = iota_transaction_to_json_object(transaction_array_at(res, 0), &json_root);
if (ret != SC_OK) {
goto done;
}

*obj = cJSON_PrintUnformatted(json_root);
if (*obj == NULL) {
ret = SC_SERIALIZER_JSON_PARSE;
log_error(seri_logger_id, "[%s:%d:%s]\n", __func__, __LINE__, "SC_SERIALIZER_JSON_PARSE");
}

done:
cJSON_Delete(json_root);
return ret;
}

status_t ta_find_transaction_objects_res_serialize(char** obj, const transaction_array_t* const res) {
status_t ret = SC_OK;
cJSON* json_root = cJSON_CreateArray();
Expand Down
25 changes: 25 additions & 0 deletions serializer/serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define SERIALIZER_SERIALIZER_H_

#include "cJSON.h"
#include "cclient/response/responses.h"
#include "common/trinary/tryte_ascii.h"
#include "request/request.h"
#include "response/response.h"
Expand Down Expand Up @@ -67,6 +68,18 @@ int serializer_logger_release();
*/
status_t ta_generate_address_res_serialize(char** obj, const ta_generate_address_res_t* const res);

/**
* @brief Serialze object `get_tips_res_t` into JSON
*
* @param[in] res Result `get_tips_res_t` object with tips inside
* @param[out] obj Output tips in JSON
*
* @return
* - SC_OK on success
* - non-zero on error
*/
status_t ta_get_tips_res_serialize(char** obj, const get_tips_res_t* const res);

/**
* @brief Deserialze JSON string to type of ta_send_transfer_req_t
*
Expand Down Expand Up @@ -104,6 +117,18 @@ status_t ta_send_trytes_req_deserialize(const char* const obj, hash8019_array_p
*/
status_t ta_send_trytes_res_serialize(const hash8019_array_p trytes, char** obj);

/**
* @brief Serialze response of api_transaction_object_single into JSON
*
* @param[in] res Transaction object array, but we take only the first one
* @param[out] obj Result of serialization in JSON format.
*
* @return
* - SC_OK on success
* - non-zero on error
*/
status_t ta_find_transaction_object_single_res_serialize(char** obj, transaction_array_t* res);

/**
* @brief Deserialze type of ta_find_transaction_objects_req_t from JSON string
*
Expand Down

0 comments on commit de09698

Please sign in to comment.