classifyCopilotPollResponse function
Classifies one device-flow poll response. Pure: 404 →
endpointDisabled, a non-JSON body → transport, an access_token →
success, otherwise the OAuth error field decides.
Implementation
CopilotPollOutcome classifyCopilotPollResponse(
http.Response response, {
required String clientId,
}) {
if (response.statusCode == 404) {
return CopilotPollFailure(
_endpointDisabled(clientId, response.statusCode, response.body),
);
}
final body = _decodeJsonObject(response);
if (body == null) {
return CopilotPollFailure(
CopilotDeviceFlowError(
CopilotDeviceFlowErrorKind.transport,
'Copilot device-flow poll returned a non-JSON response '
'(HTTP ${response.statusCode}): ${response.body.trim()}',
),
);
}
final token = body['access_token'];
if (token is String && token.isNotEmpty) return CopilotPollSuccess(token);
return switch (body['error']) {
'authorization_pending' => const CopilotPollPending(),
'slow_down' => const CopilotPollSlowDown(),
'expired_token' => const CopilotPollFailure(
CopilotDeviceFlowError(
CopilotDeviceFlowErrorKind.expired,
'the device code expired (GitHub: expired_token) — start again',
),
),
'access_denied' => const CopilotPollFailure(
CopilotDeviceFlowError(
CopilotDeviceFlowErrorKind.denied,
'the authorization was denied (GitHub: access_denied)',
),
),
final Object other => CopilotPollFailure(
CopilotDeviceFlowError(
CopilotDeviceFlowErrorKind.transport,
'Copilot device-flow poll failed: ${response.body.trim()} '
'(error: $other)',
),
),
_ => CopilotPollFailure(
CopilotDeviceFlowError(
CopilotDeviceFlowErrorKind.transport,
'Copilot device-flow poll returned no token and no error: '
'${response.body.trim()}',
),
),
};
}