Skip to content

Commit

Permalink
Merge fce0ca0 into 71ced36
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhlmannmarkus committed Jun 20, 2018
2 parents 71ced36 + fce0ca0 commit e299cac
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 12 deletions.
2 changes: 1 addition & 1 deletion include/iota/api/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Core {
* @param timeout Timeout for the requests.
*/
explicit Core(const std::string& host, const uint16_t& port, bool localPow = true,
int timeout = 60);
int timeout = 60, const std::string& user = "", const std::string& pass = "");
/**
* Default dtor.
*/
Expand Down
2 changes: 1 addition & 1 deletion include/iota/api/extended.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Extended : public Core {
* @param localPow Whether to do local or remote proof of work.
* @param timeout Timeout for the requests.
*/
Extended(const std::string& host, const uint16_t& port, bool localPow = true, int timeout = 60);
Extended(const std::string& host, const uint16_t& port, bool localPow = true, int timeout = 60, const std::string& user = "", const std::string& pass = "");
/**
* Default dtor.
*/
Expand Down
21 changes: 17 additions & 4 deletions include/iota/api/service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#pragma once

#include <cpr/cpr.h>
#include <cpr/auth.h>
#include <json.hpp>

#include <iota/constants.hpp>
Expand Down Expand Up @@ -53,7 +54,7 @@ class Service {
* @param port Port of the node.
* @param timeout Request timeout.
*/
Service(const std::string& host, const uint16_t& port, int timeout = 60);
Service(const std::string& host, const uint16_t& port, int timeout = 60, const std::string& user = "", const std::string& pass = "");
/**
* Default dtor;
*/
Expand All @@ -74,13 +75,17 @@ class Service {
json data;
request.serialize(data);

auto url = cpr::Url{ "http://" + host_ + ":" + std::to_string(port_) };
auto url = cpr::Url{ host_ + ":" + std::to_string(port_) };
auto body = cpr::Body{ data.dump() };
auto headers = cpr::Header{ { "Content-Type", "application/json" },
{ "Content-Length", std::to_string(body.size()) },
{ "X-IOTA-API-Version", APIVersion } };
auto res = cpr::Post(url, body, headers, cpr::Timeout{ timeout_ * 1000 });


auto res = cpr::Post(url, body, headers, cpr::Timeout{ timeout_ * 1000 });;
if(user_.compare("") != 0 && pass_.compare("") != 0 && host_.compare(0,5,"https") == 0){
auto auth = cpr::Authentication{user_, pass_};
res = cpr::Post(url, body, headers, cpr::Timeout{ timeout_ * 1000 }, auth);
}
if (res.error.code != cpr::ErrorCode::OK)
throw Errors::Network(res.error.message);

Expand Down Expand Up @@ -135,6 +140,14 @@ class Service {
* Timeout for requests.
*/
const int timeout_;
/**
* Username for authenticated requests.
*/
std::string user_;
/**
* Password for authenticated requests.
*/
std::string pass_;
};

} // namespace API
Expand Down
4 changes: 2 additions & 2 deletions source/api/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ namespace IOTA {

namespace API {

Core::Core(const std::string& host, const uint16_t& port, bool localPow, int timeout)
: service_(host, port, timeout), localPow_(localPow) {
Core::Core(const std::string& host, const uint16_t& port, bool localPow, int timeout, const std::string& user, const std::string& pass)
: service_(host, port, timeout, user, pass), localPow_(localPow) {
}

Responses::GetNodeInfo
Expand Down
4 changes: 2 additions & 2 deletions source/api/extended.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ namespace IOTA {

namespace API {

Extended::Extended(const std::string& host, const uint16_t& port, bool localPow, int timeout)
: Core(host, port, localPow, timeout) {
Extended::Extended(const std::string& host, const uint16_t& port, bool localPow, int timeout, const std::string& user, const std::string& pass)
: Core(host, port, localPow, timeout, user, pass) {
}

/*
Expand Down
4 changes: 2 additions & 2 deletions source/api/service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ namespace IOTA {

namespace API {

Service::Service(const std::string& host, const uint16_t& port, int timeout)
: host_(host), port_(port), timeout_(timeout) {
Service::Service(const std::string& host, const uint16_t& port, int timeout, const std::string& user, const std::string& pass)
: host_(host), port_(port), timeout_(timeout), user_(user), pass_(pass) {
}

} // namespace API
Expand Down

0 comments on commit e299cac

Please sign in to comment.