Skip to content

Commit

Permalink
[ Fix ] fixed the multi listening for HTTP streamed responses by the …
Browse files Browse the repository at this point in the history
…package logger
  • Loading branch information
anasfik committed Feb 21, 2024
1 parent dafde94 commit c91a9c6
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
10 changes: 5 additions & 5 deletions lib/src/core/networking/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ abstract class OpenAINetworkingClient {

final String encodedBody = await response.stream.bytesToString();

OpenAILogger.logResponseBody(response);
OpenAILogger.logResponseBody(encodedBody);

final Map<String, dynamic> decodedBody = decodeToMap(encodedBody);

Expand Down Expand Up @@ -511,7 +511,7 @@ abstract class OpenAINetworkingClient {

final String encodedBody = await response.stream.bytesToString();

OpenAILogger.logResponseBody(response);
OpenAILogger.logResponseBody(encodedBody);

final Map<String, dynamic> decodedBody = decodeToMap(encodedBody);

Expand Down Expand Up @@ -559,14 +559,14 @@ abstract class OpenAINetworkingClient {
final http.StreamedResponse response =
await request.send().timeout(OpenAIConfig.requestsTimeOut);

OpenAILogger.logResponseBody(response);
final String responseBody = await response.stream.bytesToString();

OpenAILogger.logResponseBody(responseBody);

OpenAILogger.requestToWithStatusCode(to, response.statusCode);

OpenAILogger.startingDecoding();

final String responseBody = await response.stream.bytesToString();

var resultBody;

resultBody = switch ((responseBody.canBeParsedToJson, responseMapAdapter)) {
Expand Down
8 changes: 3 additions & 5 deletions lib/src/core/utils/logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,13 @@ abstract final class OpenAILogger {
}

/// Logs the response of a request, if the logger is active.
static Future<void> logResponseBody(response) async {
static void logResponseBody(response) {
if (_isActive && _showResponsesLogs) {
if (response is Response) {
dev.log(response.body.toString(), name: OpenAIStrings.openai);
} else if (response is StreamedResponse) {
final asString = await response.stream.bytesToString();

} else {
dev.log(
asString,
response.toString(),
name: OpenAIStrings.openai,
);
}
Expand Down
2 changes: 1 addition & 1 deletion test/openai_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void main() async {
}
});
test('with setting a key', () {
OpenAI.apiKey = "YOUR API KEY HERE";
OpenAI.apiKey = "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";

expect(OpenAI.instance, isA<OpenAI>());
});
Expand Down

0 comments on commit c91a9c6

Please sign in to comment.