Skip to content

Commit

Permalink
Move header entries generation to base connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Kicer86 committed Nov 22, 2023
1 parent 86d8f0e commit 18ca5fe
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
13 changes: 9 additions & 4 deletions src/base_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,18 @@ std::string GitHub::BaseConnection::get(const std::string& request)
}


const std::string & GitHub::BaseConnection::address() const
std::map<std::string, std::string> GitHub::BaseConnection::getHeaderEntries() const
{
return m_address;
std::map<std::string, std::string> entries;

if (m_token.empty() == false)
entries.emplace("Authorization", "token " + m_token);

return entries;
}


const std::string & GitHub::BaseConnection::token() const
const std::string & GitHub::BaseConnection::address() const
{
return m_token;
return m_address;
}
6 changes: 5 additions & 1 deletion src/base_connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
#ifndef BASE_CONNECTION_HPP_INCLUDED
#define BASE_CONNECTION_HPP_INCLUDED

#include <map>

#include <cpp_restapi/iconnection.hpp>


namespace GitHub
{
class BaseConnection: public cpp_restapi::IConnection
Expand All @@ -15,8 +18,9 @@ namespace GitHub
virtual std::pair<std::string, std::string> fetchPage(const std::string& request) = 0;

protected:
std::map<std::string, std::string> getHeaderEntries() const;

const std::string& address() const;
const std::string& token() const;

private:
const std::string m_address;
Expand Down
13 changes: 7 additions & 6 deletions src/curl_backend/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,16 @@ std::pair<std::string, std::string> GitHub::CurlBackend::Connection::fetchPage(c
curl_easy_setopt(curl, CURLOPT_HEADERDATA, &header_links);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "github_api/1.0");

const std::string& userToken = token();
if (userToken.empty() == false)
{
authorization = std::string("Authorization: token ") + userToken;
list = curl_slist_append(list, authorization.c_str());
const auto header_entries = getHeaderEntries();

curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
for(const auto& [k, v]: header_entries)
{
const std::string entry = k + ": " + v;
list = curl_slist_append(list, entry.c_str());
}

curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);

curl_easy_perform(curl);

curl_slist_free_all(list);
Expand Down
11 changes: 4 additions & 7 deletions src/qt_backend/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,10 @@ namespace GitHub { namespace QtBackend
{
QNetworkRequest request;

const std::string& userToken = token();
if (userToken.empty() == false)
{
const QByteArray key("Authorization");
const QByteArray value = QString("token %1").arg(userToken.c_str()).toLatin1();
request.setRawHeader(key, value);
}
const auto header_entries = getHeaderEntries();

for(const auto& [k, v]: header_entries)
request.setRawHeader(k.c_str(), v.c_str());

request.setRawHeader("User-Agent", "github_api/1.0");

Expand Down

0 comments on commit 18ca5fe

Please sign in to comment.