-
Notifications
You must be signed in to change notification settings - Fork 887
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolves brave/brave-browser#11286
- Loading branch information
Showing
48 changed files
with
3,861 additions
and
1,287 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
114 changes: 114 additions & 0 deletions
114
vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/get_card/get_card.cc
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* Copyright (c) 2020 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
#include "bat/ledger/internal/endpoint/uphold/get_card/get_card.h" | ||
|
||
#include "base/json/json_reader.h" | ||
#include "base/strings/string_number_conversions.h" | ||
#include "base/strings/stringprintf.h" | ||
#include "bat/ledger/internal/endpoint/uphold/uphold_utils.h" | ||
#include "bat/ledger/internal/ledger_impl.h" | ||
#include "bat/ledger/internal/uphold/uphold_card.h" | ||
#include "net/http/http_status_code.h" | ||
|
||
using std::placeholders::_1; | ||
|
||
namespace ledger { | ||
namespace endpoint { | ||
namespace uphold { | ||
|
||
GetCard::GetCard(bat_ledger::LedgerImpl* ledger): | ||
ledger_(ledger) { | ||
DCHECK(ledger_); | ||
} | ||
|
||
GetCard::~GetCard() = default; | ||
|
||
std::string GetCard::GetUrl(const std::string& address) { | ||
return GetServerUrl("/v0/me/cards/" + address); | ||
} | ||
|
||
ledger::Result GetCard::CheckStatusCode(const int status_code) { | ||
if (status_code == net::HTTP_UNAUTHORIZED || | ||
status_code == net::HTTP_NOT_FOUND || | ||
status_code == net::HTTP_FORBIDDEN) { | ||
return ledger::Result::EXPIRED_TOKEN; | ||
} | ||
|
||
if (status_code != net::HTTP_OK) { | ||
return ledger::Result::LEDGER_ERROR; | ||
} | ||
|
||
return ledger::Result::LEDGER_OK; | ||
} | ||
|
||
ledger::Result GetCard::ParseBody( | ||
const std::string& body, | ||
double* available) { | ||
DCHECK(available); | ||
|
||
base::Optional<base::Value> value = base::JSONReader::Read(body); | ||
if (!value || !value->is_dict()) { | ||
BLOG(0, "Invalid JSON"); | ||
return ledger::Result::LEDGER_ERROR; | ||
} | ||
|
||
base::DictionaryValue* dictionary = nullptr; | ||
if (!value->GetAsDictionary(&dictionary)) { | ||
BLOG(0, "Invalid JSON"); | ||
return ledger::Result::LEDGER_ERROR; | ||
} | ||
|
||
const auto* available_str = dictionary->FindStringKey("available"); | ||
if (!available_str) { | ||
BLOG(0, "Missing available"); | ||
return ledger::Result::LEDGER_ERROR; | ||
} | ||
|
||
const bool success = base::StringToDouble(*available_str, available); | ||
if (!success) { | ||
*available = 0.0; | ||
} | ||
|
||
return ledger::Result::LEDGER_OK; | ||
} | ||
|
||
void GetCard::Request( | ||
const std::string& address, | ||
const std::string& token, | ||
GetCardCallback callback) { | ||
auto url_callback = std::bind(&GetCard::OnRequest, | ||
this, | ||
_1, | ||
callback); | ||
ledger_->LoadURL( | ||
GetUrl(address), | ||
RequestAuthorization(token), | ||
"", | ||
"", | ||
ledger::UrlMethod::GET, | ||
url_callback); | ||
} | ||
|
||
void GetCard::OnRequest( | ||
const ledger::UrlResponse& response, | ||
GetCardCallback callback) { | ||
ledger::LogUrlResponse(__func__, response); | ||
|
||
ledger::Result result = CheckStatusCode(response.status_code); | ||
|
||
if (result != ledger::Result::LEDGER_OK) { | ||
callback(result, 0.0); | ||
return; | ||
} | ||
|
||
double available; | ||
result = ParseBody(response.body, &available); | ||
callback(result, available); | ||
} | ||
|
||
} // namespace uphold | ||
} // namespace endpoint | ||
} // namespace ledger |
115 changes: 115 additions & 0 deletions
115
vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/get_card/get_card.h
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* Copyright (c) 2020 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
#ifndef BRAVELEDGER_ENDPOINT_UPHOLD_GET_CARD_GET_CARD_H_ | ||
#define BRAVELEDGER_ENDPOINT_UPHOLD_GET_CARD_GET_CARD_H_ | ||
|
||
#include <string> | ||
|
||
#include "bat/ledger/ledger.h" | ||
|
||
// GET https://api.uphold.com/v0/me/cards/{wallet_address} | ||
// | ||
// Success code: | ||
// HTTP_OK (200) | ||
// | ||
// Error codes: | ||
// HTTP_UNAUTHORIZED (401) | ||
// | ||
// Response body: | ||
// { | ||
// "CreatedByApplicationId": "193a77cf-02e8-4e10-8127-8a1b5a8bfece", | ||
// "address": { | ||
// "wire": "XXXXXXXXXX" | ||
// }, | ||
// "available": "0.00", | ||
// "balance": "0.00", | ||
// "currency": "BAT", | ||
// "id": "bd91a720-f3f9-42f8-b2f5-19548004f6a7", | ||
// "label": "Brave Browser", | ||
// "lastTransactionAt": null, | ||
// "settings": { | ||
// "position": 1, | ||
// "protected": false, | ||
// "starred": true | ||
// }, | ||
// "createdByApplicationClientId": "4c2b665ca060d912fec5c735c734859a06118cc8", | ||
// "normalized": [ | ||
// { | ||
// "available": "0.00", | ||
// "balance": "0.00", | ||
// "currency": "USD" | ||
// } | ||
// ], | ||
// "wire": [ | ||
// { | ||
// "accountName": "Uphold Europe Limited", | ||
// "address": { | ||
// "line1": "Tartu mnt 2", | ||
// "line2": "10145 Tallinn, Estonia" | ||
// }, | ||
// "bic": "LHVBEE22", | ||
// "currency": "EUR", | ||
// "iban": "EE76 7700 7710 0159 0178", | ||
// "name": "AS LHV Pank" | ||
// }, | ||
// { | ||
// "accountName": "Uphold HQ, Inc.", | ||
// "accountNumber": "XXXXXXXXXX", | ||
// "address": { | ||
// "line1": "1359 Broadway", | ||
// "line2": "New York, NY 10018" | ||
// }, | ||
// "bic": "MCBEUS33", | ||
// "currency": "USD", | ||
// "name": "Metropolitan Bank", | ||
// "routingNumber": "XXXXXXXXX" | ||
// } | ||
// ] | ||
// } | ||
|
||
namespace bat_ledger { | ||
class LedgerImpl; | ||
} | ||
|
||
namespace ledger { | ||
namespace endpoint { | ||
namespace uphold { | ||
|
||
using GetCardCallback = std::function<void( | ||
const ledger::Result result, | ||
const double available)>; | ||
|
||
class GetCard { | ||
public: | ||
explicit GetCard(bat_ledger::LedgerImpl* ledger); | ||
~GetCard(); | ||
|
||
void Request( | ||
const std::string& address, | ||
const std::string& token, | ||
GetCardCallback callback); | ||
|
||
private: | ||
std::string GetUrl(const std::string& address); | ||
|
||
ledger::Result CheckStatusCode(const int status_code); | ||
|
||
ledger::Result ParseBody( | ||
const std::string& body, | ||
double* available); | ||
|
||
void OnRequest( | ||
const ledger::UrlResponse& response, | ||
GetCardCallback callback); | ||
|
||
bat_ledger::LedgerImpl* ledger_; // NOT OWNED | ||
}; | ||
|
||
} // namespace uphold | ||
} // namespace endpoint | ||
} // namespace ledger | ||
|
||
#endif // BRAVELEDGER_ENDPOINT_UPHOLD_GET_CARD_GET_CARD_H_ |
Oops, something went wrong.