Skip to content

Commit

Permalink
Refactor : change http.Response to Response
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexis L committed Nov 19, 2022
1 parent f8c35e9 commit b9b32a8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 26 deletions.
5 changes: 3 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:flutter/material.dart';
import 'package:datadome/datadome.dart';
import 'package:http/http.dart' as http;
import 'package:http/http.dart';

void main() {
runApp(MyApp());
Expand Down Expand Up @@ -30,7 +30,8 @@ class _MyAppState extends State<MyApp> {
//make request
DataDome client = DataDome('test');

http.Response response = await client.post(url: 'https://datadome.co/wp-json', headers: {'User-Agent': 'BLOCKUA'}, body: ['id', 'data', 'test']);
Response response = await client.post(url: 'https://datadome.co/wp-json', headers: {'User-Agent': 'BLOCKUA'}, body: ['idJJ', 'data', 'test']);

print('Response status: ${response.statusCode}');
print('Response headers: ${response.headers}');
print('Response body: ${response.body}');
Expand Down
50 changes: 26 additions & 24 deletions lib/datadome.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
import 'package:http/http.dart';


class DataDome {
Expand All @@ -22,8 +22,8 @@ class DataDome {
/// The url should be a [String].
/// The headers should be a [Map<String, String>].
///
/// This method executes and return a [http.Response] instance.
Future<http.Response> get({
/// This method executes and return a [Response] instance.
Future<Response> get({
required String url,
Map<String, String> headers = const {}}) async {

Expand All @@ -34,8 +34,8 @@ class DataDome {
/// The url should be a [String].
/// The headers should be a [Map<String, String>].
///
/// This method executes and return a [http.Response] instance.
Future<http.Response> delete({
/// This method executes and return a [Response] instance.
Future<Response> delete({
required String url,
Map<String, String> headers = const {}}) async {

Expand All @@ -47,8 +47,8 @@ class DataDome {
/// The headers should be a [Map<String, String>].
/// The body can be a List or a Map.
///
/// This method executes and return a [http.Response] instance.
Future<http.Response> post({
/// This method executes and return a [Response] instance.
Future<Response> post({
required String url,
Map<String, String> headers = const {},
body}) async {
Expand All @@ -61,8 +61,8 @@ class DataDome {
/// The headers should be a [Map<String, String>].
/// The body can be a List or a Map.
///
/// This method executes and return a [http.Response] instance.
Future<http.Response> put({
/// This method executes and return a [Response] instance.
Future<Response> put({
required String url,
Map<String, String> headers = const {},
body}) async {
Expand All @@ -75,8 +75,8 @@ class DataDome {
/// The headers should be a [Map<String, String>].
/// The body can be a List or a Map.
///
/// This method executes and return a [http.Response] instance.
Future<http.Response> patch({
/// This method executes and return a [Response] instance.
Future<Response> patch({
required String url,
Map<String, String> headers = const {},
body}) async {
Expand All @@ -90,13 +90,8 @@ class DataDome {
/// The body can be a List or a Map.
///
/// This method executes the request using the underlying native SDKs
/// and creates a [http.Response] instance accordingly.
Future<http.Response> _request(
_HttpMethod method,
String url,
Map<String, String> headers,
body,
) async {
/// and creates a [Response] instance accordingly.
Future<Response> _request(_HttpMethod method, String url, Map<String, String> headers, body,) async {

final args = {
'csk': this.key,
Expand All @@ -107,12 +102,19 @@ class DataDome {
};

final Map<String, dynamic>? response = await _channel.invokeMapMethod('request', args);

Map<String, String> responseHeaders = new Map<String, String>.from(response?['headers'] ?? "");

http.Response httpResponse = http.Response.bytes(
response?['data'] ?? [],
response?['code'] ?? [],
Map<String, String> responseHeaders = {};
List<int> responseData = [];
int responseCode = 404;

if(response != null){
responseHeaders = Map<String, String>.from(response['headers']);
responseData = response['data'];
responseCode = response['code'];
}

Response httpResponse = Response.bytes(
responseData,
responseCode,
headers: responseHeaders
);

Expand Down

0 comments on commit b9b32a8

Please sign in to comment.