refactor(http)!: fix cascade dart good practices + docs

This commit is contained in:
2023-04-13 23:29:27 +02:00
parent 216a6c2aae
commit 07c2f4aeff
33 changed files with 303 additions and 974 deletions
@@ -14,8 +14,14 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
/// Defines some authentication methods
abstract class AuthenticationMethods {
/// The `Basic` authentication method.
static const String basic = 'Basic';
/// The `Bearer` authentication method.
static const String bearer = 'Bearer';
/// The `Digest` authentication method.
static const String digest = 'Digest';
}
@@ -16,7 +16,11 @@
import 'dart:convert';
class Convert {
/// Defines some convert functions.
abstract class Convert {
/// Converts a list of bytes to a hex string.
///
/// If [upperCase] is `true`, the hex string will be in uppercase.
static String toHex(List<int> bytes, {bool upperCase = false}) {
final buffer = StringBuffer();
for (final int part in bytes) {
@@ -32,6 +36,11 @@ class Convert {
}
}
/// Converts a map to a query string.
///
/// If [encoding] is `null`, the default encoding is `utf8`.
///
/// For example, the map `{a: 1, b: 2}` will be converted to `a=1&b=2`.
static String mapToQuery(Map<String, String> map, {Encoding? encoding}) {
final pairs = <List<String>>[];
map.forEach(
@@ -45,6 +54,7 @@ class Convert {
}
extension UriX on Uri {
/// Returns a new [Uri] by appending the given [path] to this [Uri].
Uri operator +(String path) {
final thisPath = toString();
return Uri.parse(thisPath + path);
@@ -18,7 +18,8 @@ import 'dart:convert';
import 'package:crypto/crypto.dart';
class Crypto {
/// Defines some crypto functions.
abstract class Crypto {
/// Hash a string using MD5
static String md5Hash(String data) {
final content = const Utf8Encoder().convert(data);
@@ -17,7 +17,9 @@
import 'dart:core';
import 'dart:math';
/// Defines some delay functions.
abstract class Delay {
/// Returns a delay based on the [attempt].
static Duration getRetryDelay(int attempt) {
assert(attempt >= 0, 'attempt cannot be negative');
if (attempt <= 0) {
@@ -19,12 +19,13 @@ import 'dart:math';
import 'package:wyatt_http_client/src/utils/convert.dart';
import 'package:wyatt_http_client/src/utils/crypto.dart';
/// A class for digest authentication.
class DigestAuth {
// request counter
DigestAuth(this.username, this.password);
String username;
String password;
final String username;
final String password;
// must get from first response
String? _algorithm;
@@ -14,8 +14,14 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
/// Defines some header keys.
abstract class HeaderKeys {
/// The `Authorization` header key.
static const String authorization = 'Authorization';
/// The `WWW-Authenticate` header key.
static const String wwwAuthenticate = 'WWW-Authenticate';
/// The `Content-Type` header key.
static const String contentType = 'Content-Type';
}
@@ -14,6 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
/// Defines http verb methods.
enum HttpMethods {
head('HEAD'),
get('GET'),
@@ -24,5 +25,8 @@ enum HttpMethods {
const HttpMethods(this.method);
/// Returns the method of the http verb.
///
/// For example, the method of [HttpMethods.get] is `GET`.
final String method;
}
@@ -83,6 +83,7 @@ enum HttpStatus {
const HttpStatus(this.statusCode);
/// Returns the [HttpStatus] with the given [statusCode].
factory HttpStatus.from(int status) =>
HttpStatus.values.firstWhere((element) => element.statusCode == status);
@@ -98,13 +99,18 @@ enum HttpStatus {
return false;
}
/// Checks if the status code is in the range of 100-199.
bool isInfo() => statusCode >= 100 && statusCode < 200;
/// Checks if the status code is in the range of 200-299.
bool isSuccess() => statusCode >= 200 && statusCode < 300;
/// Checks if the status code is in the range of 300-399.
bool isRedirection() => statusCode >= 300 && statusCode < 400;
/// Checks if the status code is in the range of 400-499.
bool isClientError() => statusCode >= 400 && statusCode < 500;
/// Checks if the status code is in the range of 500-599.
bool isServerError() => statusCode >= 500 && statusCode < 600;
}
@@ -14,9 +14,13 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
/// Defines few protocols
enum Protocols {
http,
https;
/// Returns the scheme of the protocol.
///
/// For example, the scheme of [Protocols.http] is `http://`.
String get scheme => '$name://';
}
@@ -16,6 +16,7 @@
import 'package:http/http.dart';
/// Defines some request utils.
abstract class RequestUtils {
static Request _copyNormalRequestWith(
Request original, {
@@ -38,6 +39,9 @@ abstract class RequestUtils {
return request;
}
/// Copies the given [original] request and returns a new request with the
/// given [method], [url], [headers], [maxRedirects], [followRedirects],
/// [persistentConnection] and [body].
static BaseRequest copyRequestWith(
BaseRequest original, {
String? method,
@@ -77,6 +81,8 @@ abstract class RequestUtils {
return request;
}
/// Copies the given [original] request and returns a new request.
/// This method is useful when you want to modify the request
static BaseRequest copyRequest(BaseRequest original) {
if (original is Request) {
return _copyNormalRequest(original);