diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a9b6e8..cc0496c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,332 +1,299 @@ -## πŸŽ‰ 4.0.0 +## 8.5.0 -- πŸš€ Initial version, created by Stagehand +- πŸŽ‰ Added support for initialize the client without a base url. + - Use the `WordpressClient.generic` constructor to initialize the client without a base url. + - Use the `reconfigure` method to set the base url after initializing the client. + - Failure to set the base url will throw an exception when making requests. +- πŸŽ‰ Added `isAuthenticated` method to check if the current instance has a valid authentication. + - Optionally, pass an instance of `IAuthorization` to check if the client is authenticated with the given authorization. +- πŸ’₯ Deprecated `reconfigureClient` method in favour of `reconfigure` method. -## πŸ› 5.0.4 +## 8.4.10 -- πŸ”’ Fixed Authorization Bugs +- 🩹 Fix bug on clearing middleware list -## πŸ’₯ 5.1.0 +## 8.4.9 -- πŸ”„ BREAKING CHANGE: Authorization API Change. +- πŸ“¦ Downgrade meta package to match flutter meta version -## πŸ›  5.1.1 +## 8.4.8 -- πŸ“ Formatting changes. +- 🩹 Bug fixes +- πŸ’₯ Deprecated `WordpressClient.initialize(...)` ctor and `initialize()` method +- πŸŽ‰ Added `WordpressClient.fromDioInstance(...)` constructor -## πŸ’₯ 5.2.3 +## 8.4.7 -- πŸ”„ BREAKING CHANGE: Request API Change. +- 🩹 Bug fixes +- πŸ›  Fix validations for entered `baseUrl`; Supporting sites with custom REST Api paths -## πŸ›  5.2.4 +## 8.4.6 -- πŸ—‘οΈ Remove unused package +- 🩹 Bug fixes +- πŸ›  Added validations for entered `baseUrl` +- πŸ’₯ Renamed `executeGuarded` to `guardAsync` and added `guard` method -## πŸ’₯ 5.2.5 +## 8.4.5 -- πŸ”„ BREAKING CHANGE: Remove Author Meta & Featured Image Url Fields from Post response. +- 🩹 Bug fixes -## πŸ§ͺ 5.2.6 +## 8.4.4 -- βš™οΈ Experimental Request Caching system +- 🩹 Bug fixes -## πŸ› 5.2.7 +## 8.4.3 -- 🩹 Bug fix +- πŸŽ‰ New static method to check if a site is built using WordPress -## πŸ›  5.2.8 +## 8.4.2 -- πŸ”„ Revert author meta & featured image removal +- 🩹 Bug fixes +- πŸŽ‰ Added static methods to validate base URL and discover a website -## πŸ› 5.2.9 +## 8.4.1 -- 🩹 Bug Fix +- πŸŽ‰ Introduce `ParallelWordpress` class to generate and execute parallel requests +- πŸ›  Bug fixes -## πŸ› 5.3.0 +## 8.4.0 -- 🩹 Bug fix +- πŸŽ‰ Added support for Middlewares +- πŸ’₯ Removed dependency on `synchronised` package -## πŸ›  5.3.1 +## 8.3.10 -- πŸ“œ Response structure fix +- 🩹 Bug fixes -## πŸ›  5.4.0 +## 8.3.9 -- πŸ“¦ Packages update +- 🩹 Bug fixes +- πŸŽ‰ Iterate over the raw response of the endpoint using [] operator -## πŸ›  5.4.1 +## 8.3.8 -- πŸ”’ Null safety fix +- πŸ› Renamed retrive -> retrieve. Fix typo -## πŸ›  5.4.2 +## 8.3.7 -- πŸ“¦ Packages fix +- πŸŽ‰ Support for raw requests +- πŸ›  Bug fixes -## πŸ›  5.4.3 +## 8.3.6 -- πŸ“œ Total pages parsing fix +- 🩹 Bug fixes on enum parsing -## πŸŽ‰ 6.1.0-pre +## 8.3.5 -- πŸš€ Entire API changed. (Please bear with us!) -- 🌊 Fluency is maintained by using Dart's cascading operator. -- ⚑ Performance and memory consumption improvements. +- 🩹 Bug fixes on comment list request -## πŸ›  6.1.1-pre +## 8.3.4 -- πŸ—‘οΈ Removed test package +- πŸ› Media response model null exception when parsing if media details is empty -## πŸ›  6.1.2-pre +## 8.3.3 -- πŸ“Œ Version fix +- Packages downgrade + - collection 1.18.0 -> 1.17.2 -## πŸ› 6.1.3-pre & 6.1.4-pre +## 8.3.2 -- 🩹 Bug fixes +- Packages downgrade + - meta 1.11.0 -> 1.9.1 -## πŸŽ‰ 6.1.5-pre +## 8.3.1 -- βž• Added Post extension for Media and Author +- Packages downgrade + - test 1.24.8 -> 1.24.5 +- Added avatar URLs model class for users response -## πŸ›  6.1.6-pre +## 8.3.0 -- πŸ”— Support 3xx series responses (Cached Response) +- πŸŽ‰ Supports Application Password endpoint +- Packages update -## πŸ›  6.1.7-pre to 6.1.9-pre +## 8.2.2 -- πŸ”§ Refactoring & 🩹 Bug fixes +- πŸ›  Bug fixes -## πŸŽ‰ 6.2.0-pre +## 8.2.1 -- πŸ”§ Refactoring, πŸ”„ Request Synchronization, and πŸ› Debug Mode +- πŸ›  Fixed exporting WordPressDiscovery class -## πŸ›  6.2.1-pre +## 8.2.0 -- πŸ§ͺ Misc changes +- πŸŽ‰ Added support for Pages endpoint -## πŸŽ‰ 6.3.0 +## 8.1.0 -- πŸš€ Major changes in the API. (Please check our wiki for setup and usage) +- πŸŽ‰ Added ability to fetch and cache the discovery endpoint of WordPress REST API -## πŸŽ‰ 6.3.1 +## 8.0.11 -- πŸ” Implemented search endpoint +- βž• Added `extra` property to all request classes +- βž• Added `addAllIfNotNull(...)` extension method -## πŸ› 6.3.4 +## 8.0.10 -- 🩹 Bug fix with media endpoint +- πŸ›  `featured_media_src_url` key now decodes as expected +- βž• Added `decodeByMultiKeys` method -## πŸŽ‰ 7.0.0 +## 8.0.9 -- πŸ’₯ Major changes (Please check our documentation for more details) +- βž• Added App Password support -## πŸŽ‰ 8.0.0 +## 8.0.8 -- πŸ’₯ Several major updates and changes. (Please refer to our wiki for setup and usage) +- πŸ”§ Integrated new lint rules and code refactors -## πŸ“– 8.0.1 +## 8.0.7 -- πŸ“š Docs update +- πŸ› Bug fixes +- βž• Introduced `RequestErrorType` for failure responses +- βž• Introduced `mapGuarded(...)` and `executeGuarded(...)` methods +- πŸ”§ Usual refactors and improvements -## πŸ›  8.0.2 +## 8.0.6 -- πŸ”½ Downgrade collection version +- πŸ“š Docs update -## πŸŽ‰ 8.0.3 +## 8.0.5 -- πŸ“€ Export response extensions +- 🩹 Bug fixes and improvements -## πŸ› 8.0.4 & 8.0.5 +## 8.0.4 - 🩹 Bug fixes and improvements -## πŸ“– 8.0.6 +## 8.0.3 -- πŸ“š Docs update +- πŸ“€ Export response extensions -## πŸ“– 8.0.7 +## 8.0.2 -- πŸ› Bug fixes -- βž• Introduced `RequestErrorType` for failure responses and optional `exception` & `stackTrace` properties -- βž• Introduced `mapGuarded(...)` method to safely map a list of objects -- βž• Introduced `executeGuarded(...)` method to safely execute a function -- πŸ”§ Usual amounts of refactors and improvements +- πŸ”½ Downgrade collection version -## πŸ“– 8.0.8 +## 8.0.1 -- πŸ”§ Integrated new lint rules and code refactors +- πŸ“š Docs update -## πŸ“– 8.0.9 +## 8.0.0 -- βž• Added App Password support +- πŸ’₯ Several major updates and changes -## πŸ“– 8.0.10 +## 7.0.0 -- πŸ›  `featured_media_src_url` key now decodes as expected -- βž• Added a new `decodeByMultiKeys` method to decode a value from JSON by matching aganist multiple keys. Any of the key matchs, the value is returned +- πŸ’₯ Major changes (Please check our documentation for more details) -## πŸ“– 8.0.11 +## 6.3.1 -- βž• Added `extra` property to all request classes. Now you can append (or override) custom fields into the request. -- βž• Added `addAllIfNotNull(...)` extension method to append valid key-values (not null) to existing map from another map. +- πŸ” Implemented search endpoint -## πŸŽ‰ 8.1.0 +## 6.3.0 -- πŸŽ‰ Added the ability to fetch the discovery endpoint of Wordpress REST API and cache the response. - - This allows to access the site's identity details such as description and logo url, along with all the available endpoints and other details. +- πŸš€ Major changes in the API -## πŸŽ‰ 8.2.0 +## 6.2.1-pre -- πŸŽ‰ Added support for Pages endpoint +- πŸ§ͺ Misc changes -## πŸ›  8.2.1 +## 6.2.0-pre -- πŸ›  Fixed exporting WordpressDiscovery class. +- πŸ”§ Refactoring, Request Synchronization, and Debug Mode -## πŸ›  8.2.2 +## 6.1.7-pre to 6.1.9-pre -- πŸ›  Bug fixes +- πŸ”§ Refactoring & Bug fixes -## πŸŽ‰ 8.3.0 +## 6.1.6-pre -- πŸŽ‰ Supports Application Password endpoint -- Packages update - - dio 5.3.2 -> 5.3.3 - - collection 1.17.1 -> 1.18.0 - - meta 1.9.1 -> 1.11.0 - - test 1.24.5 -> 1.24.8 - -## πŸ› 8.3.1 +- πŸ”— Support 3xx series responses (Cached Response) -- Packages downgrade - - test 1.24.8 -> 1.24.5 -- Added avatar urls model class for users response +## 6.1.5-pre -## πŸ› 8.3.2 +- βž• Added Post extension for Media and Author -- Packages downgrade +## 6.1.3-pre & 6.1.4-pre - - meta 1.11.0 -> 1.9.1 +- 🩹 Bug fixes -## πŸ› 8.3.3 +## 6.1.2-pre -- Packages downgrade - - collection 1.18.0 -> 1.17.2 +- πŸ“Œ Version fix -## πŸ› 8.3.4 +## 6.1.1-pre -- Media response model null exception when parsing if media details is empty. (while using embed=true) +- πŸ—‘οΈ Removed test package -## πŸ› 8.3.5 +## 6.1.0-pre -- 🩹 Bug fixes on comment list request. +- πŸš€ Entire API changed +- 🌊 Fluency maintained using Dart's cascading operator +- ⚑ Performance and memory consumption improvements -## πŸ› 8.3.6 +## 5.4.3 -- 🩹 Bug fixes on enum parsing. +- πŸ“œ Total pages parsing fix -## πŸ› 8.3.7 +## 5.4.2 -- πŸŽ‰ Support for raw requests - - Allows you to send requests but skip the parsing process. You can parse the response manually. - - This allows to integrate the package along with other database packages such as drift, hive etc by allowing you to create a custom model class and integrate for the response. -- πŸ›  Bug fixes +- πŸ“¦ Packages fix -## πŸ› 8.3.8 +## 5.4.1 -- πŸ› Renamed retrive -> retrieve. Fix the typo +- πŸ”’ Null safety fix -## πŸ› 8.3.9 +## 5.4.0 -- 🩹 Bug fixes -- πŸŽ‰ Iterate over the raw response of the endpoint from the final success or failure response model using [] operator. +- πŸ“¦ Packages update -## πŸ› 8.3.10 +## 5.3.1 -- 🩹 Bug fixes +- πŸ“œ Response structure fix -## πŸŽ‰ 8.4.0 +## 5.3.0 -- πŸŽ‰ Added support for Middlewares! - - You can now create custom middlewares to intercept the request and response. - - The middleware can be used to modify the request, response or abort the process entirely by directly throwing an exception in their processing block. - - You can create multiple middlewares and add them to the client. - - The middlewares are executed in the order they are added to the client. - - Added `DelegatedMiddleware` if you do not wish to create a separate class for the middleware but just need to modify the request or response. -- πŸ’₯ Removed dependency on `synchronised` package. - - This package was used to synchronise the request and response processing. This now needs to be handled by the user itself. - - it didn't make sense to restrict the requesting processing inside the library itself. +- 🩹 Bug fix -## πŸ› 8.4.1 +## 5.2.9 -- πŸŽ‰ Introduce `ParallelWordpress` class to generate requests and execute them in parallel, at the same time receive responses sorted using their page number. -- πŸ›  Bug fixes +- 🩹 Bug Fix -## πŸ› 8.4.2 +## 5.2.8 -- 🩹 Bug fixes -- πŸŽ‰ Added static methods to validate base url and to discovery a website. +- πŸ”„ Revert author meta & featured image removal -## πŸ› 8.4.3 +## 5.2.7 -- πŸŽ‰ New static method to check if a site is built using wordpress. +- 🩹 Bug fix -## πŸ› 8.4.4 +## 5.2.6 -- 🩹 Bug fixes +- βš™οΈ Experimental Request Caching system -## πŸ› 8.4.5 +## 5.2.5 -- 🩹 Bug fixes +- πŸ”„ BREAKING CHANGE: Remove Author Meta & Featured Image URL Fields from Post response -## πŸ› 8.4.6 +## 5.2.4 -- 🩹 Bug fixes -- πŸ›  Added validations for entered `baseUrl`. -- πŸ’₯Renamed `executeGuarded` to `guardAsync` and added a new `guard` method to guard synchronous functions. +- πŸ—‘οΈ Remove unused package -## πŸ› 8.4.7 +## 5.2.3 -- 🩹 Bug fixes -- πŸ›  Fix validations for entered `baseUrl`; Supporting sites with custom REST Api paths +- πŸ”„ BREAKING CHANGE: Request API Change -## πŸ› 8.4.8 +## 5.1.1 -- 🩹 Bug fixes -- πŸ’₯ Deprecated `WordpressClient.initialize(...)` ctor and `initialize()` method. - - Use `WordpressClient(baseUrl: 'https://example.com')` instead. This will automatically initialize the client. -- πŸŽ‰ Added `WordpressClient.fromDioInstance(...)` constructor to allow developers to pass a custom `Dio` instance to be used inside the client. - - Note that, the `BaseOptions` in this custom dio instance will be overwritten by the client's default options and those which are required by the libraries requests. +- πŸ“ Formatting changes -## πŸ› 8.4.9 +## 5.1.0 -- πŸ“¦ Downgrade meta package to match flutter meta version +- πŸ”„ BREAKING CHANGE: Authorization API Change -## πŸ› 8.4.10 +## 5.0.4 -- 🩹 Fix bug on clearing middleware list +- πŸ”’ Fixed Authorization Bugs -## Legend +## 4.0.0 -- πŸŽ‰ New features or major changes -- πŸ› Bug fixes -- πŸ›  Minor fixes or tweaks -- πŸ’₯ Breaking changes -- πŸ”„ Changes that revert or modify previous functionality -- πŸ“¦ Package updates -- πŸ”’ Security updates -- πŸ“ Documentation or formatting updates -- πŸ§ͺ Experimental features -- βš™οΈ System or backend changes -- πŸ—‘οΈ Removals -- πŸ”§ Refactoring -- πŸ“š Documentation updates -- πŸ” New search features or updates -- πŸ“€ Exporting features or updates -- πŸ”½ Downgrades or rollbacks -- πŸ“Œ Versioning fixes or updates -- 🌊 Flow or fluency updates -- ⚑ Performance improvements -- πŸ“œ Parsing or data structure changes -- βž• Additions of new functionalities -- πŸ”— Linking or response changes +- πŸš€ Initial version, created by Stagehand diff --git a/lib/src/exceptions/client_not_ready_exception.dart b/lib/src/exceptions/client_not_ready_exception.dart index 45b0a37..3f8f374 100644 --- a/lib/src/exceptions/client_not_ready_exception.dart +++ b/lib/src/exceptions/client_not_ready_exception.dart @@ -7,5 +7,5 @@ class ClientNotReadyException implements WordpressException { @override String? get message => - 'Client has not been initialized. You might have forgotten to call `WordpressClient.initialize()` method or you please use the factory constructor `WordpressClient.initialize()` to initialize client with the constructor.'; + 'Client has not been initialized. This could happen due to the Base URL not being set. If you had created the WordpressClient instance using `generic` constructor, please use `reconfigure` method to pass a Base URL.'; } diff --git a/lib/src/exceptions/wordpress_exception_base.dart b/lib/src/exceptions/wordpress_exception_base.dart index d3755d4..bda69a4 100644 --- a/lib/src/exceptions/wordpress_exception_base.dart +++ b/lib/src/exceptions/wordpress_exception_base.dart @@ -7,5 +7,5 @@ abstract class WordpressException implements Exception { final ErrorType errorType; @override - String toString() => '[${DateTime.now()}] ${errorType}Exception: $message'; + String toString() => '[${DateTime.now()}] $errorType $message'; } diff --git a/lib/src/internal_requester.dart b/lib/src/internal_requester.dart index 7fed97b..1cc9cf7 100644 --- a/lib/src/internal_requester.dart +++ b/lib/src/internal_requester.dart @@ -14,13 +14,12 @@ final class InternalRequester extends IRequestExecutor { } final Dio _client; - final Uri _baseUrl; + Uri? _baseUrl; IAuthorization? _defaultAuthorization; static final Map _endPointStatistics = {}; static StatisticsCallback? _statisticsCallback; bool _isDebugMode = false; final BootstrapConfiguration _configuration; - final List _middlewares = []; /// The request base url. /// @@ -28,13 +27,50 @@ final class InternalRequester extends IRequestExecutor { /// /// Basically, Base URL + Path @override - Uri get baseUrl => _baseUrl; + Uri get baseUrl { + if (_baseUrl == null) { + throw StateError( + 'Base URL is not set. Did you forget to call `initialize` method?', + ); + } + + return _baseUrl!; + } + + bool get hasBaseURL => _baseUrl != null; bool get hasDefaultAuthorization => _defaultAuthorization != null && !_defaultAuthorization!.isDefault; @override - List get middlewares => _middlewares; + List get middlewares => + _configuration.middlewares ?? []; + + void _setBaseUrl(Uri url) { + if (!url.isAbsolute) { + throw ArgumentError( + 'The provided url is relative. Base URLs should always be an absolute URL.', + 'baseUrl', + ); + } + + if (!isValidPortNumber(url.port)) { + throw ArgumentError( + 'The provided port number is invalid. Port numbers should be between 1 and 65535.', + 'baseUrl', + ); + } + + if (!isValidRestApiUrl(url)) { + throw ArgumentError( + 'The provided url is invalid. The REST API path should be appended to the base URL.', + 'baseUrl', + ); + } + + _baseUrl = url; + _client.options.baseUrl = url.toString(); + } @override void configure(BootstrapConfiguration configuration) { @@ -64,11 +100,9 @@ final class InternalRequester extends IRequestExecutor { _client.options.receiveTimeout = configuration.receiveTimeout; _client.options.followRedirects = configuration.shouldFollowRedirects; _client.options.maxRedirects = configuration.maxRedirects; - _client.options.baseUrl = _baseUrl.toString(); - if (configuration.middlewares != null && - configuration.middlewares!.isNotEmpty) { - _middlewares.addAll(configuration.middlewares!); + if (_baseUrl != null) { + _client.options.baseUrl = baseUrl.toString(); } if (configuration.enableDebugMode && @@ -99,6 +133,12 @@ final class InternalRequester extends IRequestExecutor { @override Future execute(WordpressRequest request) async { + if (_client.options.baseUrl.isEmpty) { + throw StateError( + 'Base URL is not set. Did you forget to call `reconfigure` method?', + ); + } + final requestHeaders = request.headers ?? {}; // If the request requires authorization, we need to check if the default authorization is set. @@ -240,7 +280,7 @@ final class InternalRequester extends IRequestExecutor { required WordpressRequest request, required MiddlwareResponseTransformer transformer, }) async { - for (final middleware in _middlewares) { + for (final middleware in middlewares) { final result = await middleware.onExecute(request); if (result.hasData) { @@ -306,7 +346,7 @@ final class InternalRequester extends IRequestExecutor { auth.clientFactoryProvider(_createAuthDioClient()); - await auth.initialize(baseUrl: _baseUrl); + await auth.initialize(baseUrl: baseUrl); if (await auth.authorize() && await auth.isAuthenticated()) { final authUrl = await auth.generateAuthUrl(); diff --git a/lib/src/utilities/extensions/client_extensions.dart b/lib/src/utilities/extensions/client_extensions.dart index 6fea78c..041948e 100644 --- a/lib/src/utilities/extensions/client_extensions.dart +++ b/lib/src/utilities/extensions/client_extensions.dart @@ -25,4 +25,9 @@ extension WordpressClientExtensions on WordpressClient { /// /// Optionally, call `merge()` on the `responses` to merge the results into a single iterable. ParallelWordpress get parallel => ParallelWordpress(client: this); + + Future isAuthenticated([IAuthorization? auth]) async { + final response = await me.retrieve(RetrieveMeRequest(authorization: auth)); + return response.isSuccessful; + } } diff --git a/lib/src/wordpress_client_base.dart b/lib/src/wordpress_client_base.dart index 85c911d..00c7935 100644 --- a/lib/src/wordpress_client_base.dart +++ b/lib/src/wordpress_client_base.dart @@ -65,15 +65,52 @@ final class WordpressClient implements IDisposable { configuration = bootstrapper(BootstrapBuilder()); } + final typeMap = CodableMap(); + final interfaces = , dynamic>{}; + final client = Dio(); + final middlewares = configuration.middlewares ?? []; + return WordpressClient._( InternalRequester.configure( baseUrl, - Dio(), + client, + configuration, + ), + typeMap, + interfaces, + middlewares, + ); + } + + /// Special constructor allows you to initialize the client without a base URL. + /// + /// Note that, for any operations which require access to the Base URL, it will throw an exception in this case. + /// + /// To update the client with a Base URL, you will have to call `reconfigure` method. + factory WordpressClient.generic({ + Dio? instance, + BootstrapConfiguration Function(BootstrapBuilder builder)? bootstrapper, + }) { + var configuration = const BootstrapConfiguration(); + + if (bootstrapper != null) { + configuration = bootstrapper(BootstrapBuilder()); + } + + final typeMap = CodableMap(); + final interfaces = , dynamic>{}; + final client = instance ?? Dio(); + final middlewares = configuration.middlewares ?? []; + + return WordpressClient._( + InternalRequester.configure( + null, + client, configuration, ), - CodableMap(), - {}, - configuration.middlewares ?? [], + typeMap, + interfaces, + middlewares, ); } @@ -118,15 +155,19 @@ final class WordpressClient implements IDisposable { configuration = bootstrapper(BootstrapBuilder()); } + final typeMap = CodableMap(); + final interfaces = , dynamic>{}; + final middlewares = configuration.middlewares ?? []; + return WordpressClient._( InternalRequester.configure( baseUrl, instance, configuration, ), - CodableMap(), - {}, - configuration.middlewares ?? [], + typeMap, + interfaces, + middlewares, ); } @@ -148,9 +189,15 @@ final class WordpressClient implements IDisposable { } final InternalRequester _requester; + final CodableMap _typeMap; + bool _isDisposed = false; + final Map, dynamic> _interfaces; + final List _middlewares; + WordpressDiscovery? _discovery; + bool _hasInitialized = false; - /// Base url supplied through constructor. - Uri get baseUrl => _requester._baseUrl; + /// Base url of this instance. + Uri get baseUrl => _requester.baseUrl; /// Base url path. String get path => baseUrl.path; @@ -164,18 +211,16 @@ final class WordpressClient implements IDisposable { bool get isDebugMode => _requester._isDebugMode; /// Returns true if we have valid default authorization which is to be used for all requests. - bool get hasValidDefaultAuthorization => - _requester._defaultAuthorization != null && - _requester._defaultAuthorization!.isValidAuth; + bool get hasValidDefaultAuthorization { + if (_requester._defaultAuthorization == null) { + return false; + } - /// Stores data on how to decode & encode responses. - final CodableMap _typeMap; - bool _isDisposed = false; + return _requester._defaultAuthorization!.isValidAuth; + } - final Map, dynamic> _interfaces; - final List _middlewares; - WordpressDiscovery? _discovery; - bool _hasInitialized = false; + /// Returns true if the discovery process has been completed. + bool get discoveryCompleted => _discovery != null; /// The current user interface. /// @@ -305,7 +350,7 @@ final class WordpressClient implements IDisposable { /// Status on if the client has been initialized successfully. /// /// This will be true if [_initialize] method has been called and completed. - bool get isReady => _hasInitialized; + bool get isReady => _hasInitialized && _requester.hasBaseURL; WordpressDiscovery get discovery { if (_discovery == null) { @@ -569,8 +614,8 @@ final class WordpressClient implements IDisposable { _middlewares.add(middleware); - reconfigureClient( - (builder) => builder.withMiddlewares(_middlewares).build(), + reconfigure( + bootstrapper: (builder) => builder.withMiddlewares(_middlewares).build(), ); } @@ -585,8 +630,8 @@ final class WordpressClient implements IDisposable { unawaited(middleware.onUnload()); _middlewares.remove(middleware); - reconfigureClient( - (builder) => builder.withMiddlewares(_middlewares).build(), + reconfigure( + bootstrapper: (builder) => builder.withMiddlewares(_middlewares).build(), ); } @@ -598,9 +643,15 @@ final class WordpressClient implements IDisposable { /// The default builder will contain all the previous settings. /// /// Only the settings that have changed since will be updated. + @Deprecated('Prefer `reconfigure` instead.') void reconfigureClient( - BootstrapConfiguration Function(BootstrapBuilder builder) bootstrapper, - ) { + BootstrapConfiguration Function(BootstrapBuilder builder) bootstrapper, { + Uri? baseUri, + }) { + if (baseUri != null) { + _requester._setBaseUrl(baseUri); + } + return _requester.configure( bootstrapper( BootstrapBuilder.fromConfiguration( @@ -610,6 +661,30 @@ final class WordpressClient implements IDisposable { ); } + /// Called to reconfigure the client with new settings. + /// + /// The default builder will contain all the previous settings. + /// + /// Only the settings that have changed since will be updated. + void reconfigure({ + BootstrapConfiguration Function(BootstrapBuilder builder)? bootstrapper, + Uri? baseUri, + }) { + if (baseUri != null) { + _requester._setBaseUrl(baseUri); + } + + if (bootstrapper != null) { + _requester.configure( + bootstrapper( + BootstrapBuilder.fromConfiguration( + _requester._configuration, + ), + ), + ); + } + } + /// Fetches the discovery URL of the associated wordpress site and caches the response and returns the status as a boolean. /// /// The response object may allocate a decent amount of memory, it may be possible you wish to deallocate it. @@ -666,8 +741,8 @@ final class WordpressClient implements IDisposable { _middlewares.clear(); - reconfigureClient( - (builder) => builder.withMiddlewares(_middlewares).build(), + reconfigure( + bootstrapper: (builder) => builder.withMiddlewares(_middlewares).build(), ); } diff --git a/pubspec.yaml b/pubspec.yaml index 9329118..7e5dd4e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: wordpress_client description: A library to interact with the Wordpress REST API. Supports most of the common endpoints and all of the CRUD operations on the endpoints. -version: 8.4.10 +version: 8.5.0 topics: - wordpress - parallel