Skip to content

Commit

Permalink
Adds uphold server handler
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Aug 26, 2020
1 parent ea36a29 commit 0b788bc
Show file tree
Hide file tree
Showing 48 changed files with 3,861 additions and 1,287 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -282,18 +282,12 @@ void RewardsBrowserTestResponse::Get(
}
}

if (base::StartsWith(
url,
braveledger_uphold::GetAPIUrl("/oauth2/token"),
base::CompareCase::INSENSITIVE_ASCII)) {
if (url.find("/oauth2/token") != std::string::npos) {
*response = uphold_auth_resp_;
return;
}

if (base::StartsWith(
url,
braveledger_uphold::GetAPIUrl("/v0/me/cards"),
base::CompareCase::INSENSITIVE_ASCII)) {
if (url.find("/v0/me/cards") != std::string::npos) {
if (base::EndsWith(
url,
"transactions",
Expand All @@ -313,10 +307,7 @@ void RewardsBrowserTestResponse::Get(
return;
}

if (base::StartsWith(
url,
braveledger_uphold::GetAPIUrl("/v0/me"),
base::CompareCase::INSENSITIVE_ASCII)) {
if (url.find("/v0/me") != std::string::npos) {
*response = rewards_browsertest_util::GetUpholdUser(verified_wallet_);
return;
}
Expand Down
9 changes: 9 additions & 0 deletions components/brave_rewards/test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ source_set("brave_rewards_unit_tests") {
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/promotion/put_safetynet/put_safetynet_unittest.cc",
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/rewards/get_prefix_list/get_prefix_list_unittest.cc",
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/rewards/rewards_util_unittest.cc",
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/get_card/get_card_unittest.cc",
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/get_cards/get_cards_unittest.cc",
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/get_me/get_me_unittest.cc",
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/patch_card/patch_card_unittest.cc",
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_cards/post_cards_unittest.cc",
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_oauth/post_oauth_unittest.cc",
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_transaction/post_transaction_unittest.cc",
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/post_transaction_commit/post_transaction_commit_unittest.cc",
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/uphold/uphold_utils_unittest.cc",
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_unittest.cc",
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_util_unittest.cc",
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/wallet_util_unittest.cc",
Expand Down
22 changes: 20 additions & 2 deletions vendor/bat-native-ledger/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,6 @@ source_set("ledger") {
"src/bat/ledger/internal/report/report.h",
"src/bat/ledger/internal/request/request_util.cc",
"src/bat/ledger/internal/request/request_util.h",
"src/bat/ledger/internal/response/response_uphold.cc",
"src/bat/ledger/internal/response/response_uphold.h",
"src/bat/ledger/internal/endpoint/api/api_server.cc",
"src/bat/ledger/internal/endpoint/api/api_server.h",
"src/bat/ledger/internal/endpoint/api/api_util.cc",
Expand Down Expand Up @@ -385,6 +383,26 @@ source_set("ledger") {
"src/bat/ledger/internal/endpoint/rewards/rewards_server.h",
"src/bat/ledger/internal/endpoint/rewards/rewards_util.cc",
"src/bat/ledger/internal/endpoint/rewards/rewards_util.h",
"src/bat/ledger/internal/endpoint/uphold/get_card/get_card.cc",
"src/bat/ledger/internal/endpoint/uphold/get_card/get_card.h",
"src/bat/ledger/internal/endpoint/uphold/get_cards/get_cards.cc",
"src/bat/ledger/internal/endpoint/uphold/get_cards/get_cards.h",
"src/bat/ledger/internal/endpoint/uphold/get_me/get_me.cc",
"src/bat/ledger/internal/endpoint/uphold/get_me/get_me.h",
"src/bat/ledger/internal/endpoint/uphold/patch_card/patch_card.cc",
"src/bat/ledger/internal/endpoint/uphold/patch_card/patch_card.h",
"src/bat/ledger/internal/endpoint/uphold/post_cards/post_cards.cc",
"src/bat/ledger/internal/endpoint/uphold/post_cards/post_cards.h",
"src/bat/ledger/internal/endpoint/uphold/post_oauth/post_oauth.cc",
"src/bat/ledger/internal/endpoint/uphold/post_oauth/post_oauth.h",
"src/bat/ledger/internal/endpoint/uphold/post_transaction/post_transaction.cc",
"src/bat/ledger/internal/endpoint/uphold/post_transaction/post_transaction.h",
"src/bat/ledger/internal/endpoint/uphold/post_transaction_commit/post_transaction_commit.cc",
"src/bat/ledger/internal/endpoint/uphold/post_transaction_commit/post_transaction_commit.h",
"src/bat/ledger/internal/endpoint/uphold/uphold_server.cc",
"src/bat/ledger/internal/endpoint/uphold/uphold_server.h",
"src/bat/ledger/internal/endpoint/uphold/uphold_utils.cc",
"src/bat/ledger/internal/endpoint/uphold/uphold_utils.h",
"src/bat/ledger/internal/sku/sku.h",
"src/bat/ledger/internal/sku/sku_brave.cc",
"src/bat/ledger/internal/sku/sku_brave.h",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "bat/ledger/internal/ledger_impl.h"
#include "bat/ledger/internal/sku/sku_brave.h"
#include "bat/ledger/internal/static_values.h"
#include "bat/ledger/internal/uphold/uphold_util.h"

using std::placeholders::_1;
using std::placeholders::_2;
Expand Down
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
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_
Loading

0 comments on commit 0b788bc

Please sign in to comment.