listMessages method

Future<List<GptAssistantMessage>> listMessages({
  1. required String apiKey,
  2. required String organizationId,
  3. required String threadId,
  4. required GetGptListRequest request,
})

Implementation

Future<List<GptAssistantMessage>> listMessages({
  required String apiKey,
  required String organizationId,
  required String threadId,
  required GetGptListRequest request
}) async {

  Map<String, String> headers = {
    "Authorization": "Bearer $apiKey",
    "OpenAI-Organization": organizationId,
    'Content-Type': 'application/json',
    "Access-Control-Allow-Origin": "*", // Required for CORS support to work
    "OpenAI-Beta": "assistants=v1"
  };


  Uri endpoint;
  if(secure) {
    endpoint = Uri.https(
        baseUrl, "/v1/threads/$threadId/messages", request.toJson().toQueryParam());
  }
  else {
    endpoint = Uri.http(
        baseUrl, "/v1/threads/$threadId/messages",request.toJson().toQueryParam());
  }

  var response = await
  http.get(endpoint, headers: headers);

  if (response.statusCode == 200 || response.statusCode == 201) {
    return GptList<GptAssistantMessage>.fromJson(jsonDecode(const Utf8Decoder().convert(response.bodyBytes)), (data)=> GptAssistantMessage.fromJson(data as Map<String, dynamic>)).data;
  }
  else {
    var error = ServerError.fromJson(jsonDecode(response.body));
    throw MessagesException(statusCode: response.statusCode, message: error.message);
  }
}