Skip to content

Commit

Permalink
Merge pull request #1 from editor-syntax/main
Browse files Browse the repository at this point in the history
v1
  • Loading branch information
pixxies committed Mar 28, 2023
2 parents 793a804 + b7cfd4a commit 851336b
Show file tree
Hide file tree
Showing 13 changed files with 579 additions and 0 deletions.
53 changes: 53 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
cmake_minimum_required(VERSION 3.5)

project(topgg-sdk LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_library(topgg
src/http/client.cpp
src/http/request.cpp
src/http/response.cpp
include/topgg/http/client.hpp
include/topgg/http/request.hpp
include/topgg/http/response.hpp
)

target_include_directories(topgg PUBLIC include)

target_link_libraries(topgg PUBLIC
${CURL_LIBRARIES}
nlohmann_json::nlohmann_json
)

if (MSVC)
target_compile_options(topgg PRIVATE /W4 /WX)
else ()
target_compile_options(topgg PRIVATE -Wall -Wextra -pedantic -Werror)
endif ()

add_executable(votebot
examples/votebot.cpp
)

target_link_libraries(votebot PRIVATE
topgg
)

add_executable(leaderboard
examples/leaderboard.cpp
)

target_link_libraries(leaderboard PRIVATE
topgg
)

find_package(CURL REQUIRED)
find_package(nlohmann_json 3.2.0 REQUIRED)

add_subdirectory(third_party/Catch2)

# Uncomment if you want to run unit tests
# enable_testing()
# add_subdirectory(test)
74 changes: 74 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,77 @@
<<<<<<< HEAD
# Top.gg SDK for C++

This is a simple C++ SDK for interacting with the [Top.gg API](https://docs.top.gg).

## Features

- Making authenticated requests to the Top.gg API using a bot token
- Parsing Top.gg API responses into convenient C++ objects
- Examples demonstrating how to use the SDK to implement a votebot and leaderboard

## Getting Started

1. Clone the repository:

```
git clone https://github.com/pneb/cpp-sdk.git
```

2. Add the `include` and `src` directories to your project's include path and library path, respectively.

3. Build the `topgg` library:

```
cd cpp-sdk
mkdir build
cd build
cmake ..
make
```

4. Build the examples:

```
make example-votebot
make example-leaderboard
```

5. Run the examples:

```
./example-votebot
./example-leaderboard
```

Note that the examples require you to have a Top.gg bot token set as an environment variable `TOPGG_BOT_TOKEN`.

## Usage

Include the `topgg/http/client.hpp` header and create an instance of `topgg::Api` with your Top.gg bot token:

```c++
#include <string>
#include "topgg/topgg.hpp"

// Put your bot token here
const std::string BOT_TOKEN = "YOUR_BOT_TOKEN";

int main()
{
topgg::Api api(BOT_TOKEN);

// Use the api object to make requests to the Top.gg API
return 0;
}
```

Refer to the [Top.gg API documentation](https://docs.top.gg) for information on the available endpoints and request structures.

## Contributing

Pull requests are welcome! Please include unit tests for any added or modified functionality.
=======
# Top.gg C++ SDK

The official open-source C++ wrapper for the Top.gg API
>>>>>>> upstream/main
46 changes: 46 additions & 0 deletions examples/leaderboard.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <iostream>
#include <string>
#include <vector>
#include <chrono>
#include <thread>
#include <algorithm>

#include "topgg/topgg.hpp"

// Put your bot token here
const std::string BOT_TOKEN = "YOUR_BOT_TOKEN";

int main()
{
topgg::Api api(BOT_TOKEN);

// Get the list of users that have voted for the bot
const auto [httpCode, responseBody] = api.get("/bots/"+api.getBotID()+"/votes");
std::vector<topgg::Vote> votes;
if (httpCode == 200) {
votes = topgg::Vote::fromJsonArray(responseBody);
} else {
std::cerr << "Failed to get votes: " << responseBody << "\n";
return 1;
}

// Group the votes by user ID and count their frequency
std::unordered_map<std::string, int> voteCounts;
for (const auto& vote : votes) {
++voteCounts[vote.getUserID()];
}

// Sort the users by vote count in descending order
std::vector<std::pair<std::string, int>> usersByVotes(voteCounts.begin(), voteCounts.end());
std::sort(usersByVotes.begin(), usersByVotes.end(), [](const auto& a, const auto& b){
return a.second > b.second;
});

// Print the leaderboard
std::cout << "Leaderboard:\n";
for (size_t i = 0; i < usersByVotes.size() && i < 10; ++i) {
std::cout << i+1 << ". User " << usersByVotes[i].first << ": " << usersByVotes[i].second << " votes\n";
}

return 0;
}
36 changes: 36 additions & 0 deletions examples/votebot.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <iostream>
#include <string>
#include <chrono>
#include <thread>

#include "topgg/topgg.hpp"

// Put your Top.gg bot token here
const std::string TOPGG_TOKEN = "YOUR_TOPGG_TOKEN";

// Put your Top.gg bot webhook server port here
const uint16_t PORT = 5000;

int main()
{
topgg::Api api(TOPGG_TOKEN);
topgg::WebhookServer server(api, PORT);

server.onVote([&api](const topgg::Vote& vote) {
// Launch in a new thread for non-blocking behavior
std::thread([vote, &api] {
// Increment the vote count for this user or do something else with the vote data
std::cout << "User " << vote.getUserID() << " voted for the bot!\n";

// Check if there was an error sending a thank you message to the user
const auto [httpCode, responseBody] = api.post("/bots/stats/votes", {{"bot", api.getBotID()}, {"user", vote.getUserID()}});
if (httpCode != 200) {
std::cerr << "Failed to thank the user for voting: " << responseBody << "\n";
}
}).detach();
});

server.start();

return 0;
}
44 changes: 44 additions & 0 deletions http/request.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifndef TOPGG_HTTP_REQUEST_HPP
#define TOPGG_HTTP_REQUEST_HPP

#include <string>
#include <vector>
#include <map>

namespace topgg {
namespace http {

/**
* HTTP request class for sending HTTP requests.
*
* Supports GET and POST request methods, with support for headers and query parameters.
*
* Throws a topgg::TopggAPIError when the request fails.
*/
class Request {
public:
enum class Method {
GET,
POST
};

Request(Method method, const std::string& url);

void setHeader(const std::string& key, const std::string& value);
void setQueryParameter(const std::string& key, const std::string& value);
void setBody(const std::string& body);

std::pair<int, std::string> send();

private:
Method m_method;
std::string m_url;
std::map<std::string, std::string> m_headers;
std::map<std::string, std::string> m_queryParams;
std::string m_body;
};

} // namespace http
} // namespace topgg

#endif // TOPGG_HTTP_REQUEST_HPP
30 changes: 30 additions & 0 deletions http/response.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef TOPGG_HTTP_RESPONSE_HPP
#define TOPGG_HTTP_RESPONSE_HPP

#include <string>
#include <map>

namespace topgg {
namespace http {

/**
* HTTP response class for representing HTTP response data.
*/
class Response {
public:
Response(int statusCode, const std::string& body, const std::map<std::string, std::string>& headers);

int getStatusCode() const;
std::string getBody() const;
std::string getHeader(const std::string& key) const;

private:
int m_statusCode;
std::string m_body;
std::map<std::string, std::string> m_headers;
};

} // namespace http
} // namespace topgg

#endif // TOPGG_HTTP_RESPONSE_HPP
31 changes: 31 additions & 0 deletions include/topgg/client.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef TOPGG_CLIENT_HPP
#define TOPGG_CLIENT_HPP

#include <string>
#include <memory>

#include "http/request.hpp"
#include "http/response.hpp"
#include "errors.hpp"
#include "models.hpp"

namespace topgg {

class Client {
public:
Client(const std::string& token);

std::shared_ptr<User> getCurrentUser();
std::shared_ptr<BotStats> getBotStats();
std::vector<std::shared_ptr<User>> getBotVotes(const std::string& bot_id);

private:
std::string m_token;
std::string m_baseUrl = "https://top.gg/api/";
std::string m_authHeader;
int m_timeout = 5000;
};

} // namespace topgg

#endif // TOPGG_CLIENT_HPP
28 changes: 28 additions & 0 deletions include/topgg/errors.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef TOPGG_ERRORS_HPP
#define TOPGG_ERRORS_HPP

#include <exception>

namespace topgg {

class TopggAPIError : public std::exception {
public:
TopggAPIError(const std::string& message);
const char* what() const noexcept override;

private:
std::string m_message;
};

class TopggAuthError : public std::exception {
public:
TopggAuthError(const std::string& message);
const char* what() const noexcept override;

private:
std::string m_message;
};

} // namespace topgg

#endif // TOPGG_ERRORS_HPP
Loading

0 comments on commit 851336b

Please sign in to comment.