This library is designed to fetch resources in a consistent and modular way. The user can define resources by conforming to protocols that define where and how to get them. These resources can then be retrieved using a generic service type with or without an operation provided by the library. By following this approach it's easy to have testable and modular networking stack.
You can add TABResourceLoader as a remote Swift Package dependency in Xcode 11 or newer.
To use the latest version of TABResourceLoader just add this to your Podfile and run pod update or pod install in Terminal:
pod 'TABResourceLoader'- Retrieving a
Decodableobject - Retrieving a JSON object
- Retrieving an image
- Responding to network activity
- [Using failure model example](Documentation/Using failure models.md)
This library defines/uses 4 concepts: model, resource, service and operation:
- Model: Strongly typed object in your codebase, may or may not be mapped 121 to the server model
- Resource: Defines through protocol conformance where and how to fetch a Model. For example a resource could define the URL of where a JSON file is and how to parse into strongly types model.
- Service: A type that knows how to retrieve a specific kind of Resource
- Operation: Provides a concurrency model when using a Service. Useful when implementing custom business logic such as throttling of fetches.
ResourceType: Defines a genericModelNetworkResourceType: Defines how an endpoint can be accessed. By specifying the following properties:- URL (Required)
- HTTP method (Optional, default GET)
- HTTP header fields (Optional)
- Body of request (JSON encoded) (Optional)
- URL query strings (Optional)
DataResourceType: Defines a resource that can create a genericModelfrom(NS)Data
JSONDecodableResourceType: Defines the transformation from a JSON response into an object that conforms toDecodableJSONDictionaryResourceType: Defines the transformation from a JSON object, i.e.[String: Any]to a genericModelJSONArrayResourceType: Defines the transformation from a JSON array, i.e.[Any]to a genericModelImageResourceType: Defines the transformation from(NS)Datato aUIImagePropertyListDecodableResourceType:
NetworkJSONDictionaryResourceType: CombinesJSONDictionaryResourceTypeandNetworkResourceTypeto allow for retrieving a genericModelfrom a JSON dictionary from a web service.NetworkJSONArrayResourceType: CombinesJSONArrayResourceTypeandNetworkResourceTypeto allow for retrieving a genericModelfrom a JSON array from a web service.NetworkJSONDecodableResourceType: CombinesJSONDecodableResourceTypeandNetworkResourceTypeto allow for retrieving a genericModelfrom a JSON response from a web service.
Note: The above all include ["Content-Type": "application/json"] as default header fields.
NetworkPropertyListDecodableResourceType: CombinesPropertyListDecodableResourceTypeandNetworkResourceTypeto allow for retrieving a genericModelfrom a Property List format XML response from a web service. It includes the["Content-Type": "application/x-plist"]as default header fields.
NetworkImageResource: Conforms toImageResourceTypeand can be initialized with aURL
NetworkDataResourceService: Used to retrieve a resource that conforms toNetworkResourceTypeandDataResourceTypefetchfunction returns aCancellableobject which can be used to cancel the network request- When the network request finishes a completion handler is called with a
NetworkResponse<Model>enum
GenericNetworkDataResourceService: InheritsNetworkDataResourceServiceand conforms toResourceServiceType. The purpose of this service is to be use withResourceOperation
- Uses a service that conforms to
ResourceServiceTypeto retrieve a resource that conforms toResourceType - Subclass of
(NS)Operationused to retrieve a resource with specific service - Uses a completion handler when the operation is finished to pass it's
Result
Guidelines for contributing can be found here.
Luciano Marisi @lucianomarisi
The original idea for this pattern is explained on Protocol oriented loading of resources from a network service in Swift
TABResourceLoader is available under the MIT license. See the LICENSE file for more info.
