DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
RestClient.h
Go to the documentation of this file.
1
3
4#pragma once
5
6#include "Common.h"
7#include "System/Object.h"
8#include "System/String.h"
13
14#include "System/Convert.h"
15
16namespace DotNetDupe {
17 namespace System {
18 namespace Net {
19 namespace Http {
20
26 template <typename TResource>
27 class RestClient : public virtual DotNetDupe::System::Object {
28 private:
30 HttpClient m_client;
31
32 public:
35 RestClient(const DotNetDupe::System::String& sBaseUrl) : m_sBaseUrl(sBaseUrl) {
36 m_client.GetDefaultRequestHeaders().Add("accept", "application/json");
37 }
38
40 ~RestClient() override = default;
41
45 auto& headers = m_client.GetDefaultRequestHeaders();
46 headers.Remove("Authorization");
47 headers.Remove("authorization");
48 headers.Add("Authorization", DotNetDupe::System::String("Bearer ") + sToken);
49 }
50
55 DotNetDupe::System::String credentials = sUsername + ":" + sPassword;
56 int len = credentials.GetLength();
58 for (int i = 0; i < len; ++i) {
59 arr[i] = credentials[i];
60 }
62
63 auto& headers = m_client.GetDefaultRequestHeaders();
64 headers.Remove("Authorization");
65 headers.Remove("authorization");
66 headers.Add("Authorization", DotNetDupe::System::String("Basic ") + base64);
67 }
68
71 auto& headers = m_client.GetDefaultRequestHeaders();
72 headers.Remove("Authorization");
73 headers.Remove("authorization");
74 }
75
80 auto& headers = m_client.GetDefaultRequestHeaders();
81 headers.Remove(sName);
82 headers.Add(sName, sValue);
83 }
84
88 m_client.GetDefaultRequestHeaders().Remove(sName);
89 }
90
94 auto resp = m_client.Get(m_sBaseUrl);
95 resp->EnsureSuccessStatusCode();
96 DotNetDupe::System::String body = resp->GetContent()->ReadAsString();
97 return DotNetDupe::System::Text::Json::JsonSerializer::template Deserialize<DotNetDupe::System::Collections::Generic::List<TResource>>(body);
98 }
99
103 TResource Get(const DotNetDupe::System::String& sId) {
104 DotNetDupe::System::String url = m_sBaseUrl;
105 if (!url.EndsWith('/', false)) {
106 url = url + "/";
107 }
108 url = url + sId;
109 auto resp = m_client.Get(url);
110 resp->EnsureSuccessStatusCode();
111 DotNetDupe::System::String body = resp->GetContent()->ReadAsString();
112 return DotNetDupe::System::Text::Json::JsonSerializer::template Deserialize<TResource>(body);
113 }
114
118 DotNetDupe::System::String Post(const TResource& resource) {
120 auto content = DotNetDupe::System::SmartPointer<StringContent>::NewShared(json, "application/json");
121 auto resp = m_client.Post(m_sBaseUrl, content);
122 resp->EnsureSuccessStatusCode();
123 return resp->GetContent()->ReadAsString();
124 }
125
130 template <typename TResult = TResource>
131 TResult PostAndReturn(const TResource& resource) {
133 auto content = DotNetDupe::System::SmartPointer<StringContent>::NewShared(json, "application/json");
134 auto resp = m_client.Post(m_sBaseUrl, content);
135 resp->EnsureSuccessStatusCode();
136 DotNetDupe::System::String body = resp->GetContent()->ReadAsString();
137 return DotNetDupe::System::Text::Json::JsonSerializer::template Deserialize<TResult>(body);
138 }
139
144 DotNetDupe::System::String Put(const DotNetDupe::System::String& sId, const TResource& resource) {
145 DotNetDupe::System::String url = m_sBaseUrl;
146 if (!url.EndsWith('/', false)) {
147 url = url + "/";
148 }
149 url = url + sId;
151 auto content = DotNetDupe::System::SmartPointer<StringContent>::NewShared(json, "application/json");
152 auto resp = m_client.Put(url, content);
153 resp->EnsureSuccessStatusCode();
154 return resp->GetContent()->ReadAsString();
155 }
156
160 DotNetDupe::System::String url = m_sBaseUrl;
161 if (!url.EndsWith('/', false)) {
162 url = url + "/";
163 }
164 url = url + sId;
165 auto resp = m_client.Delete(url);
166 resp->EnsureSuccessStatusCode();
167 }
168 };
169
170 }
171 }
172 }
173}
Defines common cross-platform macros, export decorators, and fundamental types.
Converts a base data type to another base data type, and encodes/decodes Base64 data.
Provides a base class for sending HTTP requests and receiving HTTP responses mirroring ....
Provides functionality to serialize objects to JSON strings and deserialize JSON strings into objects...
Represents a strongly typed list of objects that can be accessed by index mirroring ....
Base object class for DotNetDupe mirroring .NET System.Object.
Provides reference-counted and weak pointer memory management primitives ensuring zero raw ownership.
High-performance UTF-8 / UTF-16 string manipulation class mirroring .NET System.String.
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition Array.h:29
Represents a strongly typed list of objects accessible by index.
Definition List.h:29
static String ToBase64String(const Array< char > &inArray)
Definition Convert.cpp:325
Sends HTTP requests and receives HTTP responses from a resource identified by a URI.
Definition HttpClient.h:40
~RestClient() override=default
Virtual destructor.
void Delete(const DotNetDupe::System::String &sId)
Sends DELETE request targeting a specific resource ID.
Definition RestClient.h:159
DotNetDupe::System::Collections::Generic::List< TResource > GetAll()
Sends GET request to endpoint and deserializes JSON response into a List of models.
Definition RestClient.h:93
RestClient(const DotNetDupe::System::String &sBaseUrl)
Initializes a new instance of RestClient with a base resource endpoint URL.
Definition RestClient.h:35
DotNetDupe::System::String Put(const DotNetDupe::System::String &sId, const TResource &resource)
Sends PUT request updating a specific resource ID and returns raw response string.
Definition RestClient.h:144
void SetBasicAuthentication(const DotNetDupe::System::String &sUsername, const DotNetDupe::System::String &sPassword)
Sets Basic Authentication (username:password) encoded via Base64 for HTTP Authorization header (RFC 7...
Definition RestClient.h:54
TResult PostAndReturn(const TResource &resource)
Sends POST request with serialized resource and deserializes response into TResult.
Definition RestClient.h:131
DotNetDupe::System::String Post(const TResource &resource)
Sends POST request with serialized resource and returns raw string response body.
Definition RestClient.h:118
void AddDefaultRequestHeader(const DotNetDupe::System::String &sName, const DotNetDupe::System::String &sValue)
Adds a custom default request header sent with all HTTP calls.
Definition RestClient.h:79
void ClearAuthentication()
Clears custom authentication headers.
Definition RestClient.h:70
void SetBearerToken(const DotNetDupe::System::String &sToken)
Sets a Bearer token for the HTTP Authorization header (RFC 6750).
Definition RestClient.h:44
TResource Get(const DotNetDupe::System::String &sId)
Sends GET request for a specific entity ID and deserializes the JSON response.
Definition RestClient.h:103
void RemoveDefaultRequestHeader(const DotNetDupe::System::String &sName)
Removes a specific default request header.
Definition RestClient.h:87
Supports all classes in the DotNetDupe class hierarchy.
Definition Object.h:18
static SmartPointer< T > NewShared()
Creates a Shared SmartPointer, default constructing T.
Represents text as a sequence of UTF-8 code units with culture-invariant operations.
Definition String.h:74
bool EndsWith(char ch, bool bIgnoreCase) const
Definition String.cpp:315
static String Serialize(const T &value)
Converts the value of a type specified by a generic type parameter into a JSON string.