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';
|
export 'v1/main.dart';
|
||||||
|
|
||||||
class LemmyAPI {
|
class LemmyAPI {
|
||||||
/// url of this lemmy instance
|
/// host uri of this lemmy instance
|
||||||
String instanceUrl;
|
String host;
|
||||||
|
|
||||||
V1 v1;
|
V1 v1;
|
||||||
|
|
||||||
/// initialize lemmy api instance
|
/// initialize lemmy api instance
|
||||||
LemmyAPI(this.instanceUrl)
|
LemmyAPI(this.host)
|
||||||
: assert(instanceUrl != null),
|
: assert(host != null),
|
||||||
v1 = V1(instanceUrl);
|
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 'package:flutter/foundation.dart' show required;
|
||||||
|
|
||||||
|
import '../http_helper.dart';
|
||||||
import '../models/category.dart';
|
import '../models/category.dart';
|
||||||
import '../models/search.dart';
|
import '../models/search.dart';
|
||||||
|
|
||||||
|
@ -7,10 +8,13 @@ export 'comment_endpoint.dart';
|
||||||
export 'post_endpoint.dart';
|
export 'post_endpoint.dart';
|
||||||
export 'user_endpoint.dart';
|
export 'user_endpoint.dart';
|
||||||
|
|
||||||
class V1 {
|
class V1 extends HttpHelper {
|
||||||
String instanceUrl;
|
@override
|
||||||
|
final String host;
|
||||||
|
@override
|
||||||
|
final String extraPath = "/api/v1";
|
||||||
|
|
||||||
V1(this.instanceUrl);
|
V1(this.host);
|
||||||
|
|
||||||
/// GET /categories
|
/// GET /categories
|
||||||
/// https://dev.lemmy.ml/docs/contributing_websocket_http_api.html#list-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:flutter/foundation.dart' show required;
|
||||||
import 'package:http/http.dart' as http;
|
|
||||||
|
|
||||||
import '../models/post.dart';
|
import '../models/post.dart';
|
||||||
import 'main.dart';
|
import 'main.dart';
|
||||||
|
@ -49,20 +46,17 @@ extension PostEndpoint on V1 {
|
||||||
assert(type != null);
|
assert(type != null);
|
||||||
assert(sort != null);
|
assert(sort != null);
|
||||||
|
|
||||||
var res = await http.get(Uri.https(
|
var json = await get("/post/list", {
|
||||||
instanceUrl,
|
'type_': type.value,
|
||||||
"/api/v1/post/list",
|
'sort': sort.value,
|
||||||
{
|
if (page != null) 'page': page.toString(),
|
||||||
'type_': type.value,
|
if (limit != null) 'limit': limit.toString(),
|
||||||
'sort': sort.value,
|
if (communityId != null) 'community_id': communityId.toString(),
|
||||||
if (page != null) 'page': page.toString(),
|
if (communityName != null) 'community_name': communityName,
|
||||||
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();
|
||||||
));
|
|
||||||
List<dynamic> json = jsonDecode(res.body)["posts"];
|
|
||||||
return json.map((e) => PostView.fromJson(e)).toList();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// POST /post/like
|
/// POST /post/like
|
||||||
|
|
Loading…
Reference in New Issue