implemented get/post helpers
This commit is contained in:
parent
946e77bfdd
commit
506b007eea
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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<Map<String, dynamic>> get(
|
||||
String path, Map<String, String> 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<Map<String, dynamic>> post(
|
||||
String path, Map<String, dynamic> 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);
|
||||
}
|
||||
}
|
|
@ -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
|
||||
|
|
|
@ -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<dynamic> 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<dynamic> posts = json["posts"];
|
||||
return posts.map((e) => PostView.fromJson(e)).toList();
|
||||
}
|
||||
|
||||
/// POST /post/like
|
||||
|
|
Loading…
Reference in New Issue