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