From b9b32a822a534ee1e1aac406a95fa592073d72af Mon Sep 17 00:00:00 2001 From: Alexis L Date: Sat, 19 Nov 2022 12:21:16 +0100 Subject: [PATCH] Refactor : change http.Response to Response --- example/lib/main.dart | 5 +++-- lib/datadome.dart | 50 ++++++++++++++++++++++--------------------- 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index b529119..f422e73 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -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()); @@ -30,7 +30,8 @@ class _MyAppState extends State { //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}'); diff --git a/lib/datadome.dart b/lib/datadome.dart index a0e0a37..46cf986 100644 --- a/lib/datadome.dart +++ b/lib/datadome.dart @@ -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 { @@ -22,8 +22,8 @@ class DataDome { /// The url should be a [String]. /// The headers should be a [Map]. /// - /// This method executes and return a [http.Response] instance. - Future get({ + /// This method executes and return a [Response] instance. + Future get({ required String url, Map headers = const {}}) async { @@ -34,8 +34,8 @@ class DataDome { /// The url should be a [String]. /// The headers should be a [Map]. /// - /// This method executes and return a [http.Response] instance. - Future delete({ + /// This method executes and return a [Response] instance. + Future delete({ required String url, Map headers = const {}}) async { @@ -47,8 +47,8 @@ class DataDome { /// The headers should be a [Map]. /// The body can be a List or a Map. /// - /// This method executes and return a [http.Response] instance. - Future post({ + /// This method executes and return a [Response] instance. + Future post({ required String url, Map headers = const {}, body}) async { @@ -61,8 +61,8 @@ class DataDome { /// The headers should be a [Map]. /// The body can be a List or a Map. /// - /// This method executes and return a [http.Response] instance. - Future put({ + /// This method executes and return a [Response] instance. + Future put({ required String url, Map headers = const {}, body}) async { @@ -75,8 +75,8 @@ class DataDome { /// The headers should be a [Map]. /// The body can be a List or a Map. /// - /// This method executes and return a [http.Response] instance. - Future patch({ + /// This method executes and return a [Response] instance. + Future patch({ required String url, Map headers = const {}, body}) async { @@ -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 _request( - _HttpMethod method, - String url, - Map headers, - body, - ) async { + /// and creates a [Response] instance accordingly. + Future _request(_HttpMethod method, String url, Map headers, body,) async { final args = { 'csk': this.key, @@ -107,12 +102,19 @@ class DataDome { }; final Map? response = await _channel.invokeMapMethod('request', args); - - Map responseHeaders = new Map.from(response?['headers'] ?? ""); - - http.Response httpResponse = http.Response.bytes( - response?['data'] ?? [], - response?['code'] ?? [], + Map responseHeaders = {}; + List responseData = []; + int responseCode = 404; + + if(response != null){ + responseHeaders = Map.from(response['headers']); + responseData = response['data']; + responseCode = response['code']; + } + + Response httpResponse = Response.bytes( + responseData, + responseCode, headers: responseHeaders );