diff --git a/lib/client/client.dart b/lib/client/client.dart index bf02604..7dad408 100644 --- a/lib/client/client.dart +++ b/lib/client/client.dart @@ -3,13 +3,13 @@ import 'v1/main.dart'; export 'v1/main.dart'; class LemmyAPI { - /// url of this lemmy instance - String instanceUrl; + /// host uri of this lemmy instance + String host; V1 v1; /// initialize lemmy api instance - LemmyAPI(this.instanceUrl) - : assert(instanceUrl != null), - v1 = V1(instanceUrl); + LemmyAPI(this.host) + : assert(host != null), + v1 = V1(host); } diff --git a/lib/client/http_helper.dart b/lib/client/http_helper.dart new file mode 100644 index 0000000..f505414 --- /dev/null +++ b/lib/client/http_helper.dart @@ -0,0 +1,39 @@ +import 'dart:convert'; +import 'dart:io'; + +import 'package:http/http.dart' as http; + +extension OkResponse on http.Response { + bool get ok => statusCode >= 200 && statusCode < 300; +} + +abstract class HttpHelper { + String host; + String extraPath; + + Future> get( + String path, Map query) async { + var res = await http.get(Uri.https(host, "$extraPath$path", query)); + + if (!res.ok) { + // failed request, handle here + } + + return jsonDecode(res.body); + } + + Future> post( + String path, Map body) async { + var res = await http.post( + Uri.https(host, "$extraPath$path"), + body: jsonEncode(body), + headers: {HttpHeaders.contentTypeHeader: ContentType.json.mimeType}, + ); + + if (!res.ok) { + // failed request, handle here + } + + return jsonDecode(res.body); + } +} diff --git a/lib/client/v1/main.dart b/lib/client/v1/main.dart index 4a04851..f4f29c8 100644 --- a/lib/client/v1/main.dart +++ b/lib/client/v1/main.dart @@ -1,5 +1,6 @@ import 'package:flutter/foundation.dart' show required; +import '../http_helper.dart'; import '../models/category.dart'; import '../models/search.dart'; @@ -7,10 +8,13 @@ export 'comment_endpoint.dart'; export 'post_endpoint.dart'; export 'user_endpoint.dart'; -class V1 { - String instanceUrl; +class V1 extends HttpHelper { + @override + final String host; + @override + final String extraPath = "/api/v1"; - V1(this.instanceUrl); + V1(this.host); /// GET /categories /// https://dev.lemmy.ml/docs/contributing_websocket_http_api.html#list-categories diff --git a/lib/client/v1/post_endpoint.dart b/lib/client/v1/post_endpoint.dart index 178eba3..0e181a4 100644 --- a/lib/client/v1/post_endpoint.dart +++ b/lib/client/v1/post_endpoint.dart @@ -1,7 +1,4 @@ -import 'dart:convert'; - import 'package:flutter/foundation.dart' show required; -import 'package:http/http.dart' as http; import '../models/post.dart'; import 'main.dart'; @@ -49,20 +46,17 @@ extension PostEndpoint on V1 { assert(type != null); assert(sort != null); - var res = await http.get(Uri.https( - instanceUrl, - "/api/v1/post/list", - { - 'type_': type.value, - 'sort': sort.value, - if (page != null) 'page': page.toString(), - if (limit != null) 'limit': limit.toString(), - if (communityId != null) 'community_id': communityId.toString(), - if (communityName != null) 'community_name': communityName, - }, - )); - List json = jsonDecode(res.body)["posts"]; - return json.map((e) => PostView.fromJson(e)).toList(); + var json = await get("/post/list", { + 'type_': type.value, + 'sort': sort.value, + if (page != null) 'page': page.toString(), + if (limit != null) 'limit': limit.toString(), + if (communityId != null) 'community_id': communityId.toString(), + if (communityName != null) 'community_name': communityName, + }); + + List posts = json["posts"]; + return posts.map((e) => PostView.fromJson(e)).toList(); } /// POST /post/like