Commit 8bb192d2 authored by Geovanny Vera's avatar Geovanny Vera

Http service with post and get done. Created VRCLoginWidget to use instead of OWS

parent 7d776f21
# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must be checked in Version Control Systems.
#
# To customize properties used by the Ant build system edit
# "ant.properties", and override values to adapt the script to your
# project structure.
#
# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
# Project target.
target=android-19
This diff is collapsed.
This diff is collapsed.
......@@ -25,7 +25,7 @@ DefaultGraphicsPerformance=Scalable
AppliedDefaultGraphicsPerformance=Scalable
[/Script/EngineSettings.GameMapsSettings]
EditorStartupMap=/Game/Campus/Maps/TestyMainMap.TestyMainMap
EditorStartupMap=/Game/Campus/Maps/MainMenu.MainMenu
LocalMapOptions=
TransitionMap=None
bUseSplitscreen=False
......@@ -33,8 +33,8 @@ TwoPlayerSplitscreenLayout=Horizontal
ThreePlayerSplitscreenLayout=FavorTop
FourPlayerSplitscreenLayout=Grid
bOffsetPlayerGamepadIds=False
GameInstanceClass=/Game/Campus/Blueprints/BP_CGameInstance.BP_CGameInstance_C
GameDefaultMap=/Game/Campus/Maps/TestyMainMap.TestyMainMap
GameInstanceClass=/Script/VRClassroom.VRCGameInstance
GameDefaultMap=/Game/Campus/Maps/MainMenu.MainMenu
ServerDefaultMap=/Game/Campus/Maps/TestServer.TestServer
GlobalDefaultGameMode=/Game/Campus/Blueprints/BP_GameMode.BP_GameMode_C
GlobalDefaultServerGameMode=None
......
[/Script/EngineSettings.GeneralProjectSettings]
ProjectID=A37183B146C3E8DCAB0E22A58F8367D5
ProjectName=VRClassroom
VRCAPIPath="https://api.ev3.me/"
RPGAPICustomerKey="7EFDEEAE-B3CE-4043-8DE4-77219A60A9E0"
RPGAPIPath="rpgwebapi.sabredartstudios.com"
OWS2APIPath="http://owspublicapi.sabredartstudios.com/"
OWSEncryptionKey="FF82B5451E21C090AE76622AAF24BCB0"
CompanyName=Testy
Homepage="http://www.sabredartstudios.com"
Homepage=
SupportContact=""
CopyrightNotice=Copyright 2020 Testy
ProjectDisplayedTitle=NSLOCTEXT("[/Script/EngineSettings]", "837C4DE649564002BDAE8187B61096E6", "{GameName}")
......
......@@ -38,7 +38,8 @@ void UOWSLoginWidget::LoginAndCreateSession(FString Email, FString Password)
TSharedRef<IHttpRequest> Request = Http->CreateRequest();
Request->OnProcessRequestComplete().BindUObject(this, &UOWSLoginWidget::OnLoginAndCreateSessionResponseReceived);
FString url = FString(OWS2APIPath + "api/Users/LoginAndCreateSession");
FString url2 = FString(OWS2APIPath + "api/Users/LoginAndCreateSession");
FString url = FString("https://api.ev3.me/login");
//Trim whitespace
Email.TrimStartAndEndInline();
......@@ -47,22 +48,26 @@ void UOWSLoginWidget::LoginAndCreateSession(FString Email, FString Password)
TArray<FStringFormatArg> FormatParams;
FormatParams.Add(Email);
FormatParams.Add(Password);
FString PostParameters = FString::Format(TEXT("{ \"Email\": \"{0}\", \"Password\": \"{1}\" }"), FormatParams);
FString PostParameters = FString(TEXT("username=")) + Email
+ FString(TEXT("&password=")) + Password;
UE_LOG(LogTemp, Log, TEXT("FUCK"));
Request->SetURL(url);
Request->SetVerb("POST");
Request->SetHeader(TEXT("User-Agent"), "X-UnrealEngine-Agent");
Request->SetHeader("Content-Type", TEXT("application/json"));
Request->SetHeader(TEXT("X-CustomerGUID"), RPGAPICustomerKey);
Request->SetHeader("Content-Type", TEXT("application/x-www-form-urlencoded"));
//Request->SetHeader(TEXT("X-CustomerGUID"), RPGAPICustomerKey);
Request->SetContentAsString(PostParameters);
Request->ProcessRequest();
}
void UOWSLoginWidget::OnLoginAndCreateSessionResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
UE_LOG(LogTemp, Log, TEXT("DSA"), *Response->GetContentAsString());
if (bWasSuccessful && Response.IsValid())
{
TSharedPtr<FJsonObject> JsonObject;
UE_LOG(LogTemp, Log, TEXT("Token: %s"), *Response->GetContentAsString());
TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(Response->GetContentAsString());
if (FJsonSerializer::Deserialize(Reader, JsonObject))
......
// Copyright 2020 Testy
#include "HttpService.h"
#include "Runtime/Core/Public/Misc/ConfigCacheIni.h"
#include "Runtime/Online/HTTP/Public/Http.h"
UHttpService::UHttpService(const class FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
/*GConfig->GetString(
TEXT("/Script/EngineSettings.GeneralProjectSettings"),
TEXT("VRCAPIPath"),
VRCAPIPath,
GGameIni
);*/
VRCAPIPath = FString(TEXT("https://api.ev3.me/"));
Http = &FHttpModule::Get();
}
void UHttpService::SetTimeout(float Timeout) {
Http->SetHttpTimeout(Timeout);
}
bool UHttpService::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 UHttpService::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> UHttpService::RequestWithRoute(FString Subroute) {
// I have to set it here because for some reason if I set it on the constructor it becomes empty when it reaches here
// TODO: Figure out why that happens and fix it to remove this line
VRCAPIPath = FString(TEXT("https://api.ev3.me/"));
Http = &FHttpModule::Get();
TSharedRef<IHttpRequest> Request = Http->CreateRequest();
FString url = FString(VRCAPIPath + Subroute);
Request->SetURL(url);
SetRequestHeaders(Request);
return Request;
}
TSharedRef<IHttpRequest> UHttpService::PostRequest(FString Subroute, FString ContentJsonString) {
TSharedRef<IHttpRequest> Request = RequestWithRoute(Subroute);
Request->SetVerb("POST");
Request->SetContentAsString(ContentJsonString);
return Request;
}
TSharedRef<IHttpRequest> UHttpService::GetRequest(FString Subroute) {
TSharedRef<IHttpRequest> Request = RequestWithRoute(Subroute);
Request->SetVerb("GET");
return Request;
}
\ No newline at end of file
// Copyright 2020 Testy
#include "ServiceHttp.h"
// Sets default values
AServiceHttp::AServiceHttp()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void AServiceHttp::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AServiceHttp::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Copyright 2020 Testy
#include "VRCGameInstance.h"
#include "HttpService.h"
UVRCGameInstance::UVRCGameInstance(const class FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer) {
Http = NewObject<UHttpService>(UHttpService::StaticClass());
}
UHttpService* UVRCGameInstance::GetHttpService() {
return Http;
}
// Copyright 2020 Testy
#include "VRCLoginWidget.h"
#include "Runtime/Core/Public/Misc/ConfigCacheIni.h"
#include "HttpService.h"
#include "VRCGameInstance.h"
UVRCLoginWidget::UVRCLoginWidget(const class FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
}
void UVRCLoginWidget::LoginAndCreateSession(FString Username, FString Password)
{
if (Http == NULL) {
UVRCGameInstance* GameInstance = Cast<UVRCGameInstance>(GetWorld()->GetGameInstance());
Http = GameInstance->GetHttpService();
}
Http->SetTimeout(LoginTimeout); //Set timeout
//Trim whitespace
Username.TrimStartAndEndInline();
Password.TrimStartAndEndInline();
FString PostParameters = FString(TEXT("username=")) + Username
+ FString(TEXT("&password=")) + Password;
TSharedRef<IHttpRequest> Request = Http->PostRequest("login", PostParameters);
Request->OnProcessRequestComplete().BindUObject(this, &UVRCLoginWidget::OnLoginAndCreateSessionResponseReceived);
Request->SetHeader("Content-Type", TEXT("application/x-www-form-urlencoded"));
Request->ProcessRequest();
}
void UVRCLoginWidget::OnLoginAndCreateSessionResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
FString content = *Response->GetContentAsString();
if (Http->ResponseIsValid(Response, bWasSuccessful))
{
TSharedPtr<FJsonObject> JsonObject;
NotifyLoginAndCreateSession(content);
}
else
{
UE_LOG(LogTemp, Error, TEXT("OnUserLoginResponseReceived Error accessing login server!"));
ErrorLoginAndCreateSession(content);
}
}
// Copyright 2020 Testy
#pragma once
#include "CoreMinimal.h"
#include "Runtime/Online/HTTP/Public/Http.h"
#include "HttpService.generated.h"
/**
*
*/
UCLASS()
class VRCLASSROOM_API UHttpService : public UObject
{
GENERATED_BODY()
FHttpModule* Http;
public:
UHttpService(const class FObjectInitializer& ObjectInitializer);
FString VRCAPIPath = "";
void SetTimeout(float Timeout);
bool ResponseIsValid(FHttpResponsePtr Response, bool bWasSuccessful);
void SetRequestHeaders(TSharedRef<IHttpRequest>& Request);
TSharedRef<IHttpRequest> RequestWithRoute(FString Subroute);
TSharedRef<IHttpRequest> PostRequest(FString Subroute, FString ContentJsonString);
TSharedRef<IHttpRequest> GetRequest(FString Subroute);
};
// Copyright 2020 Testy
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "ServiceHttp.generated.h"
UCLASS()
class VRCLASSROOM_API AServiceHttp : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AServiceHttp();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
// Copyright 2020 Testy
#pragma once
#include "CoreMinimal.h"
#include "Engine/GameInstance.h"
#include "HttpService.h"
#include "VRCGameInstance.generated.h"
/**
*
*/
UCLASS()
class VRCLASSROOM_API UVRCGameInstance : public UGameInstance
{
GENERATED_BODY()
UHttpService* Http;
public:
UVRCGameInstance(const class FObjectInitializer& ObjectInitializer);
UFUNCTION(BlueprintCallable, Category = "Http")
UHttpService* GetHttpService();
};
// Copyright 2020 Testy
#pragma once
#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "HttpService.h"
#include "VRCGameInstance.h"
#include "VRCLoginWidget.generated.h"
/**
*
*/
UCLASS()
class VRCLASSROOM_API UVRCLoginWidget : public UUserWidget
{
GENERATED_BODY()
UHttpService* Http;
public:
UVRCLoginWidget(const class FObjectInitializer& ObjectInitializer);
UPROPERTY(BlueprintReadWrite, Category = "Config")
FString VRCAPIPath = "";
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Login")
float LoginTimeout = 20.f;
//LoginAndCreateSession
UFUNCTION(BlueprintCallable, Category = "Login")
void LoginAndCreateSession(FString Username, FString Password);
void OnLoginAndCreateSessionResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful);
UFUNCTION(BlueprintImplementableEvent, Category = "Login")
void NotifyLoginAndCreateSession(const FString& Token);
UFUNCTION(BlueprintImplementableEvent, Category = "Login")
void ErrorLoginAndCreateSession(const FString& ErrorMsg);
};
......@@ -20,7 +20,7 @@ public class VRClassroom : ModuleRules
"GameplayAbilities"
});
PrivateDependencyModuleNames.AddRange(new string[] { });
PrivateDependencyModuleNames.AddRange(new string[] { "UMG" });
PublicIncludePathModuleNames.AddRange(new string[] { "OWSPlugin" });
......
......@@ -4,3 +4,5 @@
#include "Modules/ModuleManager.h"
IMPLEMENT_PRIMARY_GAME_MODULE( FDefaultGameModuleImpl, VRClassroom, "VRClassroom" );
DEFINE_LOG_CATEGORY(VRC);
......@@ -3,4 +3,6 @@
#pragma once
//#include "Engine.h"
#include "CoreMinimal.h"
\ No newline at end of file
#include "CoreMinimal.h"
DECLARE_LOG_CATEGORY_EXTERN(VRC, Log, All);
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment