create VKClient with get_posts() method to get wall posts
This commit is contained in:
parent
637f082605
commit
fb35fa5ed2
3
.gitmodules
vendored
3
.gitmodules
vendored
@ -10,3 +10,6 @@
|
||||
[submodule "curl"]
|
||||
path = curl
|
||||
url = git@github.com:curl/curl.git
|
||||
[submodule "json"]
|
||||
path = json
|
||||
url = git@github.com:nlohmann/json.git
|
||||
|
@ -2,10 +2,11 @@ cmake_minimum_required(VERSION 3.5)
|
||||
project(mmcs-quotes-bridge LANGUAGES CXX)
|
||||
|
||||
add_subdirectory(curl)
|
||||
add_subdirectory(json)
|
||||
add_subdirectory(libuv)
|
||||
add_subdirectory(spdlog)
|
||||
add_subdirectory(td)
|
||||
|
||||
add_executable(${PROJECT_NAME} main.cpp http.cpp)
|
||||
add_executable(${PROJECT_NAME} main.cpp http.cpp vk.cpp)
|
||||
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE CURL::libcurl uv spdlog::spdlog Td::TdStatic $<$<BOOL:${MINGW}>:ws2_32>)
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE CURL::libcurl nlohmann_json::nlohmann_json uv spdlog::spdlog Td::TdStatic $<$<BOOL:${MINGW}>:ws2_32>)
|
||||
|
1
json
Submodule
1
json
Submodule
@ -0,0 +1 @@
|
||||
Subproject commit fde9a86c5ad9c671662c0e9802b621f2de3cf60a
|
78
vk.cpp
Normal file
78
vk.cpp
Normal file
@ -0,0 +1,78 @@
|
||||
#include "vk.h"
|
||||
#include "curl/curl.h"
|
||||
#include "http.h"
|
||||
#include "spdlog/sinks/stdout_color_sinks.h"
|
||||
#include <memory>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
using namespace vk;
|
||||
using namespace nlohmann;
|
||||
|
||||
const char *API_BASE_URL = "https://api.vk.com/method/";
|
||||
const char *API_VERSION = "5.199";
|
||||
const char *LOGGER_TAG = "vk";
|
||||
|
||||
VKClient::VKClient(uv_loop_t *eventLoop) : m_httpClient(eventLoop) {
|
||||
m_logger = spdlog::get(LOGGER_TAG);
|
||||
if (!m_logger) {
|
||||
m_logger = spdlog::stdout_color_mt(LOGGER_TAG);
|
||||
m_logger->set_level(spdlog::level::debug);
|
||||
}
|
||||
};
|
||||
|
||||
VKClient::VKClient(uv_loop_t *eventLoop, std::string serviceKey) : VKClient(eventLoop) {
|
||||
set_service_api_key(serviceKey);
|
||||
}
|
||||
|
||||
void VKClient::set_service_api_key(std::string key) {
|
||||
m_serviceApiKey = {key};
|
||||
}
|
||||
|
||||
void VKClient::get_posts(std::variant<long, std::string> wall, int offset, int count, std::function<void(std::optional<WallChunk>, int)> callback) {
|
||||
if (!m_serviceApiKey) {
|
||||
m_logger->error("get_posts called without authorization");
|
||||
return;
|
||||
}
|
||||
std::string url(API_BASE_URL);
|
||||
url += "wall.get?access_token=";
|
||||
url += *m_serviceApiKey;
|
||||
url += "&v=";
|
||||
url += API_VERSION;
|
||||
url += "&offset=";
|
||||
url += std::to_string(offset);
|
||||
url += "&count=";
|
||||
url += std::to_string(count);
|
||||
if (wall.index() == 0) {
|
||||
url += "&owner_id=";
|
||||
url += std::to_string(std::get<long>(wall));
|
||||
} else {
|
||||
url += "&domain=";
|
||||
url += std::get<std::string>(wall);
|
||||
}
|
||||
m_logger->debug("using URL: {}", url);
|
||||
|
||||
m_httpClient.send_request("GET", url, {}, [this, callback](std::unique_ptr<http::HttpResponse> resp, CURLcode r){
|
||||
if (r == 0) {
|
||||
auto parsedResponse = json::parse(resp->body);
|
||||
|
||||
if (parsedResponse.contains("error")) {
|
||||
auto err = parsedResponse["error"];
|
||||
m_logger->error("get_posts error {} {}", (int)err["error_code"], (std::string)err["error_msg"]);
|
||||
callback({}, -1);
|
||||
return;
|
||||
}
|
||||
|
||||
auto responsePayload = parsedResponse["response"];
|
||||
int count = responsePayload["count"];
|
||||
std::vector<Post> posts;
|
||||
for (auto post : responsePayload["items"]) {
|
||||
posts.emplace_back(post["id"], post["date"], post["edited"], post["from_id"], post["type"], post["text"]);
|
||||
}
|
||||
|
||||
callback({{count, std::move(posts)}}, 0);
|
||||
} else {
|
||||
m_logger->error("get_posts network error");
|
||||
callback({}, r);
|
||||
}
|
||||
});
|
||||
}
|
38
vk.h
Normal file
38
vk.h
Normal file
@ -0,0 +1,38 @@
|
||||
#pragma once
|
||||
#include "http.h"
|
||||
#include "spdlog/logger.h"
|
||||
#include "uv.h"
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
|
||||
namespace vk {
|
||||
using namespace http;
|
||||
|
||||
struct Post {
|
||||
Post(long id, long date, long editDate, long fromId, std::string postType, std::string text);
|
||||
|
||||
long id, date, editDate, fromId;
|
||||
std::string postType, text;
|
||||
};
|
||||
|
||||
struct WallChunk {
|
||||
int count;
|
||||
std::vector<Post> posts;
|
||||
};
|
||||
|
||||
class VKClient {
|
||||
public:
|
||||
VKClient(uv_loop_t *eventLoop);
|
||||
VKClient(VKClient&) = delete;
|
||||
VKClient(uv_loop_t *eventLoop, std::string serviceKey);
|
||||
|
||||
void set_service_api_key(std::string key);
|
||||
void get_posts(std::variant<long, std::string> wall, int offset, int count, std::function<void(std::optional<WallChunk>, int)> callback);
|
||||
private:
|
||||
HttpClient m_httpClient;
|
||||
std::optional<std::string> m_serviceApiKey;
|
||||
std::shared_ptr<spdlog::logger> m_logger;
|
||||
};
|
||||
}
|
Loading…
Reference in New Issue
Block a user