二期c++接口
Dependencies: EthernetInterface mbed-rtos mbed uniqueCPUID
Fork of bcsdk by
Account.cpp@17:82d09b5a9189, 2018-06-08 (annotated)
- Committer:
- MrAI
- Date:
- Fri Jun 08 01:40:23 2018 +0000
- Revision:
- 17:82d09b5a9189
- Parent:
- 15:7b56d49392be
????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
webmaster | 10:aabd720e632c | 1 | #include "Account.h" |
webmaster | 10:aabd720e632c | 2 | #include "Uuid.h" |
webmaster | 10:aabd720e632c | 3 | |
webmaster | 10:aabd720e632c | 4 | |
webmaster | 10:aabd720e632c | 5 | Account::Account() |
webmaster | 10:aabd720e632c | 6 | { |
MrAI | 15:7b56d49392be | 7 | // cout << "Account Construction...begin" << endl; |
webmaster | 10:aabd720e632c | 8 | this->id = ""; |
webmaster | 10:aabd720e632c | 9 | this->alias = ""; |
webmaster | 10:aabd720e632c | 10 | //this->tags = "{}"; |
webmaster | 10:aabd720e632c | 11 | this->quorum = 1; |
MrAI | 15:7b56d49392be | 12 | // cout << "Account Construction...end" << endl; |
webmaster | 10:aabd720e632c | 13 | } |
webmaster | 10:aabd720e632c | 14 | |
webmaster | 10:aabd720e632c | 15 | Account::Account(Key &key) |
webmaster | 10:aabd720e632c | 16 | { |
webmaster | 10:aabd720e632c | 17 | this->id = ""; |
webmaster | 10:aabd720e632c | 18 | this->alias = ""; |
webmaster | 10:aabd720e632c | 19 | //this->tags = "{}"; |
webmaster | 10:aabd720e632c | 20 | this->quorum = 1; |
webmaster | 10:aabd720e632c | 21 | this->m_Keys = key; |
webmaster | 10:aabd720e632c | 22 | } |
webmaster | 10:aabd720e632c | 23 | |
webmaster | 10:aabd720e632c | 24 | Account::~Account() |
webmaster | 10:aabd720e632c | 25 | { |
webmaster | 10:aabd720e632c | 26 | |
webmaster | 10:aabd720e632c | 27 | } |
webmaster | 10:aabd720e632c | 28 | |
webmaster | 8:f2a567ee3a46 | 29 | |
webmaster | 10:aabd720e632c | 30 | string Account::listAccounts() |
webmaster | 8:f2a567ee3a46 | 31 | { |
webmaster | 10:aabd720e632c | 32 | string strJson = ""; |
webmaster | 10:aabd720e632c | 33 | |
webmaster | 10:aabd720e632c | 34 | MbedJSONValue val; |
webmaster | 10:aabd720e632c | 35 | |
webmaster | 10:aabd720e632c | 36 | //fill the object |
webmaster | 10:aabd720e632c | 37 | if (!this->id.empty()) |
webmaster | 10:aabd720e632c | 38 | { |
webmaster | 10:aabd720e632c | 39 | string filter = "id='" + this->id +"'"; |
webmaster | 10:aabd720e632c | 40 | val["filter"] = filter; |
webmaster | 10:aabd720e632c | 41 | } |
webmaster | 8:f2a567ee3a46 | 42 | |
webmaster | 10:aabd720e632c | 43 | //serialize it into a JSON string |
webmaster | 10:aabd720e632c | 44 | strJson = val.serialize(); |
webmaster | 10:aabd720e632c | 45 | if ("null" == strJson) |
webmaster | 10:aabd720e632c | 46 | { |
webmaster | 10:aabd720e632c | 47 | strJson = "{}"; |
webmaster | 10:aabd720e632c | 48 | } |
webmaster | 10:aabd720e632c | 49 | //printf("json: %s\r\n", strJson.c_str()); |
webmaster | 8:f2a567ee3a46 | 50 | |
webmaster | 10:aabd720e632c | 51 | string strRep = ""; |
webmaster | 10:aabd720e632c | 52 | string strUrl = "/list-accounts"; |
webmaster | 10:aabd720e632c | 53 | client.http_post(strUrl,strJson,strRep); |
webmaster | 8:f2a567ee3a46 | 54 | |
webmaster | 10:aabd720e632c | 55 | return strJson; |
MrAI | 12:1b24ea479a59 | 56 | } |
MrAI | 12:1b24ea479a59 | 57 | |
MrAI | 15:7b56d49392be | 58 | string Account::getBalance(MbedJSONValue &in_out,string strWallet) |
MrAI | 12:1b24ea479a59 | 59 | { |
MrAI | 12:1b24ea479a59 | 60 | //https://rest-beta.sdchain.io/v1/accounts/balances/6faLUhmp9gNgS9jXS3rAWbg8hFfi9PbWH5 |
MrAI | 12:1b24ea479a59 | 61 | string strRep = ""; |
MrAI | 15:7b56d49392be | 62 | //string strWallet = "69Hu8QsnEZUP4f3AVYSW7rkpLVm6DAEq9y"; |
MrAI | 12:1b24ea479a59 | 63 | string strUrl = "/v1/accounts/balances/" + strWallet; |
MrAI | 12:1b24ea479a59 | 64 | client.http_get(strUrl, "", strRep); |
MrAI | 12:1b24ea479a59 | 65 | string content = client.get_content(strRep); |
MrAI | 15:7b56d49392be | 66 | if (content.empty()) |
MrAI | 15:7b56d49392be | 67 | { |
MrAI | 15:7b56d49392be | 68 | return ""; |
MrAI | 15:7b56d49392be | 69 | } |
MrAI | 15:7b56d49392be | 70 | string err = getParseMbedJson(in_out, content.c_str()); |
MrAI | 15:7b56d49392be | 71 | return "success"; |
MrAI | 12:1b24ea479a59 | 72 | } |
MrAI | 12:1b24ea479a59 | 73 | |
MrAI | 15:7b56d49392be | 74 | string Account::getWallet(MbedJSONValue &in_out) |
MrAI | 12:1b24ea479a59 | 75 | { |
MrAI | 12:1b24ea479a59 | 76 | //https://rest-beta.sdchain.io/v1/accounts/balances/6faLUhmp9gNgS9jXS3rAWbg8hFfi9PbWH5 |
MrAI | 12:1b24ea479a59 | 77 | string strRep = ""; |
MrAI | 12:1b24ea479a59 | 78 | string strUrl = "/v1/wallet/new"; |
MrAI | 12:1b24ea479a59 | 79 | client.http_get(strUrl, "", strRep); |
MrAI | 12:1b24ea479a59 | 80 | string content = client.get_content(strRep); |
MrAI | 15:7b56d49392be | 81 | if (content.empty()) |
MrAI | 15:7b56d49392be | 82 | { |
MrAI | 15:7b56d49392be | 83 | return ""; |
MrAI | 15:7b56d49392be | 84 | } |
MrAI | 15:7b56d49392be | 85 | string err = getParseMbedJson(in_out, content.c_str()); |
MrAI | 15:7b56d49392be | 86 | return "success"; |
MrAI | 12:1b24ea479a59 | 87 | } |
MrAI | 12:1b24ea479a59 | 88 | |
MrAI | 12:1b24ea479a59 | 89 | string Account::getParseMbedJson(MbedJSONValue& out, const char * pos) |
MrAI | 12:1b24ea479a59 | 90 | { |
MrAI | 12:1b24ea479a59 | 91 | string err = parse(out, pos); |
MrAI | 12:1b24ea479a59 | 92 | return err; |
MrAI | 12:1b24ea479a59 | 93 | } |
MrAI | 12:1b24ea479a59 | 94 | |
MrAI | 12:1b24ea479a59 | 95 | bool Account::getRequestRet(MbedJSONValue& in) |
MrAI | 12:1b24ea479a59 | 96 | { |
MrAI | 12:1b24ea479a59 | 97 | bool bRet = in["success"].get <bool>(); |
MrAI | 12:1b24ea479a59 | 98 | return bRet; |
MrAI | 12:1b24ea479a59 | 99 | } |
MrAI | 12:1b24ea479a59 | 100 | |
MrAI | 12:1b24ea479a59 | 101 | string Account::getKeyValue(MbedJSONValue& in, string Key1, string Key2) |
MrAI | 12:1b24ea479a59 | 102 | { |
MrAI | 12:1b24ea479a59 | 103 | string sValue = in[Key1][Key2].get<string>(); |
MrAI | 12:1b24ea479a59 | 104 | return sValue; |
MrAI | 12:1b24ea479a59 | 105 | } |
MrAI | 12:1b24ea479a59 | 106 | |
MrAI | 12:1b24ea479a59 | 107 | int Account::getKeyValueInt(MbedJSONValue& in, string Key1, string Key2) |
MrAI | 12:1b24ea479a59 | 108 | { |
MrAI | 12:1b24ea479a59 | 109 | int sValue = in[Key1][Key2].get<int>(); |
MrAI | 12:1b24ea479a59 | 110 | return sValue; |
MrAI | 12:1b24ea479a59 | 111 | } |
MrAI | 12:1b24ea479a59 | 112 | |
MrAI | 12:1b24ea479a59 | 113 | double Account::getKeyValuedouble(MbedJSONValue& in, string Key1, string Key2) |
MrAI | 12:1b24ea479a59 | 114 | { |
MrAI | 12:1b24ea479a59 | 115 | double sValue = in[Key1][Key2].get<double>(); |
MrAI | 12:1b24ea479a59 | 116 | return sValue; |
MrAI | 12:1b24ea479a59 | 117 | } |
MrAI | 12:1b24ea479a59 | 118 | |
MrAI | 12:1b24ea479a59 | 119 | string Account::getKeyValue(MbedJSONValue& in, string Key1) |
MrAI | 12:1b24ea479a59 | 120 | { |
MrAI | 12:1b24ea479a59 | 121 | string sValue = in[Key1].get<string>(); |
MrAI | 12:1b24ea479a59 | 122 | return sValue; |
MrAI | 12:1b24ea479a59 | 123 | } |
MrAI | 12:1b24ea479a59 | 124 | |
MrAI | 12:1b24ea479a59 | 125 | bool Account::getKeyValueBool(MbedJSONValue& in, string Key1,string Key2) |
MrAI | 12:1b24ea479a59 | 126 | { |
MrAI | 12:1b24ea479a59 | 127 | bool bRet = in[Key1][Key2].get <bool>(); |
MrAI | 12:1b24ea479a59 | 128 | return bRet; |
MrAI | 12:1b24ea479a59 | 129 | } |
MrAI | 12:1b24ea479a59 | 130 | |
MrAI | 12:1b24ea479a59 | 131 | bool Account::getKeyValueBool(MbedJSONValue& in, string Key1) |
MrAI | 12:1b24ea479a59 | 132 | { |
MrAI | 12:1b24ea479a59 | 133 | bool bRet = in[Key1].get <bool>(); |
MrAI | 12:1b24ea479a59 | 134 | return bRet; |
MrAI | 12:1b24ea479a59 | 135 | } |
MrAI | 12:1b24ea479a59 | 136 | int Account::getLedger(MbedJSONValue& in) |
MrAI | 12:1b24ea479a59 | 137 | { |
MrAI | 12:1b24ea479a59 | 138 | int iLedger = in["ledger"].get<int>(); |
MrAI | 12:1b24ea479a59 | 139 | return iLedger; |
MrAI | 12:1b24ea479a59 | 140 | } |
MrAI | 12:1b24ea479a59 | 141 | |
MrAI | 12:1b24ea479a59 | 142 | int Account::getBalanceSize(MbedJSONValue& in,string Key1) |
MrAI | 12:1b24ea479a59 | 143 | { |
MrAI | 12:1b24ea479a59 | 144 | int iCurrenciesNum = in[Key1].size(); |
MrAI | 12:1b24ea479a59 | 145 | return iCurrenciesNum; |
MrAI | 12:1b24ea479a59 | 146 | } |
MrAI | 12:1b24ea479a59 | 147 | |
MrAI | 12:1b24ea479a59 | 148 | int Account::getBalanceSize(MbedJSONValue& in, string Key1,string Key2) |
MrAI | 12:1b24ea479a59 | 149 | { |
MrAI | 12:1b24ea479a59 | 150 | int iCurrenciesNum = in[Key1][Key2].size(); |
MrAI | 12:1b24ea479a59 | 151 | return iCurrenciesNum; |
MrAI | 12:1b24ea479a59 | 152 | } |
MrAI | 15:7b56d49392be | 153 | int Account::getBalanceSizeEx(MbedJSONValue& in, string Key1, string Key2,int i) |
MrAI | 15:7b56d49392be | 154 | { |
MrAI | 15:7b56d49392be | 155 | int iCurrenciesNum = in[Key1][i][Key2].size(); |
MrAI | 15:7b56d49392be | 156 | return iCurrenciesNum; |
MrAI | 15:7b56d49392be | 157 | } |
MrAI | 12:1b24ea479a59 | 158 | |
MrAI | 15:7b56d49392be | 159 | string Account::getBalanceArray_five(MbedJSONValue& in, string Key1, int i, string Key2,int j,string Key3) |
MrAI | 15:7b56d49392be | 160 | { |
MrAI | 15:7b56d49392be | 161 | string sValue = in[Key1][i][Key2][j][Key3].get<string>(); |
MrAI | 15:7b56d49392be | 162 | return sValue; |
MrAI | 15:7b56d49392be | 163 | } |
MrAI | 12:1b24ea479a59 | 164 | string Account::getBalanceArray(MbedJSONValue& in, string Key1, int i, string Key2) |
MrAI | 12:1b24ea479a59 | 165 | { |
MrAI | 12:1b24ea479a59 | 166 | string sValue = in[Key1][i][Key2].get<string>(); |
MrAI | 12:1b24ea479a59 | 167 | return sValue; |
MrAI | 12:1b24ea479a59 | 168 | } |
MrAI | 12:1b24ea479a59 | 169 | |
MrAI | 15:7b56d49392be | 170 | string Account::getBalanceArrayEx(MbedJSONValue& in, string Key1, string Key2, int i, string Key3) |
MrAI | 15:7b56d49392be | 171 | { |
MrAI | 15:7b56d49392be | 172 | string sValue = in[Key1][i][Key2][Key3].get<string>(); |
MrAI | 15:7b56d49392be | 173 | return sValue; |
MrAI | 15:7b56d49392be | 174 | } |
MrAI | 15:7b56d49392be | 175 | |
MrAI | 12:1b24ea479a59 | 176 | string Account::getBalanceArray(MbedJSONValue& in, string Key1, string Key2,int i,string Key3) |
MrAI | 12:1b24ea479a59 | 177 | { |
MrAI | 12:1b24ea479a59 | 178 | string sValue = in[Key1][Key2][i][Key3].get<string>(); |
MrAI | 12:1b24ea479a59 | 179 | return sValue; |
MrAI | 12:1b24ea479a59 | 180 | } |
MrAI | 12:1b24ea479a59 | 181 | |
MrAI | 12:1b24ea479a59 | 182 | string Account::getBalanceArray(MbedJSONValue& in, string Key1,string Key2, string Key3,int i,string Key4) |
MrAI | 12:1b24ea479a59 | 183 | { |
MrAI | 15:7b56d49392be | 184 | string sValue = in[Key1][Key2][i][Key3][Key4].get<string>(); |
MrAI | 12:1b24ea479a59 | 185 | return sValue; |
MrAI | 12:1b24ea479a59 | 186 | } |