Loading...
Searching...
No Matches
function.h
1
2#ifndef NAF_FUNCTIONS_FUNCTION
3#define NAF_FUNCTIONS_FUNCTION
4
5
6#include <functional>
7#include <string>
8#include <vector>
9#include <map>
10#include <memory>
11
12#include "files/file_manager.h"
13#include "functions/action.h"
14#include "http/methods.h"
15#include "query/parameter.h"
16#include "http/request.h"
17#include "http/common_responses.h"
18#include "security/user.h"
19#include "security/users_manager.h"
20
21
22namespace NAF
23{
24 namespace Functions
25 {
26 class Function;
27 }
28}
29
30
32{
33 public:
34 using Ptr = std::shared_ptr<Functions::Function>;
35
36 enum class ResponseType
37 {
38 kJSON
39 ,kFile
40 ,kCustom
41 };
42
43 Function();
44 Function(std::string endpoint, HTTP::EnumMethods method, ResponseType response_type = ResponseType::kJSON);
45
46 std::string get_endpoint() const { return endpoint_; }
47 std::string get_target() const { return target_; }
48 bool get_error() const { return error_; }
49 std::string get_error_message() const { return error_message_; }
50 bool get_remove_file_on_modify() const { return remove_file_on_modify_; }
51 ResponseType get_response_type() const { return response_type_; }
52 HTTP::EnumMethods get_method() const { return method_; }
53 Security::User& get_current_user()
54 {
55 auto& var = current_user_;
56 return var;
57 }
58 std::vector<Action::Ptr>& get_actions()
59 {
60 auto& var = actions_;
61 return var;
62 }
63 HTTP::Methods& get_methods()
64 {
65 auto& var = methods_;
66 return var;
67 }
68 Files::FileManager::Ptr& get_file_manager()
69 {
70 auto& var = file_manager_;
71 return var;
72 }
73 std::vector<Query::Parameter::Ptr>& get_parameters()
74 {
75 auto& var = parameters_;
76 return var;
77 }
78
79 void set_endpoint(std::string endpoint) { endpoint_ = endpoint; }
80 void set_target(std::string target) { target_ = target; }
81 void set_remove_file_on_modify(bool remove_file_on_modify) { remove_file_on_modify_ = remove_file_on_modify; }
82 void set_method(HTTP::EnumMethods type) { method_ = type; }
83 void set_response_type(ResponseType response_type) { response_type_ = response_type; }
84
85 std::vector<Query::Parameter::Ptr>::iterator GetParameter_(std::string name);
86 Action::Ptr AddAction_(std::string identifier);
87 std::vector<Action::Ptr>::iterator GetAction_(std::string identifier);
88 void Process_(HTTP::Request::HTTPServerRequestPtr request, HTTP::Request::HTTPServerResponsePtr response);
89 bool ProcessAction_(Action::Ptr action);
90 bool ProcessJSON_(JSON::Object::Ptr& json_result);
91 bool ProcessFile_(std::string& filepath);
92 void DownloadProcess_(std::string& filepath);
93 void UploadProcess_();
94 void ModifyProcess_(std::string& filepath);
95 void RemoveProcess_(std::string& filepath);
96 void SetupCustomProcess_(std::function<void(Function&)> custom_process);
97 void IdentifyParameters_(Functions::Action::Ptr action);
98
99 protected:
100 void Setup_(HTTP::Request::HTTPServerRequestPtr request, HTTP::Request::HTTPServerResponsePtr response);
101
102 private:
103 std::string endpoint_;
104 std::string target_;
105 bool error_;
106 std::string error_message_;
107 bool remove_file_on_modify_;
108 ResponseType response_type_;
109 HTTP::EnumMethods method_;
110 Security::User current_user_;
111 std::vector<Action::Ptr> actions_;
112 HTTP::Methods methods_;
113 Files::FileManager::Ptr file_manager_;
114 std::function<void(Function&)> custom_process_;
115 std::vector<Query::Parameter::Ptr> parameters_;
116};
117
118
119#endif // NAF_FUNCTIONS_FUNCTION
Definition function.h:32
Definition common_responses.h:69
Definition methods.h:60
Definition user.h:37