-
Notifications
You must be signed in to change notification settings - Fork 1
/
APICommunication.cfc
128 lines (111 loc) · 4.87 KB
/
APICommunication.cfc
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
118
119
120
121
122
123
124
125
126
127
128
/*
USAGE:
// Create an instance of APICommunication
apiComm = new APICommunication();
*/
component {
// Define the APIKey variable
variables.APIKey = "";
//
// Request/Response Functions
///////////////////////////////////////////////////////////////////////////
// Function to configure HTTP request
function configureHttpRequest(httpRequest, method, endpoint, req) {
httpRequest.setURL("https://api.openai.com/v1/" & endpoint);
httpRequest.setMethod(method);
httpRequest.addParam(type="header", name="Content-Type", value="application/json");
httpRequest.addParam(type="header", name="Authorization", value="Bearer " & variables.APIKey);
httpRequest.addParam(type="header", name="OpenAI-Beta", value="assistants=v1");
// Convert request struct to JSON and set it as the request body
if (structKeyExists(req, "Instructions")) {
var requestBody = serializeJSON({
"instructions": req.Instructions,
"name": req.Name,
"tools": req.Tools,
"model": req.Model
// Add other parameters as needed
});
httpRequest.setBody(requestBody);
}
}
// Function to send HTTP request with retries and backoff
function sendHttpRequest(httpRequest, maxRetries) {
var currentRetry = 0;
while (currentRetry lt maxRetries) {
try {
var httpResponse = httpRequest.send();
return httpResponse;
} catch (java.net.SocketTimeoutException e) {
// Handle timeout exception
currentRetry++;
// Exponential backoff: wait for an increasing amount of time between retries
var waitTime = 2^currentRetry * 1000; // wait in milliseconds
sleep(waitTime);
if (currentRetry lt maxRetries) {
var logOutput = "Retry ##" & currentRetry & " due to timeout. Will retry up to " & maxRetries & " times.";
writeLog(text=logOutput, type="info");
} else {
var exceptionMessage = "HTTP request failed with timeout after " & maxRetries & " attempts.";
throw exceptionMessage;
}
} catch (any e) {
// Handle other exceptions
var unexpectedException = "An unexpected error occurred: " & e.getMessage();
logFullExceptionDetails(unexpectedException);
throw unexpectedException;
}
}
}
// Function to log full exception details
function logFullExceptionDetails(message) {
var exceptionDetails = "Exception Details: " & getMessageDetails(message);
writeLog(text=exceptionDetails, type="error");
}
// Function to get message details including the stack trace
function getMessageDetails(message) {
var details = message & " Stack Trace: " & getStackTrace();
return details;
}
// Function to handle HTTP response
function handleHttpResponse(result, httpResponse) {
var statusCode = httpResponse.getStatusCode();
result.status = statusCode;
// Log the entire HTTP response for debugging
writeLog("HTTP Response: " & httpResponse.toString(), "debug");
if (statusCode eq 200 or statusCode eq 201 or statusCode eq 204) {
if (len(httpResponse.fileContent)) {
result.data = deserializeJSON(httpResponse.fileContent);
}
} else {
var errorResponse = deserializeJSON(httpResponse.getAsString());
// Handle specific HTTP status codes
switch (statusCode) {
case 400:
result.error = "Bad Request: " & errorResponse.error.message;
break;
case 401:
result.error = "Unauthorized: " & errorResponse.error.message;
break;
case 403:
result.error = "Forbidden: " & errorResponse.error.message;
break;
case 404:
result.error = "Not Found: " & errorResponse.error.message;
break;
case 500:
result.error = "Internal Server Error: " & errorResponse.error.message;
break;
default:
result.error = "HTTP request failed with status code: " & statusCode & ". " & errorResponse.error.message;
break;
}
result.errorCode = errorResponse.error.code;
result.errorType = errorResponse.error.type;
result.errorParam = errorResponse.error.param;
}
}
// Function to log errors
function logError(message) {
cflog(file="error", text=message);
}
}