ServiceRouter
@objc public class ServiceRouter: NSObject, ServiceRouterProtocol
The ServiceRouter controls what services are available or unavailable and processes service requests.
When a service is registered it is made available to process requests. When it is unregistered it is no longer available to service requests.
All services registered must have a unique serviceIdentifier string.
Attempts to register a service with a serviceIdentifier string that is already registered will not be successful.
-
Shared Instance
Declaration
Swift
public static let sharedInstance: ServiceRouterProtocol = ServiceRouter.internalInstance -
Registers the provided service class.
Declaration
Swift
@discardableResult public func registerService(_ service: ServiceProtocol.Type ) -> BoolParameters
serviceclass type meeting the ServiceProtocol type.
Return Value
true if registration was successful.
-
Unregisters the provided service class, if the service was previously registered.
If the service was not registered this method has no effect.
Declaration
Swift
public func unregisterService(_ service: ServiceProtocol.Type)Parameters
serviceclass type meeting the ServiceProtocol type to unregister
-
Unregisters a service class by it’s serviceIdentifier string.
Does nothing if no service with the given service identifier string was previously registered.
Declaration
Swift
public func unregisterServiceByIdentifier(_ serviceId: String)Parameters
serviceIdString corresponding to a previously registered service’s service identifier.
-
Constructs a basic GET request for given service identifier and processes the provided request, routing the request to the appropriate service.
All processRequest methods should be considered asynchronous. Return closures may not be called on the same dispatch queue as the original request.
The request URL will be similar to:
http://pmapi/serviceidDeclaration
Swift
public func processRequest(serviceId: String, responseBlock : @escaping responseReturnBlock, dataBlock : @escaping dataReturnBlock, completionBlock : @escaping requestCompleteBlock)Parameters
serviceIdService Identifier, specifying the service that should process the request.
responseBlockFirst of three return closures. The service will call this closure to return response object back to the calling code.
dataBlockSecond of the three return closures. The service will call this closure to return data back to the calling code. This closure may not be called if the service does not return any data.
completionBlockThird and final of the three return closures. The service will call this closure when the service request has completed.
-
Constructs a GET request for given service identifier and processes the provided request, routing the request to the appropriate service.
All processRequest methods should be considered asynchronous. Return closures may not be called on the same dispatch queue as the original request.
The request URL will be similar to:
http://pmapi/serviceid/extrapathDeclaration
Swift
public func processRequest(_ serviceId: String, extraPath: String?, responseBlock : @escaping responseReturnBlock, dataBlock : @escaping dataReturnBlock, completionBlock : @escaping requestCompleteBlock)Parameters
serviceIdService Identifier, specifying the service that should process the request.
extraPathOptional string to include in the request URL after the service identifier. This string may include any valid URL components, including multiple path depths, and query strings
- examples, given a servideId of
serviceid
, and extraPath of:foo–> resulting url:http://pmapi/serviceid/foofoo/bar?some=query&another=value–> resulting url:http://pmapi/serviceid/foo/bar?some=query&another=value
responseBlockFirst of three return closures. The service will call this closure to return response object back to the calling code.
dataBlockSecond of the three return closures. The service will call this closure to return data back to the calling code. This closure may not be called if the service does not return any data.
completionBlockThird and final of the three return closures. The service will call this closure when the service request has completed.
- examples, given a servideId of
-
Constructs a request for given service identifier and processes the provided request, routing the request to the appropriate service.
All processRequest methods should be considered asynchronous. Return closures may not be called on the same dispatch queue as the original request.
The request URL will be similar to:
http://pmapi/serviceid/extrapathexamples, given a servideId of
serviceid
, and extraPath of:foo–> resulting url:http://pmapi/serviceid/foofoo/bar?some=query&another=value–> resulting url:http://pmapi/serviceid/foo/bar?some=query&another=value
Declaration
Swift
public func processRequest(_ serviceId: String, extraPath: String?, method: String, data: Data?, responseBlock : @escaping responseReturnBlock, dataBlock : @escaping dataReturnBlock, completionBlock : @escaping requestCompleteBlock)Parameters
serviceIdService Identifier, specifying the service that should process the request.
extraPathOptional string to include in the request URL after the service identifier. This string may include any valid URL components, including multiple path depths, and query strings
methodHTTP method string to use for the request, i.e.
GET
,POST,
PUT,
DELETE,
OPTION", etc.dataBody data to include in the request.
responseBlockFirst of three return closures. The service will call this closure to return response object back to the calling code.
dataBlockSecond of the three return closures. The service will call this closure to return data back to the calling code. This closure may not be called if the service does not return any data.
completionBlockThird and final of the three return closures. The service will call this closure when the service request has completed.
-
Processes the provided request, routing the request to the appropriate service.
All processRequest methods should be considered asynchronous. Return closures may not be called on the same dispatch queue as the original request.
The request URL will be similar to:
http://pmapi/serviceid/extrapathDeclaration
Swift
public func processRequest(_ request: URLRequest, responseBlock : @escaping responseReturnBlock, dataBlock : @escaping dataReturnBlock, completionBlock : @escaping requestCompleteBlock)Parameters
requestThe URLRequest to process
responseBlockFirst of three return closures. The service will call this closure to return response object back to the calling code.
dataBlockSecond of the three return closures. The service will call this closure to return data back to the calling code. This closure may not be called if the service does not return any data.
completionBlockThird and final of the three return closures. The service will call this closure when the service request has completed.
View on GitHub
ServiceRouter Class Reference