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 ) -> Bool

    Parameters

    service

    class 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

    service

    class 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

    serviceId

    String 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/serviceid

    Declaration

    Swift

    public func processRequest(serviceId: String, responseBlock : @escaping responseReturnBlock, dataBlock : @escaping dataReturnBlock, completionBlock : @escaping requestCompleteBlock)

    Parameters

    serviceId

    Service Identifier, specifying the service that should process the request.

    responseBlock

    First of three return closures. The service will call this closure to return response object back to the calling code.

    dataBlock

    Second 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.

    completionBlock

    Third 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/extrapath

    Declaration

    Swift

    public func processRequest(_ serviceId: String, extraPath: String?, responseBlock : @escaping responseReturnBlock, dataBlock : @escaping dataReturnBlock, completionBlock : @escaping requestCompleteBlock)

    Parameters

    serviceId

    Service Identifier, specifying the service that should process the request.

    extraPath

    Optional 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/foo
      • foo/bar?some=query&another=value –> resulting url: http://pmapi/serviceid/foo/bar?some=query&another=value
    responseBlock

    First of three return closures. The service will call this closure to return response object back to the calling code.

    dataBlock

    Second 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.

    completionBlock

    Third and final of the three return closures. The service will call this closure when the service request has completed.

  • 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/extrapath

    • examples, given a servideId of serviceid, and extraPath of:

      • foo –> resulting url: http://pmapi/serviceid/foo
      • foo/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

    serviceId

    Service Identifier, specifying the service that should process the request.

    extraPath

    Optional 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

    method

    HTTP method string to use for the request, i.e. GET, POST,PUT,DELETE,OPTION", etc.

    data

    Body data to include in the request.

    responseBlock

    First of three return closures. The service will call this closure to return response object back to the calling code.

    dataBlock

    Second 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.

    completionBlock

    Third 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/extrapath

    Declaration

    Swift

    public func processRequest(_ request: URLRequest, responseBlock : @escaping responseReturnBlock, dataBlock : @escaping dataReturnBlock, completionBlock : @escaping requestCompleteBlock)

    Parameters

    request

    The URLRequest to process

    responseBlock

    First of three return closures. The service will call this closure to return response object back to the calling code.

    dataBlock

    Second 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.

    completionBlock

    Third and final of the three return closures. The service will call this closure when the service request has completed.