Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added queryParameters support and moved port to Uri. #25

Merged
merged 19 commits into from Mar 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/qa.yml
@@ -0,0 +1,23 @@
name: Quality Assurance

on: [push]

jobs:
quality-assurance:
name: QA on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
steps:
- uses: actions/checkout@v1
- uses: actions/setup-java@v1
with:
java-version: '12.x'
- uses: subosito/flutter-action@v1.2.0
with:
flutter-version: '1.11.0'
channel: 'beta'
- run: flutter pub get
- run: flutter test .
- run: flutter analyze .
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -55,6 +55,7 @@ version
**/doc/api/
.dart_tool/
.flutter-plugins
.flutter-plugins-dependencies
.packages
.pub-cache/
.pub/
Expand Down
13 changes: 12 additions & 1 deletion CHANGELOG.md
Expand Up @@ -12,4 +12,15 @@

## 3.0.5

- fix: added body in patch and delete methods
- fix: added body in patch and delete methods

## 3.1.0

- BREAKING CHANGE: removed port from url. Added parameter port according to `Uri` best practices.
- feature: added queryParameter support in get requests
- feature: created Github QA workflow to prevent pushes that breaks analyze or tests
- enhancement: changed http method strings into enum to avoid errors
- enhancement: improved type of arguments to prevent unexpected errors
- enhancement: added more validations on tests
- enhancement: executed flutter format to improve pub score
- enhancement: changed some anti patterns.
4 changes: 2 additions & 2 deletions analysis_options.yaml
Expand Up @@ -10,5 +10,5 @@ include: package:pedantic/analysis_options.yaml
# - camel_case_types

analyzer:
# exclude:
# - path/to/excluded/files/**
exclude:
- test/**
3 changes: 2 additions & 1 deletion lib/requests.dart
@@ -1,2 +1,3 @@
library requests;
export 'src/requests.dart';

export 'src/requests.dart';
186 changes: 106 additions & 80 deletions lib/src/requests.dart
Expand Up @@ -11,6 +11,7 @@ import 'common.dart';
import 'event.dart';

enum RequestBodyEncoding { JSON, FormURLEncoded, PlainText }
enum HttpMethod { GET, PUT, PATCH, POST, DELETE, HEAD }

final Logger log = Logger('requests');

Expand Down Expand Up @@ -62,12 +63,6 @@ class Requests {
const Requests();

static final Event onError = Event();
static const String HTTP_METHOD_GET = "get";
static const String HTTP_METHOD_PUT = "put";
static const String HTTP_METHOD_PATCH = "patch";
static const String HTTP_METHOD_DELETE = "delete";
static const String HTTP_METHOD_POST = "post";
static const String HTTP_METHOD_HEAD = "head";
static const int DEFAULT_TIMEOUT_SECONDS = 10;

static const RequestBodyEncoding DEFAULT_BODY_ENCODING =
Expand Down Expand Up @@ -168,43 +163,51 @@ class Requests {
}

static Future<Response> head(String url,
{headers,
bodyEncoding = DEFAULT_BODY_ENCODING,
timeoutSeconds = DEFAULT_TIMEOUT_SECONDS,
persistCookies = true,
verify = true}) {
return _httpRequest(HTTP_METHOD_HEAD, url,
{Map<String, String> headers,
int port,
RequestBodyEncoding bodyEncoding = DEFAULT_BODY_ENCODING,
int timeoutSeconds = DEFAULT_TIMEOUT_SECONDS,
bool persistCookies = true,
bool verify = true}) {
return _httpRequest(HttpMethod.HEAD, url,
bodyEncoding: bodyEncoding,
port: port,
headers: headers,
timeoutSeconds: timeoutSeconds,
persistCookies: persistCookies,
verify: verify);
}

static Future<Response> get(String url,
{headers,
bodyEncoding = DEFAULT_BODY_ENCODING,
timeoutSeconds = DEFAULT_TIMEOUT_SECONDS,
persistCookies = true,
verify = true}) {
return _httpRequest(HTTP_METHOD_GET, url,
{Map<String, String> headers,
Map<String, dynamic> queryParameters,
int port,
RequestBodyEncoding bodyEncoding = DEFAULT_BODY_ENCODING,
int timeoutSeconds = DEFAULT_TIMEOUT_SECONDS,
bool persistCookies = true,
bool verify = true}) {
return _httpRequest(HttpMethod.GET, url,
bodyEncoding: bodyEncoding,
queryParameters: queryParameters,
port: port,
headers: headers,
timeoutSeconds: timeoutSeconds,
persistCookies: persistCookies,
verify: verify);
}

static Future<Response> patch(String url,
{headers,
json,
body,
bodyEncoding = DEFAULT_BODY_ENCODING,
timeoutSeconds = DEFAULT_TIMEOUT_SECONDS,
persistCookies = true,
verify = true}) {
return _httpRequest(HTTP_METHOD_PATCH, url,
{Map<String, String> headers,
int port,
Map<String, dynamic> json,
dynamic body,
RequestBodyEncoding bodyEncoding = DEFAULT_BODY_ENCODING,
int timeoutSeconds = DEFAULT_TIMEOUT_SECONDS,
bool persistCookies = true,
bool verify = true}) {
return _httpRequest(HttpMethod.PATCH, url,
bodyEncoding: bodyEncoding,
port: port,
json: json,
body: body,
headers: headers,
Expand All @@ -214,15 +217,19 @@ class Requests {
}

static Future<Response> delete(String url,
{headers,
json,
body,
bodyEncoding = DEFAULT_BODY_ENCODING,
timeoutSeconds = DEFAULT_TIMEOUT_SECONDS,
persistCookies = true,
verify = true}) {
return _httpRequest(HTTP_METHOD_DELETE, url,
{Map<String, String> headers,
Map<String, dynamic> json,
Map<String, dynamic> queryParameters,
dynamic body,
int port,
RequestBodyEncoding bodyEncoding = DEFAULT_BODY_ENCODING,
int timeoutSeconds = DEFAULT_TIMEOUT_SECONDS,
bool persistCookies = true,
bool verify = true}) {
return _httpRequest(HttpMethod.DELETE, url,
bodyEncoding: bodyEncoding,
queryParameters: queryParameters,
port: port,
json: json,
body: body,
headers: headers,
Expand All @@ -232,16 +239,18 @@ class Requests {
}

static Future<Response> post(String url,
{json,
body,
bodyEncoding = DEFAULT_BODY_ENCODING,
headers,
timeoutSeconds = DEFAULT_TIMEOUT_SECONDS,
persistCookies = true,
verify = true}) {
return _httpRequest(HTTP_METHOD_POST, url,
{Map<String, dynamic> json,
int port,
dynamic body,
RequestBodyEncoding bodyEncoding = DEFAULT_BODY_ENCODING,
Map<String, String> headers,
int timeoutSeconds = DEFAULT_TIMEOUT_SECONDS,
bool persistCookies = true,
bool verify = true}) {
return _httpRequest(HttpMethod.POST, url,
bodyEncoding: bodyEncoding,
json: json,
port: port,
body: body,
headers: headers,
timeoutSeconds: timeoutSeconds,
Expand All @@ -251,17 +260,19 @@ class Requests {

static Future<Response> put(
String url, {
json,
body,
bodyEncoding = DEFAULT_BODY_ENCODING,
headers,
timeoutSeconds = DEFAULT_TIMEOUT_SECONDS,
persistCookies = true,
verify = true,
int port,
Map<String, dynamic> json,
dynamic body,
RequestBodyEncoding bodyEncoding = DEFAULT_BODY_ENCODING,
Map<String, String> headers,
int timeoutSeconds = DEFAULT_TIMEOUT_SECONDS,
bool persistCookies = true,
bool verify = true,
}) {
return _httpRequest(
HTTP_METHOD_PUT,
HttpMethod.PUT,
url,
port: port,
bodyEncoding: bodyEncoding,
json: json,
body: body,
Expand All @@ -272,14 +283,16 @@ class Requests {
);
}

static Future<Response> _httpRequest(String method, String url,
static Future<Response> _httpRequest(HttpMethod method, String url,
{json,
body,
bodyEncoding = DEFAULT_BODY_ENCODING,
headers,
timeoutSeconds = DEFAULT_TIMEOUT_SECONDS,
persistCookies = true,
verify = true}) async {
RequestBodyEncoding bodyEncoding = DEFAULT_BODY_ENCODING,
Map<String, dynamic> queryParameters,
int port,
Map<String, String> headers,
int timeoutSeconds = DEFAULT_TIMEOUT_SECONDS,
bool persistCookies = true,
bool verify = true}) async {
http.Client client;
if (!verify) {
// Ignore SSL errors
Expand All @@ -306,6 +319,14 @@ class Requests {
throw ArgumentError('cannot use both "json" and "body" choose only one.');
}

if (queryParameters != null) {
uri = uri.replace(queryParameters: queryParameters);
}

if (port != null) {
uri = uri.replace(port: port);
}

if (json != null) {
body = json;
bodyEncoding = RequestBodyEncoding.JSON;
Expand All @@ -314,17 +335,19 @@ class Requests {
if (body != null) {
String contentTypeHeader;

if (bodyEncoding == RequestBodyEncoding.JSON) {
requestBody = Common.toJson(body);
contentTypeHeader = "application/json";
} else if (bodyEncoding == RequestBodyEncoding.FormURLEncoded) {
requestBody = Common.encodeMap(body);
contentTypeHeader = "application/x-www-form-urlencoded";
} else if (bodyEncoding == RequestBodyEncoding.PlainText) {
requestBody = body;
contentTypeHeader = "text/plain";
} else {
throw Exception('unsupported bodyEncoding "$bodyEncoding"');
switch (bodyEncoding) {
case RequestBodyEncoding.JSON:
requestBody = Common.toJson(body);
contentTypeHeader = "application/json";
break;
case RequestBodyEncoding.FormURLEncoded:
requestBody = Common.encodeMap(body);
contentTypeHeader = "application/x-www-form-urlencoded";
break;
case RequestBodyEncoding.PlainText:
requestBody = body;
contentTypeHeader = "text/plain";
break;
}

if (contentTypeHeader != null &&
Expand All @@ -333,39 +356,42 @@ class Requests {
}
}

method = method.toLowerCase();
Future future;

switch (method) {
case HTTP_METHOD_GET:
case HttpMethod.GET:
future = client.get(uri, headers: headers);
break;
case HTTP_METHOD_PUT:
case HttpMethod.PUT:
future = client.put(uri, body: requestBody, headers: headers);
break;
case HTTP_METHOD_DELETE:

case HttpMethod.DELETE:
final request = http.Request("DELETE", uri);
requestBody != null ? request.body = requestBody : null;
request.headers.addAll(headers);

if (requestBody != null) {
request.body = requestBody;
}

future = client.send(request);
break;
case HTTP_METHOD_POST:
case HttpMethod.POST:
future = client.post(uri, body: requestBody, headers: headers);
break;
case HTTP_METHOD_HEAD:
case HttpMethod.HEAD:
future = client.head(uri, headers: headers);
break;
case HTTP_METHOD_PATCH:
case HttpMethod.PATCH:
future = client.patch(uri, body: requestBody, headers: headers);
break;
default:
throw Exception('unsupported http method "$method"');
}

var response = await future.timeout(Duration(seconds: timeoutSeconds));
if(response is http.StreamedResponse){
response = await http.Response.fromStream(response);

if (response is http.StreamedResponse) {
response = await http.Response.fromStream(response);
}

return await _handleHttpResponse(hostname, response, persistCookies);
}
}
3 changes: 2 additions & 1 deletion pubspec.yaml
@@ -1,10 +1,11 @@
name: requests
description: a flutter library that helps with http requests and stored cookies
version: 3.0.5
version: 3.1.0
homepage: https://github.com/jossef/requests
authors:
- Jossef Harush <jossef12@gmail.com>
- Rick Stanley <rick-stanley@hotmail.com>
- Rafael Carvalho Monteiro <rafael@qwkin.io>
- Martin <https://github.com/genesiscz>
- Frederik Pietzko <pietzkof@gmail.com>
environment:
Expand Down