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