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

improve API file structure #26

Open
sleepdefic1t opened this issue Jun 8, 2018 · 0 comments
Open

improve API file structure #26

sleepdefic1t opened this issue Jun 8, 2018 · 0 comments
Labels
enhancement New feature or request

Comments

@sleepdefic1t
Copy link
Contributor

sleepdefic1t commented Jun 8, 2018

Improve the file structure of API files in the Ark-Cpp wrapper

The aim of this issue is to open the discussion of how to reduce the complexity and count of the API files

objectives:

  • reduce complexity and overhead
  • improve readability
  • improve overall size of the Ark-Cpp wrapper

POC Implementation available in

https://github.com/sleepdefic1t/Ark-Cpp/tree/improvements_preview/src/api

The *_gettable classes have now been streamlined to the point where is appears ready for merging into the *able class prototypes/protocols.

This will leave each API "type" (Account, Block, etc) with only its implementation and applicable API Callback Models (*_respondable).

Also proposed for discussion is an improved naming convention for *_respondable's to better convey that they are indeed callback models.
ex account_model.h

Sample header/declaration implementation



#ifndef ACCOUNTABLE_H
#define ACCOUNTABLE_H

#include "utilities/platform.h"
#include "api/accountable/account_respondable.h"
#include "models/account.h"
#include "models/delegate.h"
#include "types/address.h"
#include "types/balance.h"
#include "types/crypto/eckey.h"
#include "utilities/connector.h"
#include "api/paths.h"
#include "utilities/json.h"

namespace ARK
{
namespace API
{
/*************************************************
*	PUBLIC: ARK::API::Accountable
*	public API::Account::Gettable
*	virtual ARK::Utilities::Network::Connectable
*    
*	API's Account Model
**************************************************/
class Accountable :
	virtual ARK::Utilities::Network::Connectable
{
    public:
	/*************************************************
	*	/api/accounts/getBalance?address=arkAddress
	*
	*	@param:	Address arkAddress
	*	@return:	ARK::API::Account::Respondable::Balances
	*
	*	@brief:	Uses Ark Address to get an Accounts Confirmed	and Unconfirmed Balances from a Node via API.
	**************************************************/
	ARK::API::Account::Respondable::Balances accountBalance(const Address &arkAddress);
	/*************************************************/

	/**************************************************************************************************/

	/*************************************************
	*	api/accounts/getPublickey?address=arkAddress
	*
	*	@param:	Address arkAddress
	*	@return:	Publickey
	*
	*	@brief:	Uses Ark Address to get an Accounts Publickey from a Node via API.
	**************************************************/
	Publickey accountPublickey(const Address &arkAddress);
	/*************************************************/

	/**************************************************************************************************/

	/*************************************************
	*	/api/accounts/delegates/fee?address=arkAddress
	*
	*	@param:	Address arkAddress
	*	@return:	Balance
	*
	*	@brief:	Uses Ark Address to get Delegate Registration Fee from a Node via API.
	**************************************************/
	Balance accountDelegatesFee(const Address &arkAddress);
	/*************************************************/

	/**************************************************************************************************/

	/*************************************************
	*	/api/accounts/delegates?address=arkAddress
	*
	*	@param:	Address arkAddress
	*	@return:	ARK::Delegate
	*
	*	@brief:	Uses Ark Address to get Delegate Object from a Node via API.
	**************************************************/
	ARK::Delegate accountDelegates(const Address &arkAddress);
	/*************************************************/

	/**************************************************************************************************/

	/*************************************************
	*	/api/accounts?address=arkAddress
	*
	*	@param:	Address arkAddress
	*	@return:	ARK::Account
	*
	*	@brief:	Uses Ark Address to get Account Object from a Node via API.
	**************************************************/
	ARK::Account account(const Address &arkAddress);
	/*************************************************/

};
/*************************************************/

};
};

Sample cpp/definition implementation



#include "api/accountable/accountable.h"


/*************************************************
*	/api/accounts/getBalance?address=arkAddress
*
*	EXAMPLE:
*	{
*		"success":true,
*		"balance":  "Balance",
*		"unconfirmedBalance": "Balance"
*	}
**************************************************/
ARK::API::Account::Respondable::Balances ARK::API::Accountable::accountBalance(
		const Address &arkAddress
)
{
	char uri[68 + 1] = { '\0' };
		strcpy(uri, ARK::API::Paths::Account::getBalance_s);
		strcat(uri, "?address=");
		strcat(uri, arkAddress.getValue());
	auto callback = netConnector.callback(uri);
	auto parser = ARK::Utilities::make_json_string(callback);
	return {
		parser->valueFor("balance").c_str(),
		parser->valueFor("unconfirmedBalance").c_str()
	};
};
/*************************************************/

/**************************************************************************************************/

/*************************************************
*	api/accounts/getPublickey?address=arkAddress
*
*	EXAMPLE:
*	{
*		"success":true,
*		"publicKey":  "Publickey"
*	}
**************************************************/
Publickey ARK::API::Accountable::accountPublickey(
		const Address &arkAddress
)
{
	char uri[94 + 1] = { '\0' };
		strcpy(uri, ARK::API::Paths::Account::getPublickey_s);
		strcat(uri, "?address=");
		strcat(uri, arkAddress.getValue());
	auto callback = netConnector.callback(uri);
	auto parser = ARK::Utilities::make_json_string(callback);
	return {
		parser->valueFor("publicKey").c_str()
	};
};
/*************************************************/

/**************************************************************************************************/

/*************************************************
*	/api/accounts/delegates/fee?address=arkAddress
*
*	EXAMPLE:
*	{
*		"success":true,
*		"fee":2500000000
*	}
**************************************************/
Balance ARK::API::Accountable::accountDelegatesFee(
		const Address &arkAddress
)
{
	char uri[95 + 1] = { '\0' };
		strcpy(uri, ARK::API::Paths::Account::delegatesFee_s);
		strcat(uri, "?address=");
		strcat(uri, arkAddress.getValue());
	auto callback = netConnector.callback(uri);
	auto parser = ARK::Utilities::make_json_string(callback);
	return Balance(parser->valueFor("fee").c_str());
};
/*************************************************/

/**************************************************************************************************/

/*************************************************
*	/api/accounts/delegates?address=arkAddress
*
*	EXAMPLE:
*	{
*		"success":true,
*		"delegates":
*		[
*			{
*				"username": "sleepdeficit",
*				"address":  "Address",
*				"publicKey":  "Publickey",
*				"vote": "Balance",
*				"producedblocks": const char*,
*				"missedblocks": String,
*				"rate": int,
*				"approval": double,
*				"productivity": double
*			}
*		]
*	}
**************************************************/
ARK::Delegate ARK::API::Accountable::accountDelegates(
		const Address &arkAddress
)
{
	char uri[91 + 1] = { '\0' };
		strcpy(uri, ARK::API::Paths::Account::delegates_s);
		strcat(uri, "?address=");
		strcat(uri, arkAddress.getValue());
	auto callback = netConnector.callback(uri);
	auto parser = ARK::Utilities::make_json_string(callback);
	return {
		parser->subarrayValueIn("delegates", 0, "username").c_str(),
		parser->subarrayValueIn("delegates", 0, "address").c_str(),
		parser->subarrayValueIn("delegates", 0, "publicKey").c_str(),
		parser->subarrayValueIn("delegates", 0, "vote").c_str(),
		convert_to_int(parser->subarrayValueIn("delegates", 0, "producedblocks").c_str()),
		convert_to_int(parser->subarrayValueIn("delegates", 0, "missedblocks").c_str()),
		convert_to_int(parser->subarrayValueIn("delegates", 0, "rate").c_str()),
		convert_to_float(parser->subarrayValueIn("delegates", 0, "approval").c_str()),
		convert_to_float(parser->subarrayValueIn("delegates", 0, "productivity").c_str())
	};
};
/*************************************************/

/**************************************************************************************************/

/*************************************************
*	/api/accounts?address=arkAddress
*
*	EXAMPLE:
*	{
*		"success":true,
*		"account":
*		{
*			"address":  "Address",
*			"unconfirmedBalance": "Balance",
*			"balance":  "Balance",
*			"publicKey":  "Publickey",
*			"unconfirmedSignature": int,
*			"secondSignature":  int,
*			"secondPublicKey":  "Publickey",
*			"multisignatures":[],
*			"u_multisignatures":[]
*		}
*	}
**************************************************/
ARK::Account ARK::API::Accountable::account(
		const Address &arkAddress
)
{
	char uri[81 + 1] = { '\0' };
		strcpy(uri, ARK::API::Paths::Account::accounts_s);
		strcat(uri, "?address=");
		strcat(uri, arkAddress.getValue());
	auto callback = netConnector.callback(uri);
	auto parser = ARK::Utilities::make_json_string(callback);
		/********************
		*	FIXME 
		* multisignatures & u_multisignatures returns an array of Transaction ID's (Hash type)
		********************/
	return {
		parser->valueIn("account", "address").c_str(),
		parser->valueIn("account", "unconfirmedBalance").c_str(),
		parser->valueIn("account", "balance").c_str(),
		parser->valueIn("account", "publicKey").c_str(),
		convert_to_int(parser->valueIn("account", "unconfirmedSignature").c_str()),
		convert_to_int(parser->valueIn("account", "secondSignature").c_str()),
		parser->valueIn("account", "secondPublicKey").c_str(),
		// parser->subarrayValueIn("account", 0, "multisignatures").c_str(),	//	FIXME
		// multisigsArray,																										//	FIXME
		// parser->subarrayValueIn("account", 0, "u_multisignatures").c_str()	//	FIXME
		// u_multisigsArray																										//	FIXME
	};
};
/*************************************************/
@sleepdefic1t sleepdefic1t added the enhancement New feature or request label Jun 8, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant