DataService
public class DataService
The DataService
class provides functionality for interfacing with the Peter Symonds College
data service.
This includes authentication and retrieval of information from the Timetable, Find, Room Timetable, and User services.
-
A typealias used only in
authenticateFromSavedDetails(completion:)
to simplify the declaration.Declaration
Swift
public typealias AuthenticationCompletion = (
Parameters
error
An error if one occurred during the request; otherwise
nil
. -
A typealias used only in exchangeCodeForToken(_:completion:) in order to simplify the declaration of the completion block.
Declaration
Swift
public typealias ExchangeCompletion = (
Parameters
result
The authentication result.
error
An error, if one occurred.
-
Singleton instance of
DataService
.Declaration
Swift
public static var shared = DataService()
-
The URL used for retrieving an access token.
Declaration
Swift
public var getAccessTokenURL: URL
-
The redirect URL, passed to the Symonds Data Service in authentication requests.
Apart from being required by the Data Service, this redirect URL enables a return to the host application after authenticating externally.
Declaration
Swift
public let redirectURL = URL(string: "app://com.sorenmortensen.SymondsStudentApp")!
-
A pair of keys used for authentication with the Symonds Data Service.
These keys are secret and are therefore loaded from a JSON file that is copied into the main bundle at build time.
Declaration
Swift
private let keys: (clientID: String, secret: String)? =
-
The result of the most recent authentication.
Declaration
Swift
private var authenticationResult: AuthenticationResult?
-
A shared session used for sending network requests.
Declaration
Swift
private let session = URLSession(configuration: .default)
-
The URL where authentication requests are sent.
Declaration
Swift
private let authURL = URL(string: "https://data.psc.ac.uk/oauth/v2/auth")!
-
The URL where token exchange requests are sent.
Declaration
Swift
private let tokenURL = URL(string: "https://data.psc.ac.uk/oauth/v2/token")!
-
The result of an authentication request sent to the Symonds Data Service.
See moreDeclaration
Swift
public struct AuthenticationResult
-
A type of grant that can be used to request an access token from the Symonds Data Service.
See moreDeclaration
Swift
public enum GrantType: String
-
An error.
See moreDeclaration
Swift
public enum Error: Swift.Error
-
A private initialiser to ensure that access to
DataService
is only through theshared
singleton.Declaration
Swift
private init()
-
Attempts to use saved authentication details from Keychain to authenticate with the Data Service.
Declaration
Swift
public func authenticateFromSavedDetails(completion: @escaping AuthenticationCompletion)
Parameters
completion
Called when the request completes.
-
Exchanges an authorisation code for an access token.
Declaration
Swift
public func exchangeCodeForToken(_ code: String, grantType: GrantType, completion: @escaping ExchangeCompletion)
Parameters
code
The authorisation code to exchange.
completion
Called when the network request completes.
-
Generates an HTTP POST string to exchange an authorisation code for an access token.
Throws
An error of type
AuthenticationError
in the event of a relevant error.Declaration
Swift
private func generateExchangePOSTString(with code: String, grantType: GrantType) throws -> String
Parameters
code
The authorisation code to use for authentication when requesting a token.
Return Value
The generated string.
-
Creates an
AuthenticationResult
instance from a JSON object returned by the Symonds Data Service.Throws
Throws errors of type
AuthenticationError
in the event of a relevant error.Declaration
Swift
private func serializeLoginResponse(from data: Data) throws -> AuthenticationResult
Parameters
data
The data returned by the Symonds Data Service.