Created
June 26, 2021 08:56
-
-
Save nelson1995/1269d8223102e38ccba35af94bd37176 to your computer and use it in GitHub Desktop.
An easy to use Http client for flutter.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:convert'; | |
import 'package:dio/dio.dart'; | |
import 'package:http/http.dart' as http; | |
import 'server_response.dart'; | |
class HttpClient { | |
static HttpClient instance = new HttpClient.internal(); | |
HttpClient.internal(); | |
factory HttpClient() => instance; | |
final JsonDecoder decoder = new JsonDecoder(); | |
// creating a new dio instance | |
final Dio request = new Dio(); | |
// GET request | |
Future<ServerResponse> getRequest(String url, { String token }) async { | |
var uri = Uri.encodeFull(url); | |
try { | |
var response = await request.get(uri, options: Options( | |
headers: { 'Authorization': 'Bearer $token' } | |
)); | |
if (response.statusCode < 200 || response.statusCode > 400 || json == null) { | |
return ServerResponse(isSuccess: false, response: response.statusMessage); | |
} | |
return ServerResponse(isSuccess: true, response: response.data); | |
} on DioError catch (error) { | |
return ServerResponse(isSuccess: false, response: error.response != null ? error.response.statusMessage : error.message); | |
} | |
} | |
// GET requests | |
Future<ServerResponse> postRequest(String url, Map<String, dynamic> data, { String token }) async { | |
var uri = Uri.encodeFull(url); //encode the url passed in param e.g http:://192.168.x.x:8003/api/ | |
try { | |
FormData formData = new FormData.fromMap(data); | |
var response = await request.post(uri, data: formData, options: Options( | |
headers: { 'Authorization': 'Bearer $token' }, | |
)); | |
if (response.statusCode < 200 || response.statusCode > 400 || json == null) { | |
return ServerResponse(isSuccess: false, response: response.statusMessage); | |
} | |
return ServerResponse(isSuccess: true, response: response.data); | |
} on DioError catch (error) { | |
return ServerResponse(isSuccess: false, response: error.response != null ? error.response.statusMessage : error.message); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Using the HttpClient class | |
*/ | |
import 'http_client.dart'; | |
getUsers(){ | |
String accessToken = 'dad1....'; | |
String url="http://192.168.x.x:xxxx/api/users/" | |
HttpClient.instance.getRequest(url, token: accessToken).then((response) { | |
if (response.isSuccess) { | |
print("response successful"); | |
print("response body: ${response.response}"); | |
}else { | |
print("response failed"); | |
print("responseError: ${response.response} "); | |
} | |
}); | |
} | |
createUser(){ | |
Map<String,dynamic> data ={ | |
"full_name":"Nelson K", | |
"username":"Nelson1995", | |
"password":"1234" | |
}; | |
String accessToken = 'dad1....'; | |
String url="http://192.168.x.x:xxxx/api/create" | |
HttpClient.instance.postRequest(url,data,token: accessToken).then((response) { | |
if(response.isSuccess){ | |
print("response successful"); | |
print("response body: ${response.response}"); | |
}else{ | |
print("response failed"); | |
print("responseError: ${response.response} "); | |
} | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ServerResponse { | |
final bool isSuccess; | |
final dynamic response; | |
ServerResponse({this.isSuccess, this.response}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment