Skip to content

Commit

Permalink
Merge pull request #66 from ArunPrakashG/feature/response-refactors
Browse files Browse the repository at this point in the history
[REFACTORS] Model class refactors
  • Loading branch information
ArunPrakashG authored May 7, 2024
2 parents 455d26c + f788609 commit 366a707
Show file tree
Hide file tree
Showing 29 changed files with 316 additions and 321 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,12 @@

- 🩹 Bug fixes

## 🐛 8.4.6

- 🩹 Bug fixes
- 🛠 Added validations for entered `baseUrl`.
- 💥Renamed `executeGuarded` to `guardAsync` and added a new `guard` method to guard synchronous functions.

## Legend

- 🎉 New features or major changes
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Add `wordpress_client` in your `pubspec.yaml`:

```dart
dependencies:
wordpress_client: ^8.4.5
wordpress_client: ^8.4.6
```

> 💡 Ensure you get the [latest version here](https://pub.dev/packages/wordpress_client).
Expand Down
23 changes: 16 additions & 7 deletions lib/src/internal_requester.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ final class InternalRequester extends IRequestExecutor {
Future<WordpressRawResponse> execute(WordpressRequest request) async {
final requestHeaders = request.headers ?? <String, dynamic>{};

// If the request requires authorization, we need to check if the default authorization is set.
// If it is, we need to check if the default authorization is valid.
if (request.requireAuth) {
final authorizer = () {
if (request.authorization != null &&
Expand Down Expand Up @@ -134,6 +136,7 @@ final class InternalRequester extends IRequestExecutor {
requestHeaders[authResult.key] = authResult.value;
}

// Generate the request URL.
final requestUrl = () {
if (request.url.isAbsolute) {
return request.url.uri.replace(host: baseUrl.host).toString();
Expand All @@ -144,7 +147,10 @@ final class InternalRequester extends IRequestExecutor {

_triggerStatistics(requestUrl);

final result = await executeGuarded(
// Trigger the onExecute middleware event and returns the result.
// If the result is not null, we return the result.
// This is used if the user already has some sort of custom cache implementation.
final middlewareResult = await guardAsync(
function: () async {
return _executeMiddlewareEvent(
request: request,
Expand Down Expand Up @@ -177,18 +183,19 @@ final class InternalRequester extends IRequestExecutor {
},
);

if (result != null &&
result.code != -RequestErrorType.middlewareExecutionFailed.index) {
return result;
if (middlewareResult != null &&
middlewareResult.code !=
-RequestErrorType.middlewareExecutionFailed.index) {
return middlewareResult;
}

final watch = Stopwatch();

Future<Response<dynamic>> run() async {
Future<Response<T>> run<T>() async {
watch.start();

try {
return _client.request<dynamic>(
return _client.request<T>(
requestUrl,
data: request.body,
cancelToken: request.cancelToken,
Expand All @@ -204,12 +211,14 @@ final class InternalRequester extends IRequestExecutor {
headers: requestHeaders,
),
);
} catch (e) {
rethrow;
} finally {
watch.stop();
}
}

final response = await run();
final response = await run<dynamic>();

final statusCode =
response.statusCode ?? -RequestErrorType.invalidStatusCode.index;
Expand Down
10 changes: 5 additions & 5 deletions lib/src/internal_requester_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ abstract base class IRequestExecutor {
Future<WordpressResponse<T>> create<T>(
WordpressRequest request,
) async {
final rawResponse = await executeGuarded<WordpressRawResponse>(
final rawResponse = await guardAsync<WordpressRawResponse>(
function: () async {
request = await _handleRequestMiddlewares(
request: request,
Expand Down Expand Up @@ -109,7 +109,7 @@ abstract base class IRequestExecutor {
Future<WordpressResponse<T>> retrive<T>(
WordpressRequest request,
) async {
final rawResponse = await executeGuarded<WordpressRawResponse>(
final rawResponse = await guardAsync<WordpressRawResponse>(
function: () async {
request = await _handleRequestMiddlewares(
request: request,
Expand Down Expand Up @@ -181,7 +181,7 @@ abstract base class IRequestExecutor {
Future<WordpressResponse<bool>> delete(
WordpressRequest request,
) async {
final rawResponse = await executeGuarded<WordpressRawResponse>(
final rawResponse = await guardAsync<WordpressRawResponse>(
function: () async {
request = await _handleRequestMiddlewares(
request: request,
Expand Down Expand Up @@ -250,7 +250,7 @@ abstract base class IRequestExecutor {
Future<WordpressResponse<List<T>>> list<T>(
WordpressRequest request,
) async {
final rawResponse = await executeGuarded<WordpressRawResponse>(
final rawResponse = await guardAsync<WordpressRawResponse>(
function: () async {
request = await _handleRequestMiddlewares(
request: request,
Expand Down Expand Up @@ -322,7 +322,7 @@ abstract base class IRequestExecutor {
Future<WordpressResponse<T>> update<T>(
WordpressRequest request,
) async {
final rawResponse = await executeGuarded<WordpressRawResponse>(
final rawResponse = await guardAsync<WordpressRawResponse>(
function: () async {
request = await _handleRequestMiddlewares(
request: request,
Expand Down
2 changes: 1 addition & 1 deletion lib/src/parallel_wordpress/parallel_wordpress.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ final class ParallelWordpress {
);

final results = await responses.mapAsync(
(response) async => executeGuarded(
(response) async => guardAsync(
function: () async {
if (transformer != null) {
return await transformer(response.results);
Expand Down
4 changes: 4 additions & 0 deletions lib/src/requests/request_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ abstract base class IRequest {
return headers![key] ?? '';
}

if (extra != null) {
return extra![key] as String? ?? '';
}

return '';
}
}
36 changes: 11 additions & 25 deletions lib/src/responses/application_password_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'package:meta/meta.dart';

import '../library_exports.dart';
import '../utilities/self_representive_base.dart';
import 'properties/links.dart';

@immutable
final class ApplicationPassword implements ISelfRespresentive {
Expand All @@ -11,43 +10,31 @@ final class ApplicationPassword implements ISelfRespresentive {
required this.appId,
required this.name,
required this.created,
required this.links,
required this.self,
this.lastUsed,
this.lastIp,
this.password,
});

factory ApplicationPassword.fromJson(Map<String, dynamic> map) {
factory ApplicationPassword.fromJson(Map<String, dynamic> json) {
return ApplicationPassword(
uuid: castOrElse(map['uuid']),
appId: castOrElse(map['app_id']),
name: castOrElse(map['name']),
created: castOrElse(
map['created'],
transformer: (value) => DateTime.parse(value as String),
)!,
lastUsed: castOrElse(
map['last_used'],
transformer: (value) => DateTime.parse(value as String),
),
lastIp: castOrElse(map['last_ip']),
password: castOrElse(map['password']),
links: castOrElse(
map['_links'],
transformer: (value) => Links.fromJson(value as Map<String, dynamic>),
),
self: map,
uuid: castOrElse(json['uuid']),
appId: castOrElse(json['app_id']),
name: castOrElse(json['name']),
created: parseDateIfNotNull(castOrElse(json['created'])),
lastUsed: parseDateIfNotNull(castOrElse(json['last_used'])),
lastIp: castOrElse(json['last_ip']),
password: castOrElse(json['password']),
self: json,
);
}

final String uuid;
final String? appId;
final String name;
final DateTime created;
final DateTime? created;
final DateTime? lastUsed;
final String? lastIp;
final Links? links;
final String? password;

@override
Expand All @@ -58,11 +45,10 @@ final class ApplicationPassword implements ISelfRespresentive {
'uuid': uuid,
'app_id': appId,
'name': name,
'created': created.toIso8601String(),
'created': created?.toIso8601String(),
'last_used': lastUsed?.toIso8601String(),
'last_ip': lastIp,
'password': password,
'_links': links?.toJson(),
};
}
}
38 changes: 11 additions & 27 deletions lib/src/responses/category_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'package:meta/meta.dart';

import '../utilities/helpers.dart';
import '../utilities/self_representive_base.dart';
import 'properties/links.dart';

@immutable
class Category implements ISelfRespresentive {
Expand All @@ -17,39 +16,30 @@ class Category implements ISelfRespresentive {
required this.parent,
required this.self,
this.name,
this.meta,
this.links,
});

factory Category.fromJson(dynamic json) {
return Category(
id: castOrElse(json['id']),
count: castOrElse(json['count'], orElse: () => 0)!,
description: castOrElse(json['description'], orElse: () => '')!,
link: castOrElse(json['link'], orElse: () => '')!,
name: castOrElse(json['name'], orElse: () => ''),
slug: castOrElse(json['slug'], orElse: () => '')!,
taxonomy: castOrElse(json['taxonomy'], orElse: () => '')!,
parent: castOrElse(json['parent'], orElse: () => 0)!,
meta: json['meta'],
links: castOrElse(
json['_links'],
transformer: (value) => Links.fromJson(value as Map<String, dynamic>),
),
description: castOrElse(json['description']),
link: castOrElse(json['link']),
name: castOrElse(json['name']),
slug: castOrElse(json['slug']),
taxonomy: castOrElse(json['taxonomy']),
parent: castOrElse(json['parent']),
self: json as Map<String, dynamic>,
);
}

final int id;
final int count;
final String description;
final String link;
final String? description;
final String? link;
final String? name;
final String slug;
final String taxonomy;
final int parent;
final dynamic meta;
final Links? links;
final String? taxonomy;
final int? parent;

@override
final Map<String, dynamic> self;
Expand All @@ -64,8 +54,6 @@ class Category implements ISelfRespresentive {
'slug': slug,
'taxonomy': taxonomy,
'parent': parent,
'meta': meta,
'_links': links?.toJson(),
};
}

Expand All @@ -85,8 +73,6 @@ class Category implements ISelfRespresentive {
other.slug == slug &&
other.taxonomy == taxonomy &&
other.parent == parent &&
other.meta == meta &&
other.links == links &&
mapEquals(other.self, self);
}

Expand All @@ -100,13 +86,11 @@ class Category implements ISelfRespresentive {
slug.hashCode ^
taxonomy.hashCode ^
parent.hashCode ^
meta.hashCode ^
links.hashCode ^
self.hashCode;
}

@override
String toString() {
return 'Category(id: $id, count: $count, description: $description, link: $link, name: $name, slug: $slug, taxonomy: $taxonomy, parent: $parent, meta: $meta, links: $links, self: $self)';
return 'Category(id: $id, count: $count, description: $description, link: $link, name: $name, slug: $slug, taxonomy: $taxonomy, parent: $parent, self: $self)';
}
}
Loading

0 comments on commit 366a707

Please sign in to comment.