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#include <filesystem>
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::filesystem::path 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::filesystem::path 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
80
85 MRVIEWER_API void send( std::string url, std::string logName, ResponseCallback callback, bool async = true );
86 MRVIEWER_API void send( ResponseCallback callback );
87
89 MRVIEWER_API static void waitRemainingAsync();
90
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::filesystem::path inputPath_;
100 std::vector<FormData> formData_;
101 std::string body_;
102 std::filesystem::path outputPath_;
103 ProgressCallback uploadCallback_;
104 ProgressCallback downloadCallback_;
105};
106
107}
108#endif
Definition MRWebRequest.h:18
MRVIEWER_API void setAsync(bool async)
WebRequest()=default
MRVIEWER_API void send(ResponseCallback callback)
MRVIEWER_API void clear()
MR::WebResponseCallback ResponseCallback
Definition MRWebRequest.h:79
MRVIEWER_API void setFormData(std::vector< FormData > formData)
MRVIEWER_API void setOutputPath(std::filesystem::path outputPath)
MRVIEWER_API void setInputPath(std::filesystem::path inputPath)
MRVIEWER_API void setBody(std::string body)
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:24
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 MRMeshFwd.h:742
Definition MRCameraOrientationPlugin.h:8
tl::expected< T, E > Expected
Definition MRExpected.h:28
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: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