cppDataPipeline 0.3.0
C++ Implementation of the FAIR Data Pipeline API
Loading...
Searching...
No Matches
api.hxx
1
11#ifndef __FDP_API_HXX__
12#define __FDP_API_HXX__
13
14#include <algorithm>
15#include <curl/curl.h>
16#include <ghc/filesystem.hpp>
17#include <iostream>
18#include <iterator>
19#include <json/reader.h>
20#include <map>
21#include <regex>
22#include <string>
23#include <vector>
24
25#include "fdp/exceptions.hxx"
26#include "fdp/objects/api_object.hxx"
27#include "fdp/utilities/json.hxx"
28#include "fdp/utilities/logging.hxx"
29
30namespace FairDataPipeline {
37enum class RESTAPI {
38 REMOTE,
39 LOCAL
40};
41
42#if 0
53size_t write_func_(void *ptr, size_t size, size_t nmemb, std::string *data);
54#endif
55
65class API {
66public:
67 typedef std::shared_ptr< API > sptr;
68
75 static sptr construct( const std::string& url_root );
76
77
87 Json::Value get_request(const std::string &addr_path,
88 long expected_response = 200, std::string token = "");
89
90 Json::Value patch(const std::string addr_path, Json::Value &post_data,
91 const std::string &token, long expected_response = 200);
102 Json::Value post(const std::string addr_path, Json::Value &post_data,
103 const std::string &token, long expected_response = 201);
104
105 Json::Value post_storage_root(Json::Value &post_data, const std::string &token);
106
107 Json::Value post_file_type(Json::Value &post_data, const std::string &token);
108
109 Json::Value get_by_json_query(const std::string &addr_path,
110 Json::Value &query_data,
111 long expected_response = 200,
112 std::string token = "");
113
122 Json::Value get_by_id(const std::string &table, int const &id,
123 long expected_response = 200, std::string token = "");
124
131 std::string get_url_root() const { return url_root_; }
132
141 std::string json_to_query_string(Json::Value &json_value);
142
151 std::string escape_space(std::string &str);
152
160 static std::string append_with_forward_slash(std::string string);
161
168 static std::string remove_leading_forward_slash(std::string str);
169
170private:
171 API( const std::string& url_root)
172 : url_root_(API::append_with_forward_slash(url_root)) {}
173
174 std::string url_root_;
175 CURL *setup_json_session_(std::string &addr_path, std::string *response,
176 long &http_code, std::string token = "");
177 CURL *setup_download_session_(const ghc::filesystem::path &addr_path,
178 FILE *file);
179
180 Json::Value post_patch_request(const std::string addr_path, Json::Value &post_data,
181 const std::string &token, long expected_response, bool PATCH = false);
182
183 // Legacy Method
184 Json::Value get_request(const ghc::filesystem::path &addr_path,
185 long expected_response = 200, std::string token = "");
186
187 void download_file(const ghc::filesystem::path &url,
188 ghc::filesystem::path out_path);
189};
190
191std::string url_encode(const std::string& url);
192
193}; // namespace FairDataPipeline
194
195#endif
a class which handles the fetching and posting of information to the FDP data pipeline RestAPI
Definition: api.hxx:65
std::string json_to_query_string(Json::Value &json_value)
Formats a json object into a string representation which can be used as a url endpoint query.
Definition: api.cxx:165
static std::string remove_leading_forward_slash(std::string str)
Remove the leading forward slash from a given string.
Definition: api.cxx:347
static sptr construct(const std::string &url_root)
construct an API object using the given URL as the root
Definition: api.cxx:16
static std::string append_with_forward_slash(std::string string)
Static function to append a given string with a "/" unless the string is empty or already ends with a...
Definition: api.cxx:334
Json::Value get_request(const std::string &addr_path, long expected_response=200, std::string token="")
sends the given 'packet' of information to the RestAPI
Definition: api.cxx:102
std::string escape_space(std::string &str)
escapes space " " characters within a string with it's html character code (%20)
Definition: api.cxx:201
std::string get_url_root() const
returns the root URL for the RestAPI used by the API instance
Definition: api.hxx:131
Json::Value post(const std::string addr_path, Json::Value &post_data, const std::string &token, long expected_response=201)
post information from a JSON value object to the RestAPI
Definition: api.cxx:206
Json::Value get_by_id(const std::string &table, int const &id, long expected_response=200, std::string token="")
get an object from the api by it's id
Definition: api.cxx:158