Skip to content

Commit

Permalink
Use dependency ejection to remove json include from API
Browse files Browse the repository at this point in the history
  • Loading branch information
Royna2544 committed Oct 15, 2024
1 parent 93a5234 commit 0eca000
Show file tree
Hide file tree
Showing 8 changed files with 2,621 additions and 393 deletions.
506 changes: 255 additions & 251 deletions include/tgbot/Api.h

Large diffs are not rendered by default.

2,220 changes: 2,220 additions & 0 deletions include/tgbot/ApiImpl.h

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions include/tgbot/Bot.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class TGBOT_API Bot {
* @return Object which can execute Telegram Bot API methods.
*/
inline const Api& getApi() const {
return _api;
return *_api;
}

/**
Expand All @@ -55,7 +55,7 @@ class TGBOT_API Bot {
static HttpClient &_getDefaultHttpClient();

const std::string _token;
const Api _api;
const std::shared_ptr<Api> _api;
std::unique_ptr<EventBroadcaster> _eventBroadcaster;
const EventHandler _eventHandler;
};
Expand Down
6 changes: 3 additions & 3 deletions include/tgbot/net/TgLongPoll.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef TGBOT_TGLONGPOLL_H
#define TGBOT_TGLONGPOLL_H

#include "tgbot/Api.h"
#include "tgbot/ApiImpl.h"
#include "tgbot/export.h"

#include <cstdint>
Expand All @@ -22,7 +22,7 @@ class EventHandler;
class TGBOT_API TgLongPoll {

public:
TgLongPoll(const Api* api, const EventHandler* eventHandler, std::int32_t limit, std::int32_t timeout, std::shared_ptr<std::vector<std::string>> allowUpdates);
TgLongPoll(const ApiImpl* api, const EventHandler* eventHandler, std::int32_t limit, std::int32_t timeout, std::shared_ptr<std::vector<std::string>> allowUpdates);
TgLongPoll(const Bot& bot, std::int32_t limit = 100, std::int32_t timeout = 10, const std::shared_ptr<std::vector<std::string>>& allowUpdates = nullptr);

/**
Expand All @@ -31,7 +31,7 @@ class TGBOT_API TgLongPoll {
void start();

private:
const Api* _api;
const ApiImpl* _api;
const EventHandler* _eventHandler;
std::int32_t _lastUpdateId = 0;
std::int32_t _limit;
Expand Down
261 changes: 132 additions & 129 deletions src/Api.cpp

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/Bot.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "tgbot/ApiImpl.h"
#include "tgbot/net/BoostHttpOnlySslClient.h"
#include "tgbot/Bot.h"

Expand All @@ -10,7 +11,7 @@ namespace TgBot {

Bot::Bot(std::string token, const HttpClient& httpClient, const std::string& url)
: _token(std::move(token))
, _api(_token, httpClient, url)
, _api(std::make_shared<ApiImpl>(_token, httpClient, url))
, _eventBroadcaster(std::make_unique<EventBroadcaster>())
, _eventHandler(getEvents()) {
}
Expand Down
6 changes: 3 additions & 3 deletions src/net/TgLongPoll.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "tgbot/net/TgLongPoll.h"

#include "tgbot/Api.h"
#include "tgbot/ApiImpl.h"
#include "tgbot/Bot.h"
#include "tgbot/EventHandler.h"

Expand All @@ -11,15 +11,15 @@

namespace TgBot {

TgLongPoll::TgLongPoll(const Api* api, const EventHandler* eventHandler, std::int32_t limit, std::int32_t timeout, std::shared_ptr<std::vector<std::string>> allowUpdates)
TgLongPoll::TgLongPoll(const ApiImpl* api, const EventHandler* eventHandler, std::int32_t limit, std::int32_t timeout, std::shared_ptr<std::vector<std::string>> allowUpdates)
: _api(api), _eventHandler(eventHandler), _limit(limit), _timeout(timeout)
, _allowUpdates(std::move(allowUpdates)) {

const_cast<TgBot::HttpClient&>(_api->_httpClient)._timeout = _timeout + 5;
}

TgLongPoll::TgLongPoll(const Bot& bot, std::int32_t limit, std::int32_t timeout, const std::shared_ptr<std::vector<std::string>>& allowUpdates)
: TgLongPoll(&bot.getApi(), &bot.getEventHandler(), limit, timeout, allowUpdates) {
: TgLongPoll(dynamic_cast<const ApiImpl *>(&bot.getApi()), &bot.getEventHandler(), limit, timeout, allowUpdates) {
}

void TgLongPoll::start() {
Expand Down
8 changes: 4 additions & 4 deletions test/tgbot/Api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
#include <boost/test/unit_test.hpp>

#include "tgbot/net/HttpClient.h"
#include "tgbot/Api.h"
#include "tgbot/ApiImpl.h"
#include "tgbot/TgException.h"

using namespace std;
using namespace TgBot;

typedef TgException::ErrorCode TgErrorCode;

class TestableApi : public Api {
class TestableApi : public ApiImpl {
public:
using Api::Api;
using Api::sendRequest;
using ApiImpl::ApiImpl;
using ApiImpl::sendRequest;
};

class HttpClientMock : public HttpClient {
Expand Down

0 comments on commit 0eca000

Please sign in to comment.