Skip to content

Commit

Permalink
feat(katana): Added Api class.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathrunet committed Sep 6, 2022
1 parent 1dd3a7e commit 6d2ba70
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 7 deletions.
17 changes: 10 additions & 7 deletions packages/katana/lib/katana.dart
Expand Up @@ -17,13 +17,12 @@ import 'package:encrypt/encrypt.dart';
import 'package:intl/intl.dart';
import 'package:sprintf/sprintf.dart';
import 'package:uuid/uuid.dart';
import 'package:http/http.dart' as http;

export 'package:intl/date_symbol_data_local.dart' show initializeDateFormatting;
export 'package:intl/intl.dart' show DateFormat;
export 'package:sprintf/sprintf.dart' show sprintf;

part "src/const.dart";
part "src/functions.dart";
part "src/typedef.dart";

part 'extension/date_time_extensions.dart';
part 'extension/date_time_iterable_extensions.dart';
part 'extension/double_extensions.dart';
Expand All @@ -33,9 +32,6 @@ part 'extension/int_extensions.dart';
part 'extension/int_iterable_extensions.dart';
part 'extension/iterable_extensions.dart';
part 'extension/iterable_of_iterable_extensions.dart';
part 'extension/random_extensions.dart';
part 'extension/set_extensions.dart';
part 'extension/string_extensions.dart';
part 'extension/json_extensions.dart';
part 'extension/list_extensions.dart';
part 'extension/map_extensions.dart';
Expand All @@ -51,3 +47,10 @@ part 'extension/nullable_object_extensions.dart';
part 'extension/nullable_set_extensions.dart';
part 'extension/nullable_string_extensions.dart';
part 'extension/nullable_value_iterable_extensions.dart';
part 'extension/random_extensions.dart';
part 'extension/set_extensions.dart';
part 'extension/string_extensions.dart';
part "src/const.dart";
part "src/functions.dart";
part "src/typedef.dart";
part "src/api.dart";
144 changes: 144 additions & 0 deletions packages/katana/lib/src/api.dart
@@ -0,0 +1,144 @@
part of katana;

/// HTTP requests can be sent.
///
/// Wrapper for http package.
class Api {
const Api._();

/// Sends an HTTP DELETE request with the given headers to the given URL.
///
/// This automatically initializes a new [http.Client] and closes that client once the request is complete. If you're planning on making multiple requests to the same server, you should use a single [http.Client] for all of those requests.
Future<ApiResponse?> delete(
String path, {
Map<String, String>? headers,
Object? body,
Encoding? encoding,
}) async {
final url = Uri.tryParse(path);
if (url == null) {
return null;
}
return http.delete(
url,
headers: headers,
body: body,
encoding: encoding,
);
}

/// Sends an HTTP GET request with the given headers to the given URL.
///
/// This automatically initializes a new [http.Client] and closes that client once the request is complete. If you're planning on making multiple requests to the same server, you should use a single [http.Client] for all of those requests.
Future<ApiResponse?> get(String path, {Map<String, String>? headers}) async {
final url = Uri.tryParse(path);
if (url == null) {
return null;
}
return http.get(
url,
headers: headers,
);
}

/// Sends an HTTP HEAD request with the given headers to the given URL.
///
/// This automatically initializes a new [http.Client] and closes that client once the request is complete. If you're planning on making multiple requests to the same server, you should use a single [http.Client] for all of those requests.
Future<ApiResponse?> head(String path, {Map<String, String>? headers}) async {
final url = Uri.tryParse(path);
if (url == null) {
return null;
}
return http.head(
url,
headers: headers,
);
}

/// Sends an HTTP PATCH request with the given headers and body to the given URL.
///
/// [body] sets the body of the request. It can be a [String], a [List] or a [Map<String, String>]. If it's a String, it's encoded using [encoding] and used as the body of the request. The content-type of the request will default to "text/plain".
///
/// If [body] is a List, it's used as a list of bytes for the body of the request.
///
/// If [body] is a Map, it's encoded as form fields using [encoding]. The content-type of the request will be set to "application/x-www-form-urlencoded"; this cannot be overridden.
///
/// [encoding] defaults to [utf8].
Future<ApiResponse?> patch(
String path, {
Map<String, String>? headers,
Object? body,
Encoding? encoding,
}) async {
final url = Uri.tryParse(path);
if (url == null) {
return null;
}
return http.patch(
url,
headers: headers,
body: body,
encoding: encoding,
);
}

/// Sends an HTTP POST request with the given headers and body to the given URL.
///
/// [body] sets the body of the request. It can be a [String], a [List] or a [Map<String, String>]. If it's a String, it's encoded using [encoding] and used as the body of the request. The content-type of the request will default to "text/plain".
///
/// If [body] is a List, it's used as a list of bytes for the body of the request.
///
/// If [body] is a Map, it's encoded as form fields using [encoding]. The content-type of the request will be set to "application/x-www-form-urlencoded"; this cannot be overridden.
///
/// [encoding] defaults to [utf8].
Future<ApiResponse?> post(
String path, {
Map<String, String>? headers,
Object? body,
Encoding? encoding,
}) async {
final url = Uri.tryParse(path);
if (url == null) {
return null;
}
return http.post(
url,
headers: headers,
body: body,
encoding: encoding,
);
}

/// Sends an HTTP PUT request with the given headers and body to the given URL.
///
/// [body] sets the body of the request. It can be a [String], a [List] or a [Map<String, String>]. If it's a String, it's encoded using [encoding] and used as the body of the request. The content-type of the request will default to "text/plain".
///
/// If [body] is a List, it's used as a list of bytes for the body of the request.
///
/// If [body] is a Map, it's encoded as form fields using [encoding]. The content-type of the request will be set to "application/x-www-form-urlencoded"; this cannot be overridden.
///
/// [encoding] defaults to [utf8].
Future<ApiResponse?> put(
String path, {
Map<String, String>? headers,
Object? body,
Encoding? encoding,
}) async {
final url = Uri.tryParse(path);
if (url == null) {
return null;
}
return http.put(
url,
headers: headers,
body: body,
encoding: encoding,
);
}
}

/// An HTTP response where the entire response body is known in advance.
typedef ApiResponse = http.Response;

/// An HTTP request where the entire request body is known in advance.
typedef ApiResquest = http.Request;
7 changes: 7 additions & 0 deletions packages/katana/pubspec.lock
Expand Up @@ -106,6 +106,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
http:
dependency: "direct main"
description:
name: http
url: "https://pub.dartlang.org"
source: hosted
version: "0.13.5"
http_multi_server:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions packages/katana/pubspec.yaml
Expand Up @@ -14,6 +14,7 @@ dependencies:
uuid: ^3.0.4
sprintf: ^6.0.0
encrypt: ^5.0.1
http: ^0.13.5

dev_dependencies:
test: ^1.21.3

0 comments on commit 6d2ba70

Please sign in to comment.