-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathHttpService.cpp
118 lines (79 loc) · 3.84 KB
/
HttpService.cpp
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Fill out your copyright notice in the Description page of Project Settings.
#include "<PUT_YOUR_GAME_NAME_HERE>.h"
#include "HttpService.h"
AHttpService::AHttpService(){ PrimaryActorTick.bCanEverTick = false; }
void AHttpService::BeginPlay() {
Super::BeginPlay();
// We don't want clients to be able to run HTTP requests. Only servers.
if (!HasAuthority()) return;
Http = &FHttpModule::Get();
FRequest_Login LoginCredentials;
LoginCredentials.email = TEXT("[email protected]");
LoginCredentials.password = TEXT("asdfasdf");
Login(LoginCredentials);
}
/**********************************************************************************************************************************************/
TSharedRef<IHttpRequest> AHttpService::RequestWithRoute(FString Subroute) {
TSharedRef<IHttpRequest> Request = Http->CreateRequest();
Request->SetURL(ApiBaseUrl + Subroute);
SetRequestHeaders(Request);
return Request;
}
void AHttpService::SetRequestHeaders(TSharedRef<IHttpRequest>& Request) {
Request->SetHeader(TEXT("User-Agent"), TEXT("X-UnrealEngine-Agent"));
Request->SetHeader(TEXT("Content-Type"), TEXT("application/json"));
Request->SetHeader(TEXT("Accepts"), TEXT("application/json"));
Request->SetHeader(AuthorizationHeader, AuthorizationHash);
}
TSharedRef<IHttpRequest> AHttpService::GetRequest(FString Subroute) {
TSharedRef<IHttpRequest> Request = RequestWithRoute(Subroute);
Request->SetVerb("GET");
return Request;
}
TSharedRef<IHttpRequest> AHttpService::PostRequest(FString Subroute, FString ContentJsonString) {
TSharedRef<IHttpRequest> Request = RequestWithRoute(Subroute);
Request->SetVerb("POST");
Request->SetContentAsString(ContentJsonString);
return Request;
}
void AHttpService::Send(TSharedRef<IHttpRequest>& Request) {
Request->ProcessRequest();
}
bool AHttpService::ResponseIsValid(FHttpResponsePtr Response, bool bWasSuccessful) {
if (!bWasSuccessful || !Response.IsValid()) return false;
if (EHttpResponseCodes::IsOk(Response->GetResponseCode())) return true;
else {
UE_LOG(LogTemp, Warning, TEXT("Http Response returned error code: %d"), Response->GetResponseCode());
return false;
}
}
void AHttpService::SetAuthorizationHash(FString Hash, TSharedRef<IHttpRequest>& Request) {
AuthorizationHash = Hash;
}
/**********************************************************************************************************************************************/
template <typename StructType>
void AHttpService::GetJsonStringFromStruct(StructType FilledStruct, FString& StringOutput) {
FJsonObjectConverter::UStructToJsonObjectString(StructType::StaticStruct(), &FilledStruct, StringOutput, 0, 0);
}
template <typename StructType>
void AHttpService::GetStructFromJsonString(FHttpResponsePtr Response, StructType& StructOutput) {
StructType StructData;
FString JsonString = Response->GetContentAsString();
FJsonObjectConverter::JsonObjectStringToUStruct<StructType>(JsonString, &StructOutput, 0, 0);
}
/**********************************************************************************************************************************************/
void AHttpService::Login(FRequest_Login LoginCredentials) {
FString ContentJsonString;
GetJsonStringFromStruct<FRequest_Login>(LoginCredentials, ContentJsonString);
TSharedRef<IHttpRequest> Request = PostRequest("user/login", ContentJsonString);
Request->OnProcessRequestComplete().BindUObject(this, &AHttpService::LoginResponse);
Send(Request);
}
void AHttpService::LoginResponse(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful) {
if (!ResponseIsValid(Response, bWasSuccessful)) return;
FResponse_Login LoginResponse;
GetStructFromJsonString<FResponse_Login>(Response, LoginResponse);
SetAuthorizationHash(LoginResponse.hash);
UE_LOG(LogTemp, Warning, TEXT("Id is: %d"), LoginResponse.id);
UE_LOG(LogTemp, Warning, TEXT("Name is: %s"), *LoginResponse.name);
}