Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
teamhydra_id_generator/lib/src/idgen_http.dart
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
83 lines (73 sloc)
2.5 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:convert'; | |
import 'package:dio/dio.dart'; | |
import 'package:teamhydra_id_generator/src/idgen_dart_base.dart'; | |
class IDGenHTTPWorker { | |
final Dio _dio = Dio(); | |
final String _baseURL = 'https://id.hydra.workers.dev'; | |
final String username; | |
final String token; | |
IDGenHTTPWorker({required this.username, required this.token}); | |
Future<IDGenResponse> generate( | |
String type, Map<String, dynamic>? data) async { | |
try { | |
final response = await _dio.post(_baseURL, | |
data: { | |
'username': username, | |
'token': token, | |
'type': type, | |
...?data, | |
}, | |
options: Options(contentType: Headers.jsonContentType)); | |
return IDGenResponse.fromJson(response.data); | |
} on DioException catch (e) { | |
Map<String, dynamic>? errorData; | |
// Do we have a response? | |
if (e.response != null) { | |
// Try decoding the response | |
try { | |
errorData = jsonDecode(e.response!.data); | |
} catch (_) { | |
// Do nothing | |
} | |
} | |
if (errorData != null && errorData['error'] != null) { | |
throw IDGenException( | |
'Server rejected ID generation: ${errorData['error']}'); | |
} else if (errorData != null) { | |
throw IDGenException('Server rejected ID generation: $errorData'); | |
} else { | |
throw IDGenException( | |
'An error occurred during generation ($type): ${e.message}'); | |
} | |
} catch (e) { | |
throw IDGenException( | |
'An unknown error occurred during generation ($type): $e'); | |
} | |
} | |
Future<IDKeypairResponse> generateKeypair() async { | |
try { | |
final response = await _dio.post(_baseURL, | |
data: { | |
'username': username, | |
'token': token, | |
'type': 'keypair', | |
}, | |
options: Options(contentType: Headers.jsonContentType)); | |
return IDKeypairResponse.fromJson(response.data); | |
} on DioException catch (e) { | |
final errorData = e.response?.data; | |
if (errorData != null && errorData['error'] != null) { | |
throw IDGenException( | |
'Server rejected ID generation: ${errorData['error']}'); | |
} else if (errorData != null) { | |
throw IDGenException('Server rejected ID generation: $errorData'); | |
} else { | |
throw IDGenException( | |
'An error occurred during generation (keypair): ${e.message}'); | |
} | |
} catch (e) { | |
throw IDGenException( | |
'An unknown error occurred during generation (keypair): $e'); | |
} | |
} | |
} |