-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLibreTranslate.cpp
111 lines (79 loc) · 2.52 KB
/
LibreTranslate.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "LibreTranslate.h"
#include <vector>
#include <curl/curl.h>
size_t curl_writeback_fun(void* contents, size_t size, size_t nmemb, std::string *s) {
size_t len = size * nmemb;
s->append((char*)contents, len);
return len;
}
std::string curl_post(std::string url, std::vector<std::string> headers, std::string post_fields){
// Based on https://curl.se/libcurl/c/http-post.html
// Daniel Stenberg, <daniel@haxx.se>, et al.
std::string to_return;
CURL *curl;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_fields.c_str());
curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);
struct curl_slist *headers_slist = NULL;
if(headers.size() > 0){
for(std::string header : headers){
headers_slist = curl_slist_append(headers_slist, header.c_str());
}
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers_slist);
}
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_writeback_fun);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &to_return);
CURLcode res;
res = curl_easy_perform(curl);
if(res != CURLE_OK){
throw curl_easy_strerror(res);
}
if(headers_slist){
curl_slist_free_all(headers_slist);
}
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return to_return;
}
json json_post(std::string url, json data){
std::vector<std::string> headers;
headers.push_back("Content-Type: application/json");
std::string serialized_json = data.dump();
std::string res = curl_post(url, headers, serialized_json);
return json::parse(res);
}
LibreTranslateAPI::LibreTranslateAPI(std::string base_url, std::optional<std::string> api_key)
: base_url(base_url), api_key(api_key){}
json LibreTranslateAPI::translate(std::string q, std::string source, std::string target){
std::string url = base_url + "translate";
json req;
req["q"] = q;
req["source"] = source;
req["target"] = target;
if(api_key){
req["api_key"] = api_key.value();
}
json res = json_post(url, req);
return res;
}
json LibreTranslateAPI::languages(){
std::string url = base_url + "languages";
json req;
json res = json_post(url, req);
return res;
}
json LibreTranslateAPI::detect(std::string q){
std::string url = base_url + "detect";
json req;
req["q"] = q;
if(api_key){
req["api_key"] = api_key.value();
}
json res = json_post(url, req);
return res;
}