KituraKit
public class KituraKit
A client side library for using REST requests in a web application.
-
Default URL used for setting up the routes when no URL is provided in the initializer.
Declaration
Swift
public static var defaultBaseURL: URL
-
Default route used for setting up the paths based on the URL provided in the initializer.
Declaration
Swift
public static var `default`: KituraKit { get }
-
Customisable URL used for setting up the routes when initializing a new KituraKit instance.
Declaration
Swift
public let baseURL: URL
-
User credentials that will be used for authentication if none are provided to the route.
Usage Example:
let client = KituraKit.default client.defaultCredentials = HTTPBasic(username: "John", password: "12345")
Declaration
Swift
public var defaultCredentials: ClientCredentials?
-
The
BodyEncoder
that will be used to encode the Codable object.Usage Example:
let client = KituraKit.default let encoder = JSONEncoder() encoder.dateEncodingStrategy = .secondsSince1970 client.encoder = encoder
Declaration
Swift
public var encoder: BodyEncoder
-
The
BodyDecoder
that will be used to decode the response Codable object.Usage Example:
let client = KituraKit.default let decoder = JSONDecoder() decoder.dateDecodingStrategy = .secondsSince1970 client.decoder = decoder
Declaration
Swift
public var decoder: BodyDecoder
-
The
String
that will be used for the Content-Type and Accept headers in the HTTP requests.Usage Example:
let client = KituraKit.default client.mediaType = "application/xml"
Declaration
Swift
public var mediaType: String
-
An initializer to set up a custom KituraKit instance on a specified route using a URL
Declaration
Swift
public init(baseURL: URL, containsSelfSignedCert: Bool = false, clientCertificate: ClientCertificate? = nil)
Parameters
baseURL
The custom route KituraKit points to during REST requests.
containsSelfSignedCert
Pass
True
to use self signed certificatesclientCertificate
Pass in
ClientCertificate
with the certificate name and path to use client certificates for 2-way SSL -
An initializer to set up a custom KituraKit instance on a specified route.
Declaration
Swift
public convenience init?(baseURL: String, containsSelfSignedCert: Bool = false, clientCertificate: ClientCertificate? = nil)
Parameters
baseURL
The custom route KituraKit points to during REST requests.
containsSelfSignedCert
Pass
True
to use self signed certificatesclientCertificate
Pass in
ClientCertificate
with the certificate name and path to use client certificates for 2-way SSLReturn Value
nil if invalid URL. Otherwise return a KituraKit object
-
Retrieves data from a designated route.
Usage Example:
struct User: Codable { ... } let client = KituraKit.default client.get("/") { (returnedArray: [User]?, error: Error?) -> Void in print(returnedArray) }
This declaration of get retrieves all items. There is another declaration for specific item retrieval.
Declaration
Swift
public func get<O>(_ route: String, credentials: ClientCredentials? = nil, respondWith: @escaping CodableResultClosure<O>) where O : Decodable, O : Encodable
Parameters
route
The custom route KituraKit points to during REST requests.
-
Retrieves data from a designated route with an Identifier.
Usage Example:
struct User: Codable { ... } let client = KituraKit.default let idOfUserToRetrieve: Int = ... client.get("/", identifier: idOfUserToRetrieve) { (returnedItem: User?, error: Error?) -> Void in print(returnedItem) }
- This declaration of get retrieves single items. There is another declaration for all item retrieval.
Declaration
Swift
public func get<O>(_ route: String, identifier: Identifier, credentials: ClientCredentials? = nil, respondWith: @escaping CodableResultClosure<O>) where O : Decodable, O : Encodable
Parameters
route
The custom route KituraKit points to during REST requests.
identifier
The custom Identifier object that is searched for.
-
Sends data to a designated route.
Usage Example:
struct User: Codable { ... } let client = KituraKit.default let userToSend: User = ... client.post("/", data: userToSend) { (returnedItem: User?, error: Error?) -> Void in print(returnedItem) }
Declaration
Swift
public func post<I, O>(_ route: String, data: I, credentials: ClientCredentials? = nil, respondWith: @escaping CodableResultClosure<O>) where I : Decodable, I : Encodable, O : Decodable, O : Encodable
Parameters
route
The custom route KituraKit points to during REST requests.
data
The custom Codable object passed in to be sent.
-
Sends data to a designated route, allowing for the route to respond with an additional Identifier.
Usage Example:
struct User: Codable { ... } let client = KituraKit.default let userToSend: User = ... client.post("/", data: userToSend) { (id: Int?, returnedItem: User?, error: Error?) -> Void in print("\(id): \(returnedItem)") }
Declaration
Swift
public func post<I, Id, O>(_ route: String, data: I, credentials: ClientCredentials? = nil, respondWith: @escaping IdentifierCodableResultClosure<Id, O>) where I : Decodable, I : Encodable, Id : Identifier, O : Decodable, O : Encodable
Parameters
route
The custom route KituraKit points to during REST requests.
data
The custom Codable object passed in to be sent.
-
Updates data for a designated route using an Identifier.
Usage Example:
struct User: Codable { ... } let client = KituraKit.default let idOfUserToUpdate: Int = ... let userToSend: User = ... client.put("/", identifier: idOfUserToUpdate, data: userToSend) { (returnedItem: User?, error: Error?) -> Void in print(returnedItem) }
- This declaration uses the put method to update data.
Declaration
Swift
public func put<I, O>(_ route: String, identifier: Identifier, data: I, credentials: ClientCredentials? = nil, respondWith: @escaping CodableResultClosure<O>) where I : Decodable, I : Encodable, O : Decodable, O : Encodable
Parameters
route
The custom route KituraKit points to during REST requests.
identifier
The custom Identifier object that is searched for.
data
The custom Codable object passed in to be sent.
-
Updates data for a designated route using an Identifier.
Usage Example:
struct User: Codable { let name: String let address: String ... } struct OptionalUser: Codable { let name: String? let address: String? ... } let client = KituraKit.default let idOfUserToUpdate: Int = ... let userUpdates = OptionalUser(name: "New Name", address: nil, ...) client.patch("/", identifier: idOfUserToUpdate, data: userUpdates) { (returnedItem: User?, error: Error?) -> Void in print(returnedItem) }
- This declaration uses the patch method to update data.
Declaration
Swift
public func patch<I, O>(_ route: String, identifier: Identifier, data: I, credentials: ClientCredentials? = nil, respondWith: @escaping CodableResultClosure<O>) where I : Decodable, I : Encodable, O : Decodable, O : Encodable
Parameters
route
The custom route KituraKit points to during REST requests.
identifier
The custom Identifier object that is searched for.
-
Deletes data at a designated route.
Usage Example:
let client = KituraKit.default client.delete("/") { error in print("Successfully deleted") }
- This declaration of delete deletes all items. There is another declaration for single item deletion.
Declaration
Swift
public func delete(_ route: String, credentials: ClientCredentials? = nil, respondWith: @escaping ResultClosure)
Parameters
route
The custom route KituraKit points to during REST requests.
-
Deletes data at a designated route using an Identifier.
Usage Example:
let client = KituraKit.default client.delete("/", identifier: urlToSend) { error in print("Successfully deleted") }
- This declaration of delete deletes single items. There is another declaration for all item deletion.
Declaration
Swift
public func delete(_ route: String, identifier: Identifier, credentials: ClientCredentials? = nil, respondWith: @escaping ResultClosure)
Parameters
route
The custom route KituraKit points to during REST requests.
identifier
The custom Identifier object that is searched for.
-
Retrieves data at a designated route using a the specified Query Parameters.
Usage Example:
struct MyQuery: QueryParams { let name: String } let myQuery = MyQuery(name: "Michael") let client = KituraKit.default client.get("/users", query: myQuery) { (returnedArray: [O]?, error: Error?) -> Void in print("Successfully returned users with name 'Michael'") print(returnedArray) }
Declaration
Swift
public func get<O, Q>(_ route: String, query: Q, credentials: ClientCredentials? = nil, respondWith: @escaping CodableArrayResultClosure<O>) where O : Decodable, O : Encodable, Q : QueryParams
Parameters
route
The custom route KituraKit points to during REST requests.
queryParams
The QueryParam structure containing the route’s query parameters
-
Deletes data at a designated route using a the specified Query Parameters.
Usage Example:
struct MyQuery: QueryParams { let name: String } let myQuery = MyQuery(name: "Michael") let client = KituraKit.default client.delete("/users", query: myQuery) { error in print("Successfully deleted users with name 'Michael'") }
- This declaration of delete deletes multiple items. There is another declaration for a singular deletion and another for all item deletion.
Declaration
Swift
public func delete<Q>(_ route: String, query: Q, credentials: ClientCredentials? = nil, respondWith: @escaping ResultClosure) where Q : QueryParams
Parameters
route
The custom route KituraKit points to during REST requests.
queryParams
The QueryParam structure containing the route’s query parameters