mirror of
https://github.com/Syping/dtranslatebot.git
synced 2026-04-01 05:20:22 +02:00
translator: reuse http_request instance
This commit is contained in:
parent
305cbc9437
commit
5e3808204e
8 changed files with 19 additions and 23 deletions
|
|
@ -18,7 +18,6 @@
|
||||||
|
|
||||||
#include <dpp/json.h>
|
#include <dpp/json.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "../../core/http_request.h"
|
|
||||||
#include "deepl.h"
|
#include "deepl.h"
|
||||||
using namespace bot::http;
|
using namespace bot::http;
|
||||||
using namespace bot::translator;
|
using namespace bot::translator;
|
||||||
|
|
@ -43,8 +42,7 @@ const std::vector<language> deepl::get_languages()
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
http_request request;
|
http_response response = m_http.get(http_request::legacy_url(m_hostname, 443, "/v2/languages?type=target", true),
|
||||||
http_response response = request.get(http_request::legacy_url(m_hostname, 443, "/v2/languages?type=target", true),
|
|
||||||
{{"Authorization", "DeepL-Auth-Key " + m_apiKey}});
|
{{"Authorization", "DeepL-Auth-Key " + m_apiKey}});
|
||||||
if (response.status == 200) {
|
if (response.status == 200) {
|
||||||
const dpp::json json_response = dpp::json::parse(response.content);
|
const dpp::json json_response = dpp::json::parse(response.content);
|
||||||
|
|
@ -92,8 +90,7 @@ const std::string deepl::translate(const std::string &text, const std::string &s
|
||||||
json_body["source_lang"] = source;
|
json_body["source_lang"] = source;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
http_request request;
|
http_response response = m_http.post(http_request::legacy_url(m_hostname, 443, "/v2/translate", true), json_body.dump(),
|
||||||
http_response response = request.post(http_request::legacy_url(m_hostname, 443, "/v2/translate", true), json_body.dump(),
|
|
||||||
{{"Authorization", "DeepL-Auth-Key " + m_apiKey}, {"Content-Type", "application/json"}});
|
{{"Authorization", "DeepL-Auth-Key " + m_apiKey}, {"Content-Type", "application/json"}});
|
||||||
if (response.status == 200) {
|
if (response.status == 200) {
|
||||||
const dpp::json json_response = dpp::json::parse(response.content);
|
const dpp::json json_response = dpp::json::parse(response.content);
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@
|
||||||
#ifndef TRANSLATOR_DEEPL_H
|
#ifndef TRANSLATOR_DEEPL_H
|
||||||
#define TRANSLATOR_DEEPL_H
|
#define TRANSLATOR_DEEPL_H
|
||||||
|
|
||||||
|
#include "../../core/http_request.h"
|
||||||
#include "../../core/translator.h"
|
#include "../../core/translator.h"
|
||||||
|
|
||||||
namespace bot {
|
namespace bot {
|
||||||
|
|
@ -33,6 +34,7 @@ namespace bot {
|
||||||
private:
|
private:
|
||||||
std::string m_apiKey;
|
std::string m_apiKey;
|
||||||
std::string m_hostname;
|
std::string m_hostname;
|
||||||
|
bot::http::http_request m_http;
|
||||||
supported_languages m_languages;
|
supported_languages m_languages;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,6 @@
|
||||||
|
|
||||||
#include <dpp/json.h>
|
#include <dpp/json.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "../../core/http_request.h"
|
|
||||||
#include "libretranslate.h"
|
#include "libretranslate.h"
|
||||||
using namespace bot::http;
|
using namespace bot::http;
|
||||||
using namespace bot::translator;
|
using namespace bot::translator;
|
||||||
|
|
@ -43,8 +42,7 @@ const std::vector<language> libretranslate::get_languages()
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
http_request request;
|
http_response response = m_http.get(http_request::legacy_url(m_hostname, m_port, m_url + "languages", m_tls));
|
||||||
http_response response = request.get(http_request::legacy_url(m_hostname, m_port, m_url + "languages", m_tls));
|
|
||||||
if (response.status == 200) {
|
if (response.status == 200) {
|
||||||
const dpp::json json_response = dpp::json::parse(response.content);
|
const dpp::json json_response = dpp::json::parse(response.content);
|
||||||
if (json_response.is_array()) {
|
if (json_response.is_array()) {
|
||||||
|
|
@ -89,8 +87,7 @@ const std::string libretranslate::translate(const std::string &text, const std::
|
||||||
json_body["apiKey"] = m_apiKey;
|
json_body["apiKey"] = m_apiKey;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
http_request request;
|
http_response response = m_http.post(http_request::legacy_url(m_hostname, m_port, m_url + "translate", m_tls), json_body.dump(),
|
||||||
http_response response = request.post(http_request::legacy_url(m_hostname, m_port, m_url + "translate", m_tls), json_body.dump(),
|
|
||||||
{{"Content-Type", "application/json"}});
|
{{"Content-Type", "application/json"}});
|
||||||
if (response.status == 200) {
|
if (response.status == 200) {
|
||||||
const dpp::json json_response = dpp::json::parse(response.content);
|
const dpp::json json_response = dpp::json::parse(response.content);
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@
|
||||||
#define TRANSLATOR_LIBRETRANSLATE_H
|
#define TRANSLATOR_LIBRETRANSLATE_H
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include "../../core/http_request.h"
|
||||||
#include "../../core/translator.h"
|
#include "../../core/translator.h"
|
||||||
|
|
||||||
namespace bot {
|
namespace bot {
|
||||||
|
|
@ -35,6 +36,7 @@ namespace bot {
|
||||||
private:
|
private:
|
||||||
std::string m_apiKey;
|
std::string m_apiKey;
|
||||||
std::string m_hostname;
|
std::string m_hostname;
|
||||||
|
bot::http::http_request m_http;
|
||||||
supported_languages m_languages;
|
supported_languages m_languages;
|
||||||
uint16_t m_port;
|
uint16_t m_port;
|
||||||
std::string m_url;
|
std::string m_url;
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,6 @@
|
||||||
#include <dpp/json.h>
|
#include <dpp/json.h>
|
||||||
#include <dpp/utility.h>
|
#include <dpp/utility.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "../../core/http_request.h"
|
|
||||||
#include "lingvatranslate.h"
|
#include "lingvatranslate.h"
|
||||||
using namespace bot::http;
|
using namespace bot::http;
|
||||||
using namespace bot::translator;
|
using namespace bot::translator;
|
||||||
|
|
@ -44,8 +43,7 @@ const std::vector<language> lingvatranslate::get_languages()
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
http_request request;
|
http_response response = m_http.get(http_request::legacy_url(m_hostname, m_port, m_url + "api/v1/languages/target", m_tls));
|
||||||
http_response response = request.get(http_request::legacy_url(m_hostname, m_port, m_url + "api/v1/languages/target", m_tls));
|
|
||||||
if (response.status == 200) {
|
if (response.status == 200) {
|
||||||
const dpp::json json_response = dpp::json::parse(response.content);
|
const dpp::json json_response = dpp::json::parse(response.content);
|
||||||
if (json_response.is_object()) {
|
if (json_response.is_object()) {
|
||||||
|
|
@ -83,8 +81,7 @@ const std::vector<language> lingvatranslate::get_languages()
|
||||||
const std::string lingvatranslate::translate(const std::string &text, const std::string &source, const std::string &target)
|
const std::string lingvatranslate::translate(const std::string &text, const std::string &source, const std::string &target)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
http_request request;
|
http_response response = m_http.get(http_request::legacy_url(m_hostname, m_port, m_url + "api/v1/" + (source.empty() ? "auto" : source) + "/" + target + "/" + dpp::utility::url_encode(text), m_tls));
|
||||||
http_response response = request.get(http_request::legacy_url(m_hostname, m_port, m_url + "api/v1/" + (source.empty() ? "auto" : source) + "/" + target + "/" + dpp::utility::url_encode(text), m_tls));
|
|
||||||
if (response.status == 200) {
|
if (response.status == 200) {
|
||||||
const dpp::json json_response = dpp::json::parse(response.content);
|
const dpp::json json_response = dpp::json::parse(response.content);
|
||||||
if (json_response.is_object()) {
|
if (json_response.is_object()) {
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@
|
||||||
#define TRANSLATOR_LINGVATRANSLATE_H
|
#define TRANSLATOR_LINGVATRANSLATE_H
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include "../../core/http_request.h"
|
||||||
#include "../../core/translator.h"
|
#include "../../core/translator.h"
|
||||||
|
|
||||||
namespace bot {
|
namespace bot {
|
||||||
|
|
@ -34,6 +35,7 @@ namespace bot {
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string m_hostname;
|
std::string m_hostname;
|
||||||
|
bot::http::http_request m_http;
|
||||||
supported_languages m_languages;
|
supported_languages m_languages;
|
||||||
uint16_t m_port;
|
uint16_t m_port;
|
||||||
std::string m_url;
|
std::string m_url;
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,6 @@
|
||||||
#include <dpp/json.h>
|
#include <dpp/json.h>
|
||||||
#include <dpp/utility.h>
|
#include <dpp/utility.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "../../core/http_request.h"
|
|
||||||
#include "mozhi.h"
|
#include "mozhi.h"
|
||||||
using namespace bot::http;
|
using namespace bot::http;
|
||||||
using namespace bot::translator;
|
using namespace bot::translator;
|
||||||
|
|
@ -47,8 +46,7 @@ const std::vector<language> mozhi::get_languages()
|
||||||
const std::string parameters = dpp::utility::make_url_parameters({
|
const std::string parameters = dpp::utility::make_url_parameters({
|
||||||
{"engine", m_engine}
|
{"engine", m_engine}
|
||||||
});
|
});
|
||||||
http_request request;
|
http_response response = m_http.get(http_request::legacy_url(m_hostname, m_port, m_url + "api/target_languages", m_tls));
|
||||||
http_response response = request.get(http_request::legacy_url(m_hostname, m_port, m_url + "api/target_languages", m_tls));
|
|
||||||
if (response.status == 200) {
|
if (response.status == 200) {
|
||||||
const dpp::json json_response = dpp::json::parse(response.content);
|
const dpp::json json_response = dpp::json::parse(response.content);
|
||||||
if (json_response.is_array()) {
|
if (json_response.is_array()) {
|
||||||
|
|
@ -89,8 +87,7 @@ const std::string mozhi::translate(const std::string &text, const std::string &s
|
||||||
{"to", target},
|
{"to", target},
|
||||||
{"text", text}
|
{"text", text}
|
||||||
});
|
});
|
||||||
http_request request;
|
http_response response = m_http.get(http_request::legacy_url(m_hostname, m_port, m_url + "api/translate" + parameters, m_tls));
|
||||||
http_response response = request.get(http_request::legacy_url(m_hostname, m_port, m_url + "api/translate" + parameters, m_tls));
|
|
||||||
if (response.status == 200) {
|
if (response.status == 200) {
|
||||||
const dpp::json json_response = dpp::json::parse(response.content);
|
const dpp::json json_response = dpp::json::parse(response.content);
|
||||||
if (json_response.is_object()) {
|
if (json_response.is_object()) {
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@
|
||||||
#define TRANSLATOR_MOZHI_H
|
#define TRANSLATOR_MOZHI_H
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include "../../core/http_request.h"
|
||||||
#include "../../core/translator.h"
|
#include "../../core/translator.h"
|
||||||
|
|
||||||
namespace bot {
|
namespace bot {
|
||||||
|
|
@ -35,6 +36,7 @@ namespace bot {
|
||||||
private:
|
private:
|
||||||
std::string m_engine;
|
std::string m_engine;
|
||||||
std::string m_hostname;
|
std::string m_hostname;
|
||||||
|
bot::http::http_request m_http;
|
||||||
supported_languages m_languages;
|
supported_languages m_languages;
|
||||||
uint16_t m_port;
|
uint16_t m_port;
|
||||||
std::string m_url;
|
std::string m_url;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue