Skip to content

Commit

Permalink
Moved rest client to packages
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkkiller committed May 9, 2023
1 parent 57d7406 commit 763558b
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 53 deletions.
7 changes: 7 additions & 0 deletions packages/rest_client/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# https://dart.dev/guides/libraries/private-files
# Created by `dart pub`
.dart_tool/

# Avoid committing pubspec.lock for library packages; see
# https://dart.dev/guides/libraries/private-files#pubspeclock.
pubspec.lock
1 change: 1 addition & 0 deletions packages/rest_client/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: package:sizzle_lints/sizzle_lints.yaml
5 changes: 5 additions & 0 deletions packages/rest_client/lib/rest_client.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
library rest_client;

export 'src/rest_client_base.dart';
export 'src/exception/network_exception.dart';
export 'src/rest_client.dart';
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import 'package:meta/meta.dart';
import 'package:sizzle_starter/src/core/utils/annotation.dart';

@immutable
@exception
abstract class NetworkException implements Exception {}

@immutable
@exception
class RestClientException implements NetworkException {
const RestClientException({
this.message,
Expand All @@ -21,7 +18,6 @@ class RestClientException implements NetworkException {
}

@immutable
@exception
class InternalServerException implements NetworkException {
const InternalServerException({
this.message,
Expand Down
42 changes: 42 additions & 0 deletions packages/rest_client/lib/src/rest_client.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'package:http/http.dart' as http;
import 'package:rest_client/src/rest_client_base.dart';

abstract class RestClient {
factory RestClient({
required String baseUrl,
http.Client? client,
}) = RestClientBase;

Future<Map<String, Object?>> get(
String path, {
Map<String, Object?>? headers,
Map<String, Object?>? queryParams,
});

Future<Map<String, Object?>> post(
String path, {
required Map<String, Object?> body,
Map<String, Object?>? headers,
Map<String, Object?>? queryParams,
});

Future<Map<String, Object?>> put(
String path, {
required Map<String, Object?> body,
Map<String, Object?>? headers,
Map<String, Object?>? queryParams,
});

Future<Map<String, Object?>> delete(
String path, {
Map<String, Object?>? headers,
Map<String, Object?>? queryParams,
});

Future<Map<String, Object?>> patch(
String path, {
required Map<String, Object?> body,
Map<String, Object?>? headers,
Map<String, Object?>? queryParams,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:meta/meta.dart';
import 'package:path/path.dart' as p;
import 'package:platform_info/platform_info.dart' as info;
import 'package:sizzle_starter/src/core/utils/exception/network_exception.dart';
import 'package:rest_client/src/exception/network_exception.dart';
import 'package:rest_client/src/rest_client.dart';

@immutable
class RestClient {
RestClient({
class RestClientBase implements RestClient {
RestClientBase({
required String baseUrl,
http.Client? client,
}) : _client = client ?? http.Client(),
Expand All @@ -17,6 +17,7 @@ class RestClient {
final Uri _baseUri;
final http.Client _client;

@override
Future<Map<String, Object?>> get(
String path, {
Map<String, Object?>? headers,
Expand All @@ -29,6 +30,7 @@ class RestClient {
queryParams: queryParams,
);

@override
Future<Map<String, Object?>> post(
String path, {
required Map<String, Object?> body,
Expand All @@ -42,6 +44,7 @@ class RestClient {
queryParams: queryParams,
);

@override
Future<Map<String, Object?>> put(
String path, {
required Map<String, Object?> body,
Expand All @@ -55,6 +58,7 @@ class RestClient {
queryParams: queryParams,
);

@override
Future<Map<String, Object?>> delete(
String path, {
Map<String, Object?>? headers,
Expand All @@ -67,6 +71,7 @@ class RestClient {
queryParams: queryParams,
);

@override
Future<Map<String, Object?>> patch(
String path, {
required Map<String, Object?> body,
Expand All @@ -89,7 +94,6 @@ class RestClient {
}) async {
try {
final request = buildRequest(
baseUri: _baseUri,
method: method,
path: path,
queryParams: queryParams,
Expand Down Expand Up @@ -124,7 +128,7 @@ class RestClient {

@protected
@visibleForTesting
static List<int> encodeBody(
List<int> encodeBody(
Map<String, Object?> body,
) {
try {
Expand All @@ -139,7 +143,7 @@ class RestClient {

@protected
@visibleForTesting
static Map<String, Object?> decodeResponse(http.Response response) {
Map<String, Object?> decodeResponse(http.Response response) {
final contentType = response.headers['content-type'] ?? response.headers['Content-Type'];
if (contentType?.contains('application/json') ?? false) {
final body = response.body;
Expand Down Expand Up @@ -180,35 +184,33 @@ class RestClient {

@protected
@visibleForTesting
static Uri buildUri({
required Uri baseUri,
Uri buildUri({
required String path,
Map<String, Object?>? queryParams,
}) {
final uri = Uri.tryParse(path);
if (uri == null) return baseUri;
if (uri == null) return _baseUri;
final queryParameters = <String, Object?>{
...baseUri.queryParameters,
..._baseUri.queryParameters,
...uri.queryParameters,
...?queryParams,
};
return baseUri.replace(
path: p.normalize(p.join(baseUri.path, uri.path)),
return _baseUri.replace(
path: p.normalize(p.join(_baseUri.path, uri.path)),
queryParameters: queryParameters.isEmpty ? null : queryParameters,
);
}

@protected
@visibleForTesting
static http.Request buildRequest({
required Uri baseUri,
http.Request buildRequest({
required String method,
required String path,
Map<String, Object?>? queryParams,
Map<String, Object?>? body,
Map<String, Object?>? headers,
}) {
final uri = buildUri(path: path, baseUri: baseUri, queryParams: queryParams);
final uri = buildUri(path: path, queryParams: queryParams);
final request = http.Request(method, uri);
if (body != null) request.bodyBytes = encodeBody(body);
request.headers.addAll({
Expand All @@ -223,7 +225,6 @@ class RestClient {
// by the server even if it is HTTP/1.1+
'Pragma': 'no-cache',
'Accept': 'application/json',
'User-Agent': info.Platform.I.version,
...?headers?.map((key, value) => MapEntry(key, value.toString())),
});
return request;
Expand Down
18 changes: 18 additions & 0 deletions packages/rest_client/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: rest_client
description: A starting point for Dart libraries or applications.
version: 1.0.0
# repository: https://github.com/my_org/my_repo

environment:
sdk: '>=2.19.6 <3.0.0'

# dependencies:
# path: ^1.8.0

dev_dependencies:
sizzle_lints: ^1.0.10
test: ^1.21.0
dependencies:
http: ^0.13.6
meta: ^1.8.0
path: ^1.8.0
Loading

0 comments on commit 763558b

Please sign in to comment.