headers method
This is used to build the headers for all the requests, it will return a Map<String, String>. if an organization id is set, it will be added to the headers as well. If in anyhow the API key is not set, it will throw an AssertionError.
Implementation
Map<String, String> headers() {
final Map<String, String> headers = <String, String>{
'Content-Type': 'application/json',
};
assert(
_apiKey != null,
"You must set the API key before making building any headers for a request.",
);
if (_organization != null) {
headers['OpenAI-Organization'] = _organization!;
}
if (_apiKey?.startsWith('Bearer ') ?? false) {
headers["Authorization"] = _apiKey!;
} else {
headers["Authorization"] = "Bearer $_apiKey";
}
return headers;
}