Skip to content

Commit

Permalink
Merge #132
Browse files Browse the repository at this point in the history
132: feat: fixed for the issue (#127) r=myConsciousness a=myConsciousness

# 1. Description

<!-- Provide a description of what this PR is doing.
If you're modifying existing behavior, describe the existing behavior, how this PR is changing it,
and what motivated the change. If this is a breaking change, specify explicitly which APIs have been
changed. -->

## 1.1. Checklist

<!-- Before you create this PR confirm that it meets all requirements listed below by checking the
relevant checkboxes (`[x]`). This will ensure a smooth and quick review process. -->

- [x] The title of my PR starts with a [Conventional Commit] prefix (`fix:`, `feat:`, `docs:` etc).
- [x] I have read the [Contributor Guide] and followed the process outlined for submitting PRs.
- [x] I have updated/added tests for ALL new/updated/fixed functionality.
- [x] I have updated/added relevant documentation in `docs` and added dartdoc comments with `///`.
- [x] I have updated/added relevant examples in `examples`.

## 1.2. Breaking Change

<!-- Does your PR require users to manually update their apps to accommodate your change?

If the PR is a breaking change this should be indicated with suffix "!"  (for example, `feat!:`, `fix!:`). See [Conventional Commit] for details.
-->

- [ ] Yes, this is a breaking change.
- [x] No, this is _not_ a breaking change.

## 1.3. Related Issues

<!-- Provide a list of issues related to this PR from the [issue database].
Indicate which of these issues are resolved or fixed by this PR, i.e. Fixes #xxxx* !-->

<!-- Links -->

[issue database]: https://github.com/mastodon-dart/mastodon-api/issues
[contributor guide]: https://github.com/mastodon-dart/mastodon-api/blob/main/CONTRIBUTING.md
[style guide]: https://github.com/mastodon-dart/mastodon-api/blob/main/STYLEGUIDE.md
[conventional commit]: https://conventionalcommits.org


Co-authored-by: myConsciousness <contact@shinyakato.dev>
  • Loading branch information
bors[bot] and myConsciousness committed Feb 6, 2023
2 parents c2b8e49 + 8386dcf commit 57381c9
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- `DELETE /api/v1/domain_blocks`
- Added response headers in `MastodonResponse`. ([#128](https://github.com/mastodon-dart/mastodon-api/issues/128))
- Added HTTP status in `MastodonResponse`. ([#126](https://github.com/mastodon-dart/mastodon-api/issues/126))
- Added HTTP request in `MastodonResponse`. ([#127](https://github.com/mastodon-dart/mastodon-api/issues/127))

## v0.4.0

Expand Down
2 changes: 2 additions & 0 deletions lib/mastodon_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export 'package:mastodon_api/src/core/exception/data_not_found_exception.dart';
export 'package:mastodon_api/src/core/exception/mastodon_exception.dart';
export 'package:mastodon_api/src/core/exception/rate_limit_exceeded_exception.dart';
export 'package:mastodon_api/src/core/exception/unauthorized_exception.dart';
export 'package:mastodon_api/src/core/http_method.dart';
export 'package:mastodon_api/src/core/http_status.dart';
export 'package:mastodon_api/src/core/language.dart';
export 'package:mastodon_api/src/core/locale.dart';
Expand Down Expand Up @@ -74,6 +75,7 @@ export 'package:mastodon_api/src/service/entities/trends_link.dart';
export 'package:mastodon_api/src/service/entities/usage_statistics.dart';
export 'package:mastodon_api/src/service/mastodon_v1_service.dart';
export 'package:mastodon_api/src/service/mastodon_v2_service.dart';
export 'package:mastodon_api/src/service/response/mastodon_request.dart';
export 'package:mastodon_api/src/service/response/mastodon_response.dart';
export 'package:mastodon_api/src/service/v1/accounts/account_default_settings_param.dart';
export 'package:mastodon_api/src/service/v1/accounts/account_profile_meta_param.dart';
Expand Down
36 changes: 36 additions & 0 deletions lib/src/core/http_method.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2023 Kato Shinya. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided the conditions.

enum HttpMethod {
/// `GET`
get('GET'),

/// `POST`
post('POST'),

/// `DELETE`
delete('DELETE'),

/// `PUT`
put('PUT'),

/// `PATCH`
patch('PATCH');

/// The value.
final String value;

/// Returns the http method associated with [value].
static HttpMethod valueOf(final String value) {
for (final method in values) {
if (method.value == value) {
return method;
}
}

throw UnsupportedError('Unsupported value [$value].');
}

const HttpMethod(this.value);
}
18 changes: 18 additions & 0 deletions lib/src/service/base_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ import '../core/exception/data_not_found_exception.dart';
import '../core/exception/mastodon_exception.dart';
import '../core/exception/rate_limit_exceeded_exception.dart';
import '../core/exception/unauthorized_exception.dart';
import '../core/http_method.dart';
import '../core/http_status.dart';
import '../core/service_helper.dart';
import '../core/util/json_utils.dart';
import 'entities/empty.dart';
import 'entities/rate_limit.dart';
import 'response/mastodon_request.dart';
import 'response/mastodon_response.dart';

/// The callback function for building data object from response.
Expand Down Expand Up @@ -213,6 +215,10 @@ abstract class BaseService implements _Service {
MastodonResponse(
headers: response.headers,
status: HttpStatus.valueOf(response.statusCode),
request: MastodonRequest(
method: HttpMethod.valueOf(response.request!.method),
url: response.request!.url,
),
rateLimit: RateLimit.fromJson(
rateLimitConverter.convert(response.headers),
),
Expand All @@ -227,6 +233,10 @@ abstract class BaseService implements _Service {
MastodonResponse(
headers: response.headers,
status: HttpStatus.valueOf(response.statusCode),
request: MastodonRequest(
method: HttpMethod.valueOf(response.request!.method),
url: response.request!.url,
),
rateLimit: RateLimit.fromJson(
rateLimitConverter.convert(response.headers),
),
Expand All @@ -245,6 +255,10 @@ abstract class BaseService implements _Service {
return MastodonResponse(
headers: response.headers,
status: HttpStatus.valueOf(response.statusCode),
request: MastodonRequest(
method: HttpMethod.valueOf(response.request!.method),
url: response.request!.url,
),
rateLimit: RateLimit.fromJson(
rateLimitConverter.convert(response.headers),
),
Expand All @@ -263,6 +277,10 @@ abstract class BaseService implements _Service {
return MastodonResponse(
headers: response.headers,
status: HttpStatus.valueOf(response.statusCode),
request: MastodonRequest(
method: HttpMethod.valueOf(response.request!.method),
url: response.request!.url,
),
rateLimit: RateLimit.fromJson(
rateLimitConverter.convert(response.headers),
),
Expand Down
20 changes: 20 additions & 0 deletions lib/src/service/response/mastodon_request.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2023 Kato Shinya. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided the conditions.

// 🌎 Project imports:
import '../../core/http_method.dart';

class MastodonRequest {
/// Returns the new instance of [MastodonRequest].
const MastodonRequest({
required this.method,
required this.url,
});

/// The http method when request has sent.
final HttpMethod method;

/// The request url.
final Uri url;
}
5 changes: 5 additions & 0 deletions lib/src/service/response/mastodon_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
import '../../core/http_status.dart';
import '../entities/empty.dart';
import '../entities/rate_limit.dart';
import 'mastodon_request.dart';

/// The class represents the response from Mastodon API.
class MastodonResponse<D> {
/// Returns the new instance of [MastodonResponse].
const MastodonResponse({
required this.headers,
required this.status,
required this.request,
required this.rateLimit,
required this.data,
});
Expand All @@ -23,6 +25,9 @@ class MastodonResponse<D> {
/// The HTTP status from Mastodon API server.
final HttpStatus status;

/// The request that generated this response.
final MastodonRequest request;

/// The rate limit
final RateLimit rateLimit;

Expand Down
32 changes: 32 additions & 0 deletions test/mocks/client_context_stubs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ MockClientContext buildGetStub(
await File(resourcePath).readAsString(),
statusCode,
headers: {'content-type': 'application/json; charset=utf-8'},
request: Request(
'GET',
Uri(),
),
),
);

Expand Down Expand Up @@ -60,6 +64,10 @@ MockClientContext buildPostStub(
headers: {
'content-type': 'application/json; charset=utf-8',
},
request: Request(
'POST',
Uri(),
),
),
);

Expand Down Expand Up @@ -87,6 +95,10 @@ MockClientContext buildPostMultipartStub(
headers: {
'content-type': 'application/json; charset=utf-8',
},
request: Request(
'POST',
Uri(),
),
),
);

Expand All @@ -110,6 +122,10 @@ MockClientContext buildDeleteStub(
await File(resourcePath).readAsString(),
statusCode,
headers: {'content-type': 'application/json; charset=utf-8'},
request: Request(
'DELETE',
Uri(),
),
),
);

Expand All @@ -134,6 +150,10 @@ MockClientContext buildPutStub(
await File(resourcePath).readAsString(),
statusCode,
headers: {'content-type': 'application/json; charset=utf-8'},
request: Request(
'PUT',
Uri(),
),
),
);

Expand All @@ -159,6 +179,10 @@ MockClientContext buildPatchStub(
await File(resourcePath).readAsString(),
statusCode,
headers: {'content-type': 'application/json; charset=utf-8'},
request: Request(
'PATCH',
Uri(),
),
),
);

Expand All @@ -183,6 +207,10 @@ MockClientContext buildPatchMultipartStub(
await File(resourcePath).readAsString(),
statusCode,
headers: {'content-type': 'application/json; charset=utf-8'},
request: Request(
'PATCH',
Uri(),
),
),
);

Expand Down Expand Up @@ -216,6 +244,10 @@ MockClientContext buildSendStub(
responseStream(),
statusCode,
headers: {'content-type': 'application/json; charset=utf-8'},
request: Request(
'GET',
Uri(),
),
);
},
);
Expand Down
36 changes: 36 additions & 0 deletions test/src/core/http_method_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2023 Kato Shinya. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided the conditions.

// 📦 Package imports:
import 'package:mastodon_api/src/core/http_method.dart';
import 'package:test/test.dart';

void main() {
test('.name', () {
expect(HttpMethod.get.name, 'get');
expect(HttpMethod.post.name, 'post');
expect(HttpMethod.delete.name, 'delete');
expect(HttpMethod.put.name, 'put');
expect(HttpMethod.patch.name, 'patch');
});

test('.value', () {
expect(HttpMethod.get.value, 'GET');
expect(HttpMethod.post.value, 'POST');
expect(HttpMethod.delete.value, 'DELETE');
expect(HttpMethod.put.value, 'PUT');
expect(HttpMethod.patch.value, 'PATCH');
});

group('.valueOf', () {
test('when value is supported', () {
expect(HttpMethod.valueOf('POST'), HttpMethod.post);
});

test('when value is not supported', () {
expect(
() => HttpMethod.valueOf('TEST'), throwsA(isA<UnsupportedError>()));
});
});
}

0 comments on commit 57381c9

Please sign in to comment.