Skip to content

Commit

Permalink
feat: fixed for the issue (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
myConsciousness committed Feb 6, 2023
1 parent c2b8e49 commit 82432b2
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 0 deletions.
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

0 comments on commit 82432b2

Please sign in to comment.