Skip to content

Sunday SWIFT outfoxx/sunday-swift

A Sunday client library implementation written in Swift and supporting Apple platforms including macOS, iOS, iPadOS, and watchOS.


Tip

While you can use Sunday (Swift) to write REST clients manually Sunday is most useful when generating clietns from API definitions using the Sunday Generator.

Learn about Swift Generator

Installation

Sunday is delivered as a Swift Package Manager package.

Package URL

https://github.com/outfoxx/sunday-swift.git

Current Release

1.0.0-beta.13

Usage

After installing the Sunday (Swift) package int the target project, using Sunday only requires importing the module by name.

import Sunday

Initializing Service Clients

Service clients need to be initialized with an RequestFactory implementation to adapt service methods into network requests.

Sunday's standard implementation of RequestFactory, named NetworkRequestFactory, is based on Apple's URLSession.

You initialize the NetworkRequestFactory with a base URL template:

let baseURL = URI.Template("https://example.com/api/v1");

let requestFactory = NetworkRequestFactory(baseURL: baseURL);

Customizing Requests

If needed, you can pass an adapter to the initializer to customize generated requests. The adapter allows you to add authorization headers, change the URL or make any other changes needed to requests generated by the library.

let baseURI = URI.Template("https://example.com/api/v1");
let authorizer =
    HeaderTokenAuthorizingAdapter(tokenHeaderType: "Bearer", token: accessToken)

let requestFactory = NetworkRequestFactory(baseURI, adapter: authorizer);

Certificate Pinning

NetworkRequestFactory supports certificate pinning for added security.

Passing a ServerTrustPolicyManager to the request factory allows configuring server trust globally or on a per host basis.

let baseURI = URI.Template("https://example.com/api/v1");

let publicKeys = ServerTrustPolicy.publicKeys(in: Bundle.main)
let pinnedTrust = ServerTrustPolicyManager([
    "example.com": ServerTrustPolicy.pinPublicKeys(
        publicKeys: publicKeys,
        validateCertificateChain: true,
        validateHost: true
    )
])

let requestFactory =
    NetworkRequestFactory(baseURI, serverTrustPolicyManager: pinnedTrust);

License

Copyright 2015 Outfox, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.