-
Notifications
You must be signed in to change notification settings - Fork 36
/
Requests.cs
113 lines (102 loc) · 4.58 KB
/
Requests.cs
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using ChatGPTWrapper;
namespace Reqs {
public class Requests
{
private void SetHeaders(ref UnityWebRequest req, List<(string, string)> headers)
{
for (int i = 0; i < headers.Count; i++)
{
req.SetRequestHeader(headers[i].Item1, headers[i].Item2);
}
}
public IEnumerator GetReq<T>(string uri, System.Action<T> callback, List<(string, string)> headers = null)
{
UnityWebRequest webRequest = new UnityWebRequest(uri, "GET");
if (headers != null) SetHeaders(ref webRequest, headers);
webRequest.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer();
webRequest.disposeUploadHandlerOnDispose = true;
webRequest.disposeDownloadHandlerOnDispose = true;
yield return webRequest.SendWebRequest();
#if UNITY_2020_3_OR_NEWER
switch (webRequest.result)
{
case UnityWebRequest.Result.ConnectionError:
case UnityWebRequest.Result.DataProcessingError:
Debug.LogError("Error: " + webRequest.error);
break;
case UnityWebRequest.Result.ProtocolError:
Debug.LogError("HTTP Error: " + webRequest.error);
break;
case UnityWebRequest.Result.Success:
var responseJson = JsonUtility.FromJson<T>(webRequest.downloadHandler.text);
callback(responseJson);
break;
}
#else
if(!string.IsNullOrWhiteSpace(webRequest.error))
{
Debug.LogError($"Error {webRequest.responseCode} - {webRequest.error}");
yield break;
}
else
{
var responseJson = JsonUtility.FromJson<T>(webRequest.downloadHandler.text);
callback(responseJson);
}
#endif
webRequest.Dispose();
}
public IEnumerator PostReq<T>(string uri, string json, System.Action<T> callback, List<(string, string)> headers = null)
{
UnityWebRequest webRequest = new UnityWebRequest(uri, "POST");
if (headers != null) SetHeaders(ref webRequest, headers);
byte[] jsonToSend = new System.Text.UTF8Encoding().GetBytes(json);
webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(jsonToSend);
webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
webRequest.disposeDownloadHandlerOnDispose = true;
webRequest.disposeUploadHandlerOnDispose = true;
yield return webRequest.SendWebRequest();
#if UNITY_2020_3_OR_NEWER
switch (webRequest.result)
{
case UnityWebRequest.Result.ConnectionError:
case UnityWebRequest.Result.DataProcessingError:
Debug.LogError("Error: " + webRequest.error);
break;
case UnityWebRequest.Result.ProtocolError:
Debug.LogError("HTTP Error: " + webRequest.error);
if (uri.EndsWith("/completions")) {
var errJson = JsonUtility.FromJson<ChatGPTResError>(webRequest.downloadHandler.text);
Debug.LogError(errJson.error.message);
if (webRequest.error == "HTTP/1.1 429 Too Many Requests")
{
Debug.Log("retrying...");
yield return PostReq<T>(uri, json, callback, headers);
}
}
break;
case UnityWebRequest.Result.Success:
var responseJson = JsonUtility.FromJson<T>(webRequest.downloadHandler.text);
callback(responseJson);
break;
}
#else
if(!string.IsNullOrWhiteSpace(webRequest.error))
{
Debug.LogError($"Error {webRequest.responseCode} - {webRequest.error}");
yield break;
}
else
{
var responseJson = JsonUtility.FromJson<T>(webRequest.downloadHandler.text);
callback(responseJson);
}
#endif
webRequest.Dispose();
}
}
}