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 
22 namespace 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 
74  void set_endpoint(std::string endpoint) { endpoint_ = endpoint; }
75  void set_target(std::string target) { target_ = target; }
76  void set_remove_file_on_modify(bool remove_file_on_modify) { remove_file_on_modify_ = remove_file_on_modify; }
77  void set_method(HTTP::EnumMethods type) { method_ = type; }
78  void set_response_type(ResponseType response_type) { response_type_ = response_type; }
79 
80  Action::Ptr AddAction_(std::string identifier);
81  void Process_(HTTP::Request::HTTPServerRequestPtr request, HTTP::Request::HTTPServerResponsePtr response);
82  bool ProcessAction_(Action::Ptr action);
83  bool ProcessJSON_(JSON::Object::Ptr& json_result);
84  bool ProcessFile_(std::string& filepath);
85  void DownloadProcess_(std::string& filepath);
86  void UploadProcess_();
87  void ModifyProcess_(std::string& filepath);
88  void RemoveProcess_(std::string& filepath);
89  void SetupCustomProcess_(std::function<void(Function&)> custom_process);
90 
91  protected:
92  void Setup_(HTTP::Request::HTTPServerRequestPtr request, HTTP::Request::HTTPServerResponsePtr response);
93 
94  private:
95  std::string endpoint_;
96  std::string target_;
97  bool error_;
98  std::string error_message_;
99  bool remove_file_on_modify_;
100  ResponseType response_type_;
101  HTTP::EnumMethods method_;
102  Security::User current_user_;
103  std::vector<Action::Ptr> actions_;
104  HTTP::Methods methods_;
105  Files::FileManager::Ptr file_manager_;
106  std::function<void(Function&)> custom_process_;
107 };
108 
109 
110 #endif // NAF_FUNCTIONS_FUNCTION
Definition: function.h:32
Definition: common_responses.h:69
Definition: methods.h:60
Definition: user.h:37