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{
15
17MRVIEWER_API Expected<Json::Value> parseResponse( const Json::Value& response );
18
20class MRVIEWER_CLASS WebRequest
21{
22public:
23 WebRequest() = default;
24 MRVIEWER_API explicit WebRequest( std::string url );
25
26 enum class Method
27 {
28 Get,
29 Post,
30 Patch,
31 Put,
32 Delete,
33 };
34
36 MRVIEWER_API void clear();
37
39 MRVIEWER_API void setMethod( Method method );
40
42 MRVIEWER_API void setTimeout( int timeoutMs );
43
45 MRVIEWER_API void setParameters( std::unordered_map<std::string, std::string> parameters );
46
48 MRVIEWER_API void setHeaders( std::unordered_map<std::string, std::string> headers );
49
51 MRVIEWER_API void setInputPath( std::filesystem::path inputPath );
52
55 MRVIEWER_API void setUploadProgressCallback( ProgressCallback callback );
56
58 struct FormData
59 {
60 std::string path;
61 std::string contentType;
62 std::string name;
63 std::string fileName;
64 };
65 MRVIEWER_API void setFormData( std::vector<FormData> formData );
66
68 MRVIEWER_API void setBody( std::string body );
69
71 MRVIEWER_API void setOutputPath( std::filesystem::path outputPath );
72
74 MRVIEWER_API void setDownloadProgressCallback( ProgressCallback callback );
75
77 MRVIEWER_API void setAsync( bool async );
78
80 MRVIEWER_API void setLogName( std::string logName );
81
83
88 MRVIEWER_API void send( std::string url, std::string logName, ResponseCallback callback, bool async = true );
89 MRVIEWER_API void send( ResponseCallback callback );
90
92 MRVIEWER_API static void waitRemainingAsync();
93
94private:
95 Method method_{ Method::Get };
96 std::string url_;
97 std::string logName_;
98 bool async_{ true };
99 int timeout_{ 10000 };
100 std::unordered_map<std::string, std::string> params_;
101 std::unordered_map<std::string, std::string> headers_;
102 std::filesystem::path inputPath_;
103 std::vector<FormData> formData_;
104 std::string body_;
105 std::filesystem::path outputPath_;
106 ProgressCallback uploadCallback_;
107 ProgressCallback downloadCallback_;
108};
109
110}
111#endif
this class is needed to unify cpp and wasm requests
Definition MRWebRequest.h:21
std::string path
Definition MRWebRequest.h:60
MRVIEWER_API void setAsync(bool async)
set async mode (return immediately after sending request)
WebRequest()=default
MRVIEWER_API void send(ResponseCallback callback)
MRVIEWER_API void clear()
clear all request data
MR::WebResponseCallback ResponseCallback
Definition MRWebRequest.h:82
MRVIEWER_API void setFormData(std::vector< FormData > formData)
MRVIEWER_API void setOutputPath(std::filesystem::path outputPath)
prefer to save the response to file
tl::expected< T, E > Expected
Definition MRExpected.h:31
MRVIEWER_API void setInputPath(std::filesystem::path inputPath)
set path to the file to upload
std::string name
Definition MRWebRequest.h:62
MRVIEWER_API void setBody(std::string body)
set payload in plain format
MRVIEWER_API void setUploadProgressCallback(ProgressCallback callback)
std::string fileName
Definition MRWebRequest.h:63
std::function< void(const Json::Value &response)> WebResponseCallback
Definition MRWebResponseCallback.h:12
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)
set HTTP method
Method
Definition MRWebRequest.h:27
MRVIEWER_API void setHeaders(std::unordered_map< std::string, std::string > headers)
set HTTP headers
MRVIEWER_API void setLogName(std::string logName)
set log name
MRVIEWER_API Expected< Json::Value > parseResponse(const Json::Value &response)
returns json value of text or error if response failed
static MRVIEWER_API void waitRemainingAsync()
if any async request is still in progress, wait for it
MRVIEWER_API void setDownloadProgressCallback(ProgressCallback callback)
set progress callback for download
std::string contentType
Definition MRWebRequest.h:61
MRVIEWER_API void setParameters(std::unordered_map< std::string, std::string > parameters)
set URL query parameters
MRVIEWER_API void setTimeout(int timeoutMs)
set timeout in milliseconds
only for bindings generation
Definition MRCameraOrientationPlugin.h:8
set payload in multipart/form-data format
Definition MRWebRequest.h:59