4
2
Fork 0

Integration of cache into contact, conversation and user api providers

pull/35/head
UnicodingUnicorn 2019-03-14 20:19:32 +08:00
parent 6e63969aff
commit 8eacad4dfc
4 changed files with 69 additions and 29 deletions

View File

@ -3,15 +3,26 @@ import "package:http/http.dart" as http;
import "dart:convert";
import "../models/user_model.dart";
import "../services/cache_http.dart";
import "../../settings.dart";
class ContactApiProvider {
Future<List<User>> fetchContacts() async {
final response = await http.get("$baseUrlCore/user/$globalUserId/contact");
CacheHttp cache;
return jsonDecode(response.body)
.map<User>((user) => User.fromJson(user))
.toList();
Future<void> init() async {
this.cache = new CacheHttp();
await this.cache.init();
}
Future<List<User>> fetchContacts() async {
try {
final responseBody = await this.cache.fetch("$baseUrlCore/user/$globalUserId/contact");
return jsonDecode(responseBody)
.map<User>((user) => User.fromJson(user))
.toList();
} catch(e) {
throw e;
}
}
void createContact(User user) async =>

View File

@ -5,9 +5,18 @@ import "dart:convert";
import "../models/conversation_model.dart";
import "../models/user_model.dart";
import "../services/cache_http.dart";
import "../../settings.dart";
class ConversationApiProvider {
CacheHttp cache;
Future<void> init() async {
this.cache = new CacheHttp();
await this.cache.init();
}
Future<Conversation> createConversation(String title) async {
final response = await http.post("$baseUrlCore/user/conversation",
headers: {"Content-Type": "application/json"},
@ -20,27 +29,34 @@ class ConversationApiProvider {
await http.delete("$baseUrlCore/user/conversation/$id");
Future<List<Conversation>> fetchConversations() async {
final response =
await http.get("$baseUrlCore/user/$globalUserId/conversation");
return jsonDecode(response.body)
.map<Conversation>(
(conversation) => Conversation.fromJson(conversation))
.toList();
try {
final responseBody = await this.cache.fetch("$baseUrlCore/user/$globalUserId/conversation");
return jsonDecode(responseBody)
.map<Conversation>(
(conversation) => Conversation.fromJson(conversation))
.toList();
} catch(e) {
throw e;
}
}
Future<Conversation> fetchConversation(String id) async {
final response = await http.get("baseUrlCore/user/conversation/$id");
return Conversation.fromJson(jsonDecode(response.body));
try {
final responseBody = await this.cache.fetch("$baseUrlCore/user/conversation/$id");
return Conversation.fromJson(jsonDecode(responseBody));
} catch(e) {
throw e;
}
}
Future<List<User>> fetchConversationMembers(String id) async {
final response = await http
.get("$baseUrlCore/user/$globalUserId/conversation/$id/member");
return jsonDecode(response.body)
.map<User>((user) => User.fromJson(user))
.toList();
try {
final responseBody = await this.cache.fetch("$baseUrlCore/user/$globalUserId/conversation/$id/member");
return jsonDecode(responseBody)
.map<User>((user) => User.fromJson(user))
.toList();
} catch(e) {
throw e;
}
}
}

View File

@ -3,9 +3,17 @@ import "dart:convert";
import "package:http/http.dart" as http;
import "../models/user_model.dart";
import "../services/cache_http.dart";
import "../../settings.dart";
class UserApiProvider {
CacheHttp cache;
Future<void> init() async {
this.cache = new CacheHttp();
await this.cache.init();
}
Future<User> createUser(User user) async {
// Prob need to add the headers
final response = await http.post("$baseUrlCore/user",
@ -15,15 +23,20 @@ class UserApiProvider {
}
Future<User> fetchUserByPhone(String phoneNumber) async {
final uri = Uri.https(baseUrlCore, "/user", {"phone_number": phoneNumber});
final response = await http.get(uri);
return User.fromJson(jsonDecode(response.body));
try {
final responseBody = await this.cache.fetch("$baseUrlCore/user?phone_number=$phoneNumber");
return User.fromJson(jsonDecode(responseBody));
} catch(e) {
throw e;
}
}
Future<User> fetchUserById(String id) async {
final response = await http.get("$baseUrlCore/user/id/$id");
return User.fromJson(jsonDecode(response.body));
try {
final responseBody = await this.cache.fetch("$baseUrlCore/user/id/$id");
return User.fromJson(jsonDecode(responseBody));
} catch(e) {
throw e;
}
}
}

View File

@ -4,7 +4,7 @@ import "package:http/http.dart" as http;
import 'package:sqflite/sqflite.dart';
class CacheManager {
class CacheHttp {
Database db;
// Call this immediately after instantiating new CacheManager