MeshLib
 
Loading...
Searching...
No Matches
MRWebRequest.h
Go to the documentation of this file.
1#pragma once
2#include "MRMesh/MRMeshFwd.h"
3#if defined( __EMSCRIPTEN__ ) || !defined( MRMESH_NO_CPR )
4#include "MRViewerFwd.h"
5#include "MRMesh/MRExpected.h"
6#include "MRPch/MRJson.h"
7#include <unordered_map>
8#include <string>
9#include <functional>
10
11namespace MR
12{
13// returns json value of text or error if response failed
14MRVIEWER_API Expected<Json::Value> parseResponse( const Json::Value& response );
15
16// this class is needed to unify cpp and wasm requests
17class MRVIEWER_CLASS WebRequest
18{
19public:
20 WebRequest() = default;
21 MRVIEWER_API explicit WebRequest( std::string url );
22
23 enum class Method
24 {
25 Get,
26 Post,
27 Patch,
28 Put,
29 Delete,
30 };
31
32 // clear all request data
33 MRVIEWER_API void clear();
34
35 // set HTTP method
36 MRVIEWER_API void setMethod( Method method );
37
38 // set timeout in milliseconds
39 MRVIEWER_API void setTimeout( int timeoutMs );
40
41 // set URL query parameters
42 MRVIEWER_API void setParameters( std::unordered_map<std::string, std::string> parameters );
43
44 // set HTTP headers
45 MRVIEWER_API void setHeaders( std::unordered_map<std::string, std::string> headers );
46
47 // set path to the file to upload
48 MRVIEWER_API void setInputPath( std::string inputPath );
49
50 // set progress callback for upload
51 // NOTE: due to limitations, the upload callback won't work on web platforms when `setOutputPath` method is called
52 MRVIEWER_API void setUploadProgressCallback( ProgressCallback callback );
53
54 // set payload in multipart/form-data format
55 struct FormData
56 {
57 std::string path;
58 std::string contentType;
59 std::string name;
60 std::string fileName;
61 };
62 MRVIEWER_API void setFormData( std::vector<FormData> formData );
63
64 // set payload in plain format
65 MRVIEWER_API void setBody( std::string body );
66
67 // prefer to save the response to file
68 MRVIEWER_API void setOutputPath( std::string outputPath );
69
70 // set progress callback for download
71 MRVIEWER_API void setDownloadProgressCallback( ProgressCallback callback );
72
73 // set async mode (return immediately after sending request)
74 MRVIEWER_API void setAsync( bool async );
75
76 // set log name
77 MRVIEWER_API void setLogName( std::string logName );
78
79 using ResponseCallback = std::function<void( const Json::Value& response )>;
80
85 MRVIEWER_API void send( std::string url, const std::string & logName, ResponseCallback callback, bool async = true );
86 MRVIEWER_API void send( ResponseCallback callback );
87
88private:
89 Method method_{ Method::Get };
90 std::string url_;
91 std::string logName_;
92 bool async_{ true };
93 int timeout_{ 10000 };
94 std::unordered_map<std::string, std::string> params_;
95 std::unordered_map<std::string, std::string> headers_;
96 std::string inputPath_;
97 std::vector<FormData> formData_;
98 std::string body_;
99 std::string outputPath_;
100 ProgressCallback uploadCallback_;
101 ProgressCallback downloadCallback_;
102};
103
104}
105#endif
Definition MRWebRequest.h:18
MRVIEWER_API void setOutputPath(std::string outputPath)
MRVIEWER_API void setAsync(bool async)
WebRequest()=default
MRVIEWER_API void send(ResponseCallback callback)
MRVIEWER_API void clear()
std::function< void(const Json::Value &response)> ResponseCallback
Definition MRWebRequest.h:79
MRVIEWER_API void setFormData(std::vector< FormData > formData)
MRVIEWER_API void setBody(std::string body)
MRVIEWER_API void setInputPath(std::string inputPath)
MRVIEWER_API void setUploadProgressCallback(ProgressCallback callback)
MRVIEWER_API void send(std::string url, const std::string &logName, ResponseCallback callback, bool async=true)
MRVIEWER_API WebRequest(std::string url)
MRVIEWER_API void setMethod(Method method)
Method
Definition MRWebRequest.h:24
MRVIEWER_API void setHeaders(std::unordered_map< std::string, std::string > headers)
MRVIEWER_API void setLogName(std::string logName)
MRVIEWER_API void setDownloadProgressCallback(ProgressCallback callback)
MRVIEWER_API void setParameters(std::unordered_map< std::string, std::string > parameters)
MRVIEWER_API void setTimeout(int timeoutMs)
std::function< bool(float)> ProgressCallback
Definition MRMesh/MRMeshFwd.h:589
Definition MRCameraOrientationPlugin.h:7
tl::expected< T, E > Expected
Definition MRExpected.h:49
MRVIEWER_API Expected< Json::Value > parseResponse(const Json::Value &response)
Definition MRWebRequest.h:56
std::string path
Definition MRWebRequest.h:57
std::string name
Definition MRWebRequest.h:59
std::string fileName
Definition MRWebRequest.h:60
std::string contentType
Definition MRWebRequest.h:58