Skip to content

Commit

Permalink
Merge pull request #9 from andgar2010/main
Browse files Browse the repository at this point in the history
feat(http_status): Add methods to check HTTP status code ranges of Class HttpStatus
  • Loading branch information
tech-andgar authored Feb 17, 2024
2 parents 294ba7a + afa3e51 commit 0a4fe1e
Show file tree
Hide file tree
Showing 8 changed files with 1,049 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
steps:
- uses: actions/checkout@v4

- uses: dart-lang/[email protected].1
- uses: dart-lang/[email protected].2
with:
sdk: ${{ matrix.channel }}

Expand Down
16 changes: 16 additions & 0 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Publish to pub.dev

on:
push:
tags:
- '[0-9]+.[0-9]+.[0-9]+*' # for tags like: '1.2.3'

jobs:
publish:
permissions:
id-token: write # Required for authentication using OIDC
uses: dart-lang/setup-dart/.github/workflows/publish.yml@v1
with:
# Specify the github actions deployment environment
environment: pub.dev
# working-directory: path/to/package/within/repository
15 changes: 14 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# Changelog

## [3.1.0] - 2024-02-17

### Added - 3.1.0

- Added new methods to the `HttpStatus` class for checking HTTP status code ranges. Includes `isInformationHttpStatusCode`, `isSuccessfulHttpStatusCode`, `isRedirectHttpStatusCode`, `isClientErrorHttpStatusCode`, and `isServerErrorHttpStatusCode`. These methods facilitate categorizing HTTP status codes without manual comparisons.

### Fixed - 3.1.0

- Added earlyHints 103 in HttpStatus.fromCode for this missing element.

### Changed - 3.1.0

- Documentation Enhanced: Improved documentation for `readme.md`, offering clearer guidelines and usage examples.

## [3.0.0] - 2024-02-12

### Removed - 3.0.0
Expand Down Expand Up @@ -87,7 +101,6 @@
## [2.1.0] - 2024-02-10

- Expanded the Dart SDK compatibility range to '<4.0.0'.
-

## [2.0.1] - 2024-02-10

Expand Down
114 changes: 79 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,7 @@ main() {
// )

print('${HttpStatusCode.noContent}');
// HttpStatus(
// code: 204,
// name: 'No Content',
// description: 'The server has successfully fulfilled the request and that there is no additional content to send in the response payload body.'
// )
// 204

print('${HttpStatus.fromCode(404)}');
// HttpStatus(
Expand All @@ -134,62 +130,110 @@ main() {
// description: 'The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.'
// )


// isInformation (Http Status Code 100 - 199)
// isInformation (Http Status Code 200 - 299)
print(HttpStatusCode.processing.isInformationHttpStatusCode); // true
print(HttpStatusCode.notFound.isInformationHttpStatusCode); // false
print(HttpStatus.fromCode(103).isInformationHttpStatusCode); // true
print(HttpStatus.fromCode(404).isInformationHttpStatusCode); // false
print(103.isInformationHttpStatusCode); // true
print(400.isInformationHttpStatusCode); // false

// isSuccessful (Http Status Code 200 - 299)
print(HttpStatusCode.accepted.isSuccessfulHttpStatusCode); // true
print(HttpStatusCode.notFound.isSuccessfulHttpStatusCode); // false
print(HttpStatus.fromCode(200).isSuccessfulHttpStatusCode); // true
print(HttpStatus.fromCode(404).isSuccessfulHttpStatusCode); // false
print(200.isSuccessfulHttpStatusCode); // true
print(400.isSuccessfulHttpStatusCode); // false

// isRedirect (Http Status Code 300 - 399)
print(HttpStatusCode.permanentRedirect.isRedirectHttpStatusCode); // true
print(HttpStatusCode.notFound.isRedirectHttpStatusCode); // false
print(HttpStatus.fromCode(303).isRedirectHttpStatusCode); // true
print(HttpStatus.fromCode(404).isRedirectHttpStatusCode); // false
print(303.isRedirectHttpStatusCode); // true
print(400.isRedirectHttpStatusCode); // false

// isClientError (Http Status Code 400 - 499)
print(HttpStatusCode.notFound.isClientErrorHttpStatusCode); // true
print(HttpStatusCode.processing.isClientErrorHttpStatusCode); // false
print(HttpStatus.fromCode(404).isClientErrorHttpStatusCode); // true
print(HttpStatus.fromCode(500).isClientErrorHttpStatusCode); // false
print(404.isClientErrorHttpStatusCode); // true
print(200.isClientErrorHttpStatusCode); // false

// isServerError (Http Status Code 500 - 599)
print(HttpStatusCode.internalServerError.isServerError); // true
print(HttpStatusCode.notFound.isServerError); // false
print(HttpStatusCode.internalServerError.isServerErrorHttpStatusCode); // true
print(HttpStatusCode.notFound.isServerErrorHttpStatusCode); // false;
print(HttpStatus.fromCode(502).isServerErrorHttpStatusCode); // true
print(HttpStatus.fromCode(200).isServerErrorHttpStatusCode); // false
print(503.isServerErrorHttpStatusCode); // true
print(200.isServerErrorHttpStatusCode); // false
}
```

```dart
import 'package:http/http.dart' as http;
import 'package:http_status/http_status.dart';
1. Classic method

final res = await http.get(Uri.parse(url));
```dart
import 'package:http/http.dart' as http;
import 'package:http_status/http_status.dart';
if (res.statusCode == HttpStatusCode.ok) {
final httpStatus = HttpStatus.fromCode(res.statusCode);
final res = await http.get(Uri.parse(url));
return {
'statusCode': res.statusCode,
'httpStatus': httpStatus,
'data': res.body
};
}
```
if (res.statusCode == HttpStatusCode.ok) { // res.statusCode == 200
final httpStatus = HttpStatus.fromCode(res.statusCode);
```dart
import 'package:http/http.dart' as http;
import 'package:http_status/http_status.dart';
return {
'statusCode': res.statusCode,
'httpStatus': httpStatus,
'data': res.body
};
}
```

final res = await http.get(Uri.parse(url));
2. Alternative method (Same as #1 method, but with more direct validation using `.isSuccessfulHttpStatusCode`)

if (res.statusCode.isSuccessfulHttpStatusCode) {
final httpStatus = HttpStatus.fromCode(res.statusCode);
```dart
import 'package:http/http.dart' as http;
import 'package:http_status/http_status.dart';
return {
'statusCode': res.statusCode,
'httpStatus': httpStatus,
'data': res.body
};
}
```
final res = await http.get(Uri.parse(url));
if (res.statusCode.isSuccessfulHttpStatusCode) {
final httpStatus = HttpStatus.fromCode(res.statusCode);
return {
'statusCode': res.statusCode,
'httpStatus': httpStatus,
'data': res.body
};
}
```

3. Alternative method (Same as #1 method, if you need the HttpStatus object from the dynamically generated status code of the response)

```dart
import 'package:http/http.dart' as http;
import 'package:http_status/http_status.dart';
final res = await http.get(Uri.parse(url));
final httpStatusResponse = HttpStatus.fromCode(res.statusCode);
if (httpStatusResponse.isSuccessfulHttpStatusCode) {
return {
'statusCode': res.statusCode,
'httpStatus': httpStatusResponse,
'data': res.body
};
} else if (httpStatusResponse.isClientErrorHttpStatusCode) {
// Handle client error
// ...
} else {
// Handle other code error
// ...
}
```

## Thanking all Awesome Contributors :heart:

Expand Down
50 changes: 39 additions & 11 deletions example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,58 @@
import 'package:http_status/http_status.dart';

void main() {
print('${HttpStatusCode.ok}');
print('${HttpStatusCode.ok}'); // 200
print('${HttpStatus.ok}');
print('${HttpStatusCode.noContent}');
// HttpStatus(
// code: 200,
// name: 'OK',
// description: 'The request was fulfilled.'
// )
print('${HttpStatusCode.noContent}'); // 204
print('${HttpStatus.fromCode(404)}');
// HttpStatus(
// code: 404,
// name: 'Not Found',
// description: 'The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.'
// )

// isInformation (HttpStatusCode 200-299)
// isInformation (Http Status Code 200 - 299)
print(HttpStatusCode.processing.isInformationHttpStatusCode); // true
print(HttpStatusCode.notFound.isInformationHttpStatusCode); // false
print(HttpStatus.fromCode(103).isInformationHttpStatusCode); // true
print(HttpStatus.fromCode(404).isInformationHttpStatusCode); // false
print(103.isInformationHttpStatusCode); // true
print(400.isInformationHttpStatusCode); // false

// isSuccessful (HttpStatusCode 200-299)
print(200.isSuccessfulHttpStatusCode);
print(400.isSuccessfulHttpStatusCode);
print(HttpStatusCode.accepted.isSuccessfulHttpStatusCode);
print(HttpStatusCode.notFound.isSuccessfulHttpStatusCode);
// isSuccessful (Http Status Code 200 - 299)
print(HttpStatusCode.accepted.isSuccessfulHttpStatusCode); // true
print(HttpStatusCode.notFound.isSuccessfulHttpStatusCode); // false
print(HttpStatus.fromCode(200).isSuccessfulHttpStatusCode); // true
print(HttpStatus.fromCode(404).isSuccessfulHttpStatusCode); // false
print(200.isSuccessfulHttpStatusCode); // true
print(400.isSuccessfulHttpStatusCode); // false

// isRedirect (HttpStatusCode 300-399)
// isRedirect (Http Status Code 300 - 399)
print(HttpStatusCode.permanentRedirect.isRedirectHttpStatusCode); // true
print(HttpStatusCode.notFound.isRedirectHttpStatusCode); // false
print(HttpStatus.fromCode(303).isRedirectHttpStatusCode); // true
print(HttpStatus.fromCode(404).isRedirectHttpStatusCode); // false
print(303.isRedirectHttpStatusCode); // true
print(400.isRedirectHttpStatusCode); // false

// isClientError (HttpStatusCode 400-499)
// isClientError (Http Status Code 400 - 499)
print(HttpStatusCode.notFound.isClientErrorHttpStatusCode); // true
print(HttpStatusCode.processing.isClientErrorHttpStatusCode); // false
print(HttpStatus.fromCode(404).isClientErrorHttpStatusCode); // true
print(HttpStatus.fromCode(500).isClientErrorHttpStatusCode); // false
print(404.isClientErrorHttpStatusCode); // true
print(200.isClientErrorHttpStatusCode); // false

// isServerError (HttpStatusCode 500-599)
// isServerError (Http Status Code 500 - 599)
print(HttpStatusCode.internalServerError.isServerErrorHttpStatusCode); // true
print(HttpStatusCode.notFound.isServerErrorHttpStatusCode); // false;
print(HttpStatus.fromCode(502).isServerErrorHttpStatusCode); // true
print(HttpStatus.fromCode(200).isServerErrorHttpStatusCode); // false
print(503.isServerErrorHttpStatusCode); // true
print(200.isServerErrorHttpStatusCode); // false
}
17 changes: 17 additions & 0 deletions lib/src/http_status.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// is governed by a BSD-style license that can be found in the LICENSE file.

import 'http_status_code.dart';
import 'utils/int_http_status_code_extension.dart';

/// Each [HttpStatusCode] is described below, including a description of which
/// method(s) it can follow and any metainformation required in the response.
Expand Down Expand Up @@ -2713,6 +2714,7 @@ class HttpStatus {
HttpStatusCode.continue_: HttpStatus.code100Continue,
HttpStatusCode.switchingProtocols: HttpStatus.code101SwitchingProtocols,
HttpStatusCode.processing: HttpStatus.code102Processing,
HttpStatusCode.earlyHints: HttpStatus.code103EarlyHints,
// 2xx Success.
HttpStatusCode.ok: HttpStatus.code200Ok,
HttpStatusCode.created: HttpStatus.code201Created,
Expand Down Expand Up @@ -2793,6 +2795,21 @@ class HttpStatus {
HttpStatus.code599NetworkConnectTimeoutError,
};

/// Returns true if this ranges between 100 and 199
bool get isInformationHttpStatusCode => code.isInformationHttpStatusCode;

/// Returns true if code ranges between 200 and 299
bool get isSuccessfulHttpStatusCode => code.isSuccessfulHttpStatusCode;

/// Returns true if this ranges between 300 and 399
bool get isRedirectHttpStatusCode => code.isRedirectHttpStatusCode;

/// Returns true if this ranges between 400 and 499
bool get isClientErrorHttpStatusCode => code.isClientErrorHttpStatusCode;

/// Returns true if code ranges between 500 and 599
bool get isServerErrorHttpStatusCode => code.isServerErrorHttpStatusCode;

@override
int get hashCode => _equality().hashCode;

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: http_status
description: Constants enumerating the HTTP status codes in Dart. All status codes defined in RFC1945 (HTTP/1.0, RFC2616 (HTTP/1.1), and RFC2518 (WebDAV) are supported.
version: 3.0.0
version: 3.1.0
repository: https://github.com/DartForge/http_status
issue_tracker: https://github.com/DartForge/http_status/issues
homepage: https://github.com/DartForge/http_status
Expand Down
Loading

0 comments on commit 0a4fe1e

Please sign in to comment.