Molybden API
Loading...
Searching...
No Matches
upload_data.hpp
1// Copyright (c) 2000-2024 TeamDev. All rights reserved.
2// TeamDev PROPRIETARY and CONFIDENTIAL.
3// Use is subject to license terms.
4
5#ifndef MOLYBDEN_UPLOAD_DATA_HPP
6#define MOLYBDEN_UPLOAD_DATA_HPP
7
8#include <memory>
9#include <string>
10#include <utility>
11#include <vector>
12
13namespace molybden {
14
18enum UploadDataType { kByteData, kTextData, kFormData, kMultipartFormData };
19
24 public:
25 virtual ~UploadData() = default;
26
45 template <class S>
46 std::shared_ptr<S> as();
47
48 protected:
49 UploadData() = default;
50
54 virtual UploadDataType type() const = 0;
55};
56
60class ByteData : public UploadData {
61 public:
62 static std::shared_ptr<ByteData> create(std::string value);
63
67 virtual const std::string& value() const = 0;
68
72 virtual void swap(std::string* value) = 0;
73};
74
78class TextData : public UploadData {
79 public:
80 static std::shared_ptr<TextData> create(std::string value);
81
85 virtual std::string value() const = 0;
86};
87
93 explicit FormDataPair(std::string key, std::string value = "");
94
98 const std::string key;
99
103 const std::string value;
104};
105
109class FormData : public UploadData {
110 public:
111 static std::shared_ptr<FormData> create(std::vector<FormDataPair> value);
112
117 virtual std::vector<FormDataPair> pairs() const = 0;
118};
119
123enum MultipartFormDataPairValueType { kString, kFile };
124
129 public:
130 virtual ~MultipartFormDataPairValue() = default;
131
146 template <class S>
147 std::shared_ptr<S> as();
148
149 protected:
150 MultipartFormDataPairValue() = default;
151
155 virtual MultipartFormDataPairValueType type() const = 0;
156};
157
162 explicit MultipartFormDataPair(
163 std::string key,
164 std::shared_ptr<MultipartFormDataPairValue> value = {});
165
169 const std::string key;
170
174 const std::shared_ptr<MultipartFormDataPairValue> value;
175};
176
180enum FileValueType { kPath, kBytes };
181
186 public:
187 virtual ~FileValue() = default;
188
202 template <class S>
203 std::shared_ptr<S> as();
204
205 protected:
206 FileValue() = default;
207
211 virtual FileValueType type() const = 0;
212};
213
217class FilePath : public FileValue {
218 public:
219 static std::shared_ptr<FilePath> create(std::string value);
220
224 virtual std::string value() const = 0;
225};
226
230class FileBytes : public FileValue {
231 public:
232 static std::shared_ptr<FileBytes> create(std::string value);
233
237 virtual const std::string& value() const = 0;
238
242 virtual void swap(std::string* value) = 0;
243};
244
255struct File {
259 std::string name;
260
266 std::string content_type;
267
271 std::shared_ptr<FileValue> value;
272};
273
278 public:
279 static std::shared_ptr<MultipartFormDataPairValueFile> create(File value);
280
284 virtual File value() const = 0;
285};
286
291 public:
292 static std::shared_ptr<MultipartFormDataPairValueString> create(
293 std::string value);
294
298 virtual std::string value() const = 0;
299};
300
305 public:
306 static std::shared_ptr<MultipartFormData> create(
307 std::vector<MultipartFormDataPair> value);
308
313 virtual std::vector<MultipartFormDataPair> pairs() const = 0;
314};
315
316} // namespace molybden
317
318#endif // MOLYBDEN_UPLOAD_DATA_HPP
An upload data as a sequence of bytes.
Definition upload_data.hpp:60
virtual void swap(std::string *value)=0
Moves the upload data bytes to the given value.
virtual const std::string & value() const =0
Returns the upload data bytes or an empty string if the bytes were swapped.
The file bytes value.
Definition upload_data.hpp:230
virtual void swap(std::string *value)=0
Moves the file bytes to the given value.
virtual const std::string & value() const =0
Returns the file bytes or an empty string if the bytes were swapped.
The file path value.
Definition upload_data.hpp:217
virtual std::string value() const =0
Returns the absolute or relative file path.
The base class that all file value types must implement.
Definition upload_data.hpp:185
std::shared_ptr< S > as()
Casts this file value to the target type.
virtual FileValueType type() const =0
Returns the current file value type.
An upload data of the "application/x-www-form-urlencoded" content type.
Definition upload_data.hpp:109
virtual std::vector< FormDataPair > pairs() const =0
Returns the list of the key-value pairs each representing a segment of a form data.
An upload data of the "multipart/form-data" content type.
Definition upload_data.hpp:304
virtual std::vector< MultipartFormDataPair > pairs() const =0
Returns the list of the key-value pairs each representing a segment of a multi-part form data.
The multi-part form data pair file value.
Definition upload_data.hpp:277
virtual File value() const =0
Returns the file value.
The base class that all pair value types must implement.
Definition upload_data.hpp:128
virtual MultipartFormDataPairValueType type() const =0
Returns the current value type.
std::shared_ptr< S > as()
Casts this pair value to the target type.
The multi-part form data pair string value.
Definition upload_data.hpp:290
virtual std::string value() const =0
Returns the string value.
An upload data of the "text/plain" content type.
Definition upload_data.hpp:78
virtual std::string value() const =0
The text representation of the upload data.
An interface that all upload data implementations must extend.
Definition upload_data.hpp:23
std::shared_ptr< S > as()
Casts this upload data to the target type.
virtual UploadDataType type() const =0
Returns the current upload data type.
The file data.
Definition upload_data.hpp:255
std::string name
The file name including extension.
Definition upload_data.hpp:259
std::shared_ptr< FileValue > value
The file value.
Definition upload_data.hpp:271
std::string content_type
The content type determined by the file extension.
Definition upload_data.hpp:266
The key-value pair that represents a segment of a form data (e.g.
Definition upload_data.hpp:92
const std::string value
The form data segment value.
Definition upload_data.hpp:103
const std::string key
The form data segment key.
Definition upload_data.hpp:98
The key-value pair that represents a segment of a multi-part form data.
Definition upload_data.hpp:161
const std::string key
The form content segment key.
Definition upload_data.hpp:169
const std::shared_ptr< MultipartFormDataPairValue > value
The form content segment value.
Definition upload_data.hpp:174