API constructor
API({})
Implementation
API({
required this.hostUrl,
required this.ua,
this.keyId,
this.keySecret, // Made optional for OAuth scenarios
this.oauthToken,
this.headers = const {},
}) {
// Allow either key_id/key_secret OR oauthToken
if ((keyId == null || keySecret == null) && oauthToken == null) {
throw ArgumentError(
'Either (keyId, keySecret) or oauthToken is mandatory',
);
}
final authHeaders = <String, dynamic>{};
if (keyId != null && keySecret != null) {
final credentials = '$keyId:$keySecret';
final encodedCredentials = base64Encode(utf8.encode(credentials));
authHeaders['Authorization'] = 'Basic $encodedCredentials';
} else if (oauthToken != null) {
authHeaders['Authorization'] = 'Bearer $oauthToken';
}
client = Dio(
BaseOptions(
baseUrl: hostUrl,
headers: {
'User-Agent': ua,
...getValidHeaders(headers),
...authHeaders, // Add authentication headers
},
),
);
}