-
Notifications
You must be signed in to change notification settings - Fork 216
Expand file tree
/
Copy pathSimpleHttpModule.h
More file actions
84 lines (67 loc) · 3.08 KB
/
SimpleHttpModule.h
File metadata and controls
84 lines (67 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#pragma once
#include "pch.h"
#include "resource.h"
#if __has_include("codegen/NativeSimpleHttpModuleDataTypes.g.h")
#include "codegen/NativeSimpleHttpModuleDataTypes.g.h"
#endif
#include "codegen/NativeSimpleHttpModuleSpec.g.h"
#include "JSValue.h"
#include "NativeModules.h"
#include <functional>
#include <sstream>
#include <string>
namespace winrt::NativeModuleSample {
REACT_TURBO_MODULE(SimpleHttpModule);
struct SimpleHttpModule {
using ModuleSpec = NativeModuleSampleCodegen::SimpleHttpModuleSpec;
using Response = NativeModuleSampleCodegen::SimpleHttpModuleSpec_Response;
// An example asynchronous method which uses asynchronous Windows APIs to make a
// http request to the given url and resolve the given promise with the result
static winrt::Windows::Foundation::IAsyncAction GetHttpResponseAsync(
std::string uri,
winrt::Microsoft::ReactNative::ReactPromise<Response> promise) noexcept {
// Capture the promise to make sure it doesn't get cleaned up
// during the asynchronous calls below
auto capturedPromise = promise;
// Create an HttpClient object
auto httpClient = winrt::Windows::Web::Http::HttpClient();
// Send the GET request asynchronously
auto httpResponseMessage = co_await httpClient.GetAsync(winrt::Windows::Foundation::Uri(winrt::to_hstring(uri)));
// Parse response
auto statusCode = httpResponseMessage.StatusCode();
auto content = co_await httpResponseMessage.Content().ReadAsStringAsync();
// Build result object
auto resultObject = Response();
resultObject.statusCode = static_cast<int>(statusCode);
resultObject.content = winrt::to_string(content);
capturedPromise.Resolve(std::move(resultObject));
}
// An example method which provides a promise-based JS method on one side
// which seamlessly calls native asynchronous code without any blocking
//
// Example JS:
//
// NativeModules.SimpleHttpModule.GetHttpResponse('https://microsoft.github.io/react-native-windows/')
// .then(result => console.log(result))
// .catch(error => console.log(error));
REACT_METHOD(GetHttpResponse);
void GetHttpResponse(std::string uri, winrt::Microsoft::ReactNative::ReactPromise<Response> promise) noexcept {
// Here we're simply starting our asynchronous method
// and returning back to the caller
auto asyncOp = GetHttpResponseAsync(uri, promise);
asyncOp.Completed([promise](auto action, auto status) {
// Here we handle any unhandled exceptions thrown during the
// asynchronous call by rejecting the promise with the error code
if (status == winrt::Windows::Foundation::AsyncStatus::Error) {
std::stringstream errorCode;
errorCode << "0x" << std::hex << action.ErrorCode() << std::endl;
auto error = winrt::Microsoft::ReactNative::ReactError();
error.Message = "HRESULT " + errorCode.str() + ": " + std::system_category().message(action.ErrorCode());
promise.Reject(error);
}
});
}
};
} // namespace winrt::NativeModuleSample