Unreal OpenAI API 1.0.0
OpenAIProviderFake.h
1// OpenAI, Copyright LifeEXE. All Rights Reserved.
2
3#pragma once
4
5#include "CoreMinimal.h"
6#include "Provider/OpenAIProvider.h"
7#include "OpenAIProviderFake.generated.h"
8
9class FFakeHttpResponse : public IHttpResponse
10{
11public:
12 FFakeHttpResponse(const FString& ResponseStr) : ReponseData(ResponseStr) {}
13 virtual int32 GetResponseCode() const override { return static_cast<int32>(EHttpResponseCodes::Ok); }
14 virtual FString GetContentAsString() const override { return ReponseData; }
15 virtual FString GetURL() const override { return FString(); }
16 virtual FString GetURLParameter(const FString& ParameterName) const override { return FString(); }
17 virtual FString GetHeader(const FString& HeaderName) const override { return FString(); }
18 virtual TArray<FString> GetAllHeaders() const override { return TArray<FString>(); }
19 virtual FString GetContentType() const override { return FString(); }
20 virtual uint64 GetContentLength() const override { return uint64(); }
21 virtual const TArray<uint8>& GetContent() const override
22 {
23 static TArray<uint8> None;
24 return None;
25 }
26 virtual const FString& GetEffectiveURL() const override { return EffectiveURL; }
27 virtual EHttpRequestStatus::Type GetStatus() const override { return EHttpRequestStatus::Type::Succeeded; }
28 virtual EHttpFailureReason GetFailureReason() const override { return EHttpFailureReason::None; }
29
30private:
31 FString ReponseData;
32 FString EffectiveURL;
33};
34
35class FFakeHttpRequest : public IHttpRequest
36{
37public:
38 FFakeHttpRequest(const FString& ResponseStr) : ReponseData(ResponseStr) {}
39 virtual FString GetURL() const override { return FString(); }
40 virtual FHttpRequestWillRetryDelegate& OnRequestWillRetry() override { return HttpRequestWillRetryDelegate; }
41 virtual FString GetURLParameter(const FString& ParameterName) const override { return FString(); }
42 virtual FString GetHeader(const FString& HeaderName) const override { return FString(); }
43 virtual TArray<FString> GetAllHeaders() const override { return TArray<FString>(); }
44 virtual FString GetContentType() const override { return FString(); }
45 virtual uint64 GetContentLength() const override { return uint64(); }
46 virtual const TArray<uint8>& GetContent() const override
47 {
48 static TArray<uint8> None;
49 return None;
50 }
51 virtual FString GetVerb() const override { return FString{}; }
52 virtual void SetVerb(const FString& Verb) override {}
53 virtual void SetURL(const FString& URL) override {}
54 virtual void SetContent(const TArray<uint8>& ContentPayload) override {}
55 virtual void SetContent(TArray<uint8>&& ContentPayload) override {}
56 virtual void SetContentAsString(const FString& ContentString) override {}
57 virtual bool SetContentAsStreamedFile(const FString& Filename) override { return false; }
58 virtual bool SetContentFromStream(TSharedRef<FArchive, ESPMode::ThreadSafe> Stream) override { return false; }
59 virtual void SetHeader(const FString& HeaderName, const FString& HeaderValue) override {}
60 virtual void AppendToHeader(const FString& HeaderName, const FString& AdditionalHeaderValue) override {}
61 virtual void SetTimeout(float InTimeoutSecs) override {}
62 virtual void ClearTimeout() override {}
63 virtual TOptional<float> GetTimeout() const override { return TOptional<float>(); }
64 virtual bool ProcessRequest() override
65 {
66 HttpRequestCompleteDelegate.Execute(SharedThis(this), GetResponse(), true);
67 return true;
68 }
69 virtual FHttpRequestCompleteDelegate& OnProcessRequestComplete() override { return HttpRequestCompleteDelegate; }
70 virtual FHttpRequestHeaderReceivedDelegate& OnHeaderReceived() override { return HttpHeaderReceivedDelegate; }
71 virtual FHttpRequestProgressDelegate64& OnRequestProgress64() override { return HttpRequestProgressDelegate64; }
72 virtual FHttpRequestStatusCodeReceivedDelegate& OnStatusCodeReceived() override { return HttpRequestStatusCodeReceivedDelegate; }
73 virtual void CancelRequest() override {}
74 virtual EHttpRequestStatus::Type GetStatus() const override { return EHttpRequestStatus::Type::NotStarted; }
75 virtual const FHttpResponsePtr GetResponse() const override { return MakeShareable(new FFakeHttpResponse(ReponseData)); }
76 virtual void Tick(float DeltaSeconds) override {}
77 virtual float GetElapsedTime() const override { return float{}; }
78 virtual bool SetResponseBodyReceiveStream(TSharedRef<FArchive> Stream) override { return false; }
79 virtual void SetDelegateThreadPolicy(EHttpRequestDelegateThreadPolicy InThreadPolicy) override {}
80 virtual EHttpRequestDelegateThreadPolicy GetDelegateThreadPolicy() const override
81 {
82 return EHttpRequestDelegateThreadPolicy::CompleteOnGameThread;
83 }
84 virtual const FString& GetEffectiveURL() const override { return EffectiveURL; }
85 virtual EHttpFailureReason GetFailureReason() const override { return EHttpFailureReason::None; }
86 virtual void ProcessRequestUntilComplete() override {}
87 virtual void SetActivityTimeout(float InTimeoutSecs) override {}
88 virtual FString GetOption(const FName Option) const override { return {}; }
89 virtual void SetOption(const FName Option, const FString& OptionValue) override {}
90 virtual void ResetTimeoutStatus() override {}
91
92public:
93 FHttpRequestProgressDelegate HttpRequestProgressDelegate;
94 FHttpRequestProgressDelegate64 HttpRequestProgressDelegate64;
95 FHttpRequestCompleteDelegate HttpRequestCompleteDelegate;
96 FHttpRequestHeaderReceivedDelegate HttpHeaderReceivedDelegate;
97 FHttpRequestWillRetryDelegate HttpRequestWillRetryDelegate;
98 FHttpRequestStatusCodeReceivedDelegate HttpRequestStatusCodeReceivedDelegate;
99
100private:
101 FString ReponseData;
102 FString EffectiveURL;
103};
104
105UCLASS()
106class OPENAITESTRUNNER_API UOpenAIProviderFake : public UOpenAIProvider
107{
108 GENERATED_BODY()
109
110public:
111 void SetResponse(const FString& ResponseStr) { ReponseData = ResponseStr; };
112
113private:
114 FString ReponseData;
115
116 virtual TSharedRef<IHttpRequest, ESPMode::ThreadSafe> CreateRequest() const override
117 { //
118 return MakeShareable(new FFakeHttpRequest(ReponseData));
119 }
120};
Definition: OpenAIProviderFake.h:36
Definition: OpenAIProviderFake.h:10
Definition: OpenAIProviderFake.h:107
Definition: OpenAIProvider.h:45