This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RtRestSvc.cpp
98 lines (81 loc) · 2.75 KB
/
RtRestSvc.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
// Fill out your copyright notice in the Description page of Project Settings.
#include "RtRestSvc.h"
#include "RtGameMode.h"
#include "RtGameState.h"
#include "ModuleManager.h"
#include "HttpModule.h"
#include "IHttpResponse.h"
#include "JsonUtilities/Public/JsonUtilities.h"
#include "Engine/World.h"
// Sets default values
ARtRestSvc::ARtRestSvc()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = false;
Http = &FModuleManager::LoadModuleChecked<FHttpModule>("HTTP");
}
// Called when the game starts or when spawned
void ARtRestSvc::BeginPlay()
{
Super::BeginPlay();
ARtGameState* GameState = GetWorld()->GetGameState<ARtGameState>();
if (GameState)
{
APIBaseUrl = GameState->GetAPIBaseUrl();
}
}
TSharedRef<IHttpRequest> ARtRestSvc::RequestWithRoute(FString Subroute)
{
TSharedRef<class IHttpRequest> HttpRequest = Http->CreateRequest();
HttpRequest->SetURL(APIBaseUrl + Subroute);
HttpRequest->SetHeader(TEXT("User-Agent"), TEXT("X-UnrealEngine-Agent"));
HttpRequest->SetHeader(TEXT("Content-Type"), TEXT("application/json"));
HttpRequest->SetHeader(TEXT("Accepts"), TEXT("application/json"));
return HttpRequest;
}
TSharedRef<IHttpRequest> ARtRestSvc::GetRequest(FString Subroute)
{
TSharedRef<class IHttpRequest> HttpRequest = RequestWithRoute(Subroute);
HttpRequest->SetVerb("GET");
return HttpRequest;
}
TSharedRef<IHttpRequest> ARtRestSvc::PostRequest(FString Subroute, FString ContentJsonString)
{
TSharedRef<class IHttpRequest> HttpRequest = RequestWithRoute(Subroute);
HttpRequest->SetVerb("POST");
HttpRequest->SetContentAsString(ContentJsonString);
return HttpRequest;
}
void ARtRestSvc::Send(TSharedRef<IHttpRequest>& Request)
{
Request->ProcessRequest();
}
bool ARtRestSvc::ResponseIsValid(FHttpResponsePtr Response, bool bWasSuccessful)
{
if ((!bWasSuccessful) || (!Response.IsValid()))
return false;
if (EHttpResponseCodes::IsOk(Response->GetResponseCode()))
return true;
UE_LOG(LogTemp, Warning, TEXT("Http Response returned error code: %d"), Response->GetResponseCode());
return false;
}
void ARtRestSvc::OnGetItemInfo(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
if(ResponseIsValid(Response, bWasSuccessful))
{
TArray<FRtItemInfo> Items;
FJsonObjectConverter::JsonArrayStringToUStruct<FRtItemInfo>(Response->GetContentAsString(), &Items, 0, 0);
ARtGameMode* GameMode = GetWorld()->GetAuthGameMode<ARtGameMode>();
if(GameMode)
{
GameMode->OnRefreshItems(Items);
}
}
}
void ARtRestSvc::GetItemInfo()
{
// Get status update on all items
TSharedRef<IHttpRequest> Request = GetRequest("item");
Request->OnProcessRequestComplete().BindUObject(this, &ARtRestSvc::OnGetItemInfo);
Send(Request);
}