cyrtophora

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 837226081bebd3a729b245ea4b71d4a69f688862
parent 0a7ff3dd715ff2c645fc5924a1ec6175d20df37f
Author: Jackson G. Kaindume <seestem@merely.tech>
Date:   Wed, 31 Aug 2022 16:53:08 +0200

[cyrto] add api client

Diffstat:
Mcyrto/lib/cyrtophora.dart | 1+
Acyrto/lib/src/api.dart | 30++++++++++++++++++++++++++++++
2 files changed, 31 insertions(+), 0 deletions(-)

diff --git a/cyrto/lib/cyrtophora.dart b/cyrto/lib/cyrtophora.dart @@ -5,4 +5,5 @@ library cyrtophora; export 'src/utils.dart'; export 'src/validate.dart'; export 'src/data.dart'; +export 'src/api.dart'; export 'src/account.dart'; diff --git a/cyrto/lib/src/api.dart b/cyrto/lib/src/api.dart @@ -0,0 +1,30 @@ +import 'package:http/http.dart' as http; +import 'package:cyrtophora/src/data.dart'; +import 'dart:convert'; +import 'package:cyrtophora/src/account.dart'; + +/// Accounts API +class CyrtophoraAPI { + CyrtophoraAPI(this.api); + final String api; + final Map<String, String> jsonHeaders = { + 'Content-Type': 'application/json; charset=UTF-8', + }; + + /// Create new account + Future<PublicAccount> create(AccountRegistration input) async { + final response = await http.post( + Uri.parse("$api/accounts"), + headers: jsonHeaders, + body: jsonEncode(input.toJson()), + ); + + if (response.statusCode == 400) { + throw Exception('Invalid Input: ${response.body}'); + } else if (response.statusCode == 201) { + return PublicAccount.fromJson(jsonDecode(response.body)); + } else { + throw Exception('Server side error: ${response.body}'); + } + } +}