Skip to content
Snippets Groups Projects
Commit a5cea00b authored by Bartosz Ziemba's avatar Bartosz Ziemba
Browse files

Dev

parent 196f9128
Branches
No related tags found
No related merge requests found
cmake_minimum_required(VERSION 3.9)
project(main)
project(api_example)
find_package(cpprestsdk REQUIRED) #sudo apt-get install libcpprest-dev
add_executable(main main.cpp)
target_link_libraries(main PRIVATE cpprestsdk::cpprest)
\ No newline at end of file
add_executable(api_example main.cpp)
target_link_libraries(api_example PRIVATE cpprestsdk::cpprest)
\ No newline at end of file
......@@ -20,5 +20,7 @@ cmake --build .
- clang 10.0.0 and gcc 9.3.0
- ubuntu 20.04
***By default this example will not compile with older ubuntu due to a bug in ubuntu apt package. You can however fix this by adding specific variable. For more information see [this issue](https://github.com/microsoft/cpprestsdk/issues/686)*** .
......@@ -3,31 +3,26 @@
#include <vector>
#include <thread>
#include <string>
#include <iterator>
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include "file_helper.hpp"
using namespace utility; // Common utilities like string conversions
using namespace web; // Common features like URIs.
using namespace web::http; // Common HTTP functionality
using namespace web::http::client; // HTTP client features
using namespace concurrency::streams; // Asynchronous streams
using namespace file_helper;
namespace http_helper
{
std::string upload_file(const char *filename, const char *url)
std::string post_get_string(const char *url, std::string body)
{
http_client client(U(url));
http_request request(methods::POST);
std::string result;
request.headers().add("Content-Type", "application/octet-stream");
request.set_body(readFileBytes(filename));
printf("body set as bytes\n");
pplx::task<void> requestTask = client.request(request)
http_request req(methods::POST);
req.set_body(body, "application/json");
pplx::task<void> requestTask = client.request(req)
.then([&result](http_response response) {
stringstreambuf ss;
result = response.extract_string().get();
......@@ -36,6 +31,74 @@ namespace http_helper
return result;
}
web::json::value get_json(const char *url)
{
web::json::value result;
http_client client(U(url));
http_request req(methods::GET);
pplx::task<void> requestTask = client.request(req)
.then([&result](http_response response) {
response.headers().set_content_type("application/json");
result = response.extract_json().get();
});
requestTask.wait();
return result;
}
}
namespace file_helper
{
std::vector<unsigned char> read_file_bytes(const char *filename)
{
// open the file:
std::ifstream file(filename, std::ios::binary);
if (file.is_open())
{
printf("Opened file %s \n", filename);
}
else
{
std::string error_msg = filename;
error_msg = "Failed to open file " + error_msg;
throw std::runtime_error(error_msg.c_str());
}
// Stop eating new lines in binary mode!!!
file.unsetf(std::ios::skipws);
// get its size:
std::streampos fileSize;
file.seekg(0, std::ios::end);
fileSize = file.tellg();
file.seekg(0, std::ios::beg);
// reserve capacity
std::vector<unsigned char> vec;
vec.reserve(fileSize);
// read the data:
vec.insert(vec.begin(),
std::istream_iterator<unsigned char>(file),
std::istream_iterator<unsigned char>());
printf("file read\n");
return vec;
}
void write_file_bytes(const char *filename, std::vector<unsigned char> &fileBytes)
{
std::ofstream file(filename, std::ios::out | std::ios::binary);
std::copy(fileBytes.cbegin(), fileBytes.cend(),
std::ostream_iterator<unsigned char>(file));
printf("File saved: %s\n", filename);
}
}
namespace APIexample
{
std::vector<unsigned char> download_bytes(const char *url)
{
http_client client(U(url));
......@@ -50,14 +113,21 @@ namespace http_helper
return result;
}
std::string post_get_string(const char *url, std::string body)
void download_file(const char *url, const char *filename)
{
auto bytes = download_bytes(url);
file_helper::write_file_bytes(filename, bytes);
}
std::string upload_file(const char *filename, const char *url)
{
http_client client(U(url));
http_request request(methods::POST);
std::string result;
http_request req(methods::POST);
req.set_body(body, "application/json");
pplx::task<void> requestTask = client.request(req)
request.headers().add("Content-Type", "application/octet-stream");
request.set_body(file_helper::read_file_bytes(filename));
printf("body set as bytes\n");
pplx::task<void> requestTask = client.request(request)
.then([&result](http_response response) {
stringstreambuf ss;
result = response.extract_string().get();
......@@ -66,35 +136,21 @@ namespace http_helper
return result;
}
web::json::value get_json(const char *url)
{
web::json::value result;
http_client client(U(url));
http_request req(methods::GET);
pplx::task<void> requestTask = client.request(req)
.then([&result](http_response response) {
response.headers().set_content_type("application/json");
result = response.extract_json().get();
});
requestTask.wait();
return result;
}
web::json::value process(web::json::value data, std::string url)
{
auto doc = data.serialize();
auto taskid = post_get_string((url + "/startTask").c_str(), doc);
auto taskid = http_helper::post_get_string((url + "/startTask").c_str(), doc);
std::this_thread::sleep_for(std::chrono::milliseconds(200));
auto resp = get_json((url + "/getStatus/" + taskid).c_str());
auto resp = http_helper::get_json((url + "/getStatus/" + taskid).c_str());
auto status = resp.at("status").as_string();
while (status == "QUEUE" or status == "PROCESSING")
{
printf("STATUS: %s \n", status.c_str());
std::this_thread::sleep_for(std::chrono::milliseconds(500));
resp = get_json((url + "/getStatus/" + taskid).c_str());
resp = http_helper::get_json((url + "/getStatus/" + taskid).c_str());
status = resp.at("status").as_string();
}
if (status == "ERROR")
......@@ -103,4 +159,5 @@ namespace http_helper
}
return resp.at("value");
}
}
\ No newline at end of file
};
\ No newline at end of file
#pragma once
#include <vector>
#include <fstream>
#include <iterator>
namespace file_helper
{
std::vector<unsigned char> readFileBytes(const char *filename)
{
// open the file:
std::ifstream file(filename, std::ios::binary);
if (file.is_open())
{
printf("Opened file %s \n", filename);
}
else
{
throw std::runtime_error("Couldn't open the file");
}
// Stop eating new lines in binary mode!!!
file.unsetf(std::ios::skipws);
// get its size:
std::streampos fileSize;
file.seekg(0, std::ios::end);
fileSize = file.tellg();
file.seekg(0, std::ios::beg);
// reserve capacity
std::vector<unsigned char> vec;
vec.reserve(fileSize);
// read the data:
vec.insert(vec.begin(),
std::istream_iterator<unsigned char>(file),
std::istream_iterator<unsigned char>());
printf("file read\n");
return vec;
}
void writeFileBytes(const char *filename, std::vector<unsigned char> &fileBytes)
{
std::ofstream file(filename, std::ios::out | std::ios::binary);
std::copy(fileBytes.cbegin(), fileBytes.cend(),
std::ostream_iterator<unsigned char>(file));
printf("File saved: %s\n", filename);
}
}
\ No newline at end of file
#include "http_helper.hpp"
#include "api_example.hpp"
using namespace std::literals;
int main(int argc, char* argv[])
{
auto url = "http://ws.clarin-pl.eu/nlprest2/base"s;
auto input_filename = "paczka.zip";
auto input_filename = "input.zip";
auto output_filename = "output.zip";
auto task = "any2txt|wcrft2|wsd"s;
auto user = "email@email.com"s; // Please use your email
try
{
auto fileid = http_helper::upload_file(input_filename,(url+"/upload").c_str());
auto fileid = APIexample::upload_file(input_filename,(url+"/upload").c_str());
auto lpmn = "filezip("+fileid+")|"+task+"|dir|makezip";
web::json::value data;
data["lpmn"] = web::json::value::string(lpmn);
data["user"] = web::json::value::string(user);
data = http_helper::process(data,url);
data = APIexample::process(data,url);
if (!data.is_null())
{
data = data[0]["fileID"];
auto content = http_helper::download_bytes((url+"/download"+data.as_string()).c_str());
writeFileBytes(output_filename,content);
APIexample::download_file((url+"/download"+data.as_string()).c_str(),output_filename);
}
}
catch (const std::exception &e)
......
import json
import urllib2
import glob
import os
import time
user="bartosz.ziemba@pwr.edu.pl"
task="any2txt|wcrft2|wsd"
url="http://ws.clarin-pl.eu/nlprest2/base"
def upload(file):
with open (file, "rb") as myfile:
doc=myfile.read()
return urllib2.urlopen(urllib2.Request(url+'/upload/',doc,{'Content-Type': 'binary/octet-stream'})).read();
def process(data):
doc=json.dumps(data)
taskid = urllib2.urlopen(urllib2.Request(url+'/startTask/',doc,{'Content-Type': 'application/json'})).read();
print(url+'/startTask/')
print(doc)
time.sleep(0.2);
resp = urllib2.urlopen(urllib2.Request(url+'/getStatus/'+taskid));
data=json.load(resp)
while data["status"] == "QUEUE" or data["status"] == "PROCESSING" :
time.sleep(0.5);
resp = urllib2.urlopen(urllib2.Request(url+'/getStatus/'+taskid));
data=json.load(resp)
if data["status"]=="ERROR":
print("Error "+data["value"]);
return None;
return data["value"]
def main():
in_file = 'paczka.zip'
out_file= 'out.zip'
global_time = time.time()
print("upload 1")
fileid=upload(in_file)
print("upload 2")
lpmn='filezip('+fileid+')|'+task+'|dir|makezip'
data={'lpmn':lpmn,'user':user}
print("process 1")
data=process(data)
print("process 2")
if data!=None:
data=data[0]["fileID"]
content = urllib2.urlopen(urllib2.Request(url+'/download'+data)).read()
with open (out_file, "w") as outfile:
outfile.write(content)
print("GLOBAL %s seconds ---" % (time.time() - global_time))
main()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment