Skip to content

feat: add swift support #319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from
37 changes: 37 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,43 @@ RUN gem install bundler -v 2.3.26 && \
apt-get update && \
apt-get install -y --no-install-recommends ruby-dev=1:2.7+2

# swift

RUN apt-get -y install libncurses5 clang

RUN apt-get -y install libxml2

RUN \
curl https://download.swift.org/swift-5.9.2-release/ubuntu1804/swift-5.9.2-RELEASE/swift-5.9.2-RELEASE-ubuntu18.04.tar.gz -o swift.tar.gz &&\
tar xzf swift.tar.gz && \
mv swift-5.9.2-RELEASE-ubuntu18.04 /usr/share/swift && \
export PATH=/usr/share/swift/usr/bin:$PATH && \
swift -v

ENV PATH /usr/share/swift/usr/bin:$PATH

# Install swift-openapi-generator
RUN git clone https://github.com/apple/swift-openapi-generator.git \
&& cd swift-openapi-generator \
&& swift build \
&& ln -s $(pwd)/.build/debug/swift-openapi-generator /usr/local/bin/swift-openapi-generator \
&& swift-openapi-generator --help

# Install Homebrew
RUN git clone https://github.com/Homebrew/brew /home/linuxbrew/Homebrew \
&& mkdir -p /home/linuxbrew/bin \
&& ln -s /home/linuxbrew/Homebrew/bin/brew /home/linuxbrew/bin/ \
&& export PATH="/home/linuxbrew/bin:$PATH"

# Export Homebrew binary directory to PATH
ENV PATH /home/linuxbrew/bin:$PATH

# Disable automatic updates
ENV HOMEBREW_NO_AUTO_UPDATE=1

# Install SourceDocs to generate swift client documentation
RUN brew install sourcedocs

ADD go.mod go.mod
ADD go.sum go.sum
RUN go build -o /usr/local/bin/ory github.com/ory/cli
Expand Down
26 changes: 26 additions & 0 deletions contrib/clients/swift/CustomDateTranscoder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import Foundation
import OpenAPIRuntime

/// A transcoder for dates encoded as an ISO-8601 string (in RFC 3339 format). Allows date with internet date time and fractional seconds.
public struct CustomDateTranscoder: DateTranscoder, @unchecked Sendable {

/// Creates and returns an ISO 8601 formatted string representation of the specified date.
public func encode(_ date: Date) throws -> String {
let formatter = ISO8601DateFormatter()
formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
return formatter.string(from: date) }

/// Creates and returns a date object from the specified ISO 8601 formatted string representation.
public func decode(_ dateString: String) throws -> Date {
let formatter = ISO8601DateFormatter()
formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
print(dateString)
guard let date = formatter.date(from: dateString) else {
throw DecodingError.dataCorrupted(
.init(codingPath: [], debugDescription: "Expected date string to be ISO8601-formatted.")
)
}
return date
}
}

23 changes: 23 additions & 0 deletions contrib/clients/swift/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// swift-tools-version: 5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription


let package = Package(
name: "OryClient",
platforms: [.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), .visionOS(.v1)],
dependencies: [
.package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-openapi-urlsession", from: "1.0.0")
],
targets: [
.executableTarget(
name: "OryClient",
dependencies: [
.product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"),
.product(name: "OpenAPIURLSession", package: "swift-openapi-urlsession")
]
)
]
)
45 changes: 45 additions & 0 deletions contrib/clients/swift/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Ory Swift SDK

Documentation for all public and administrative Ory APIs. Administrative APIs can only be accessed with a valid Personal Access Token. Public APIs are mostly used in browsers.


This package is atomatically generated by [Swift OpenAPI Generator](https://github.com/apple/swift-openapi-generator)

## Installation

Add the package dependency in your Package.swift:

```swift
.package(url: "https://github.com/ory/client-swift", from: "1.0.0"),
```

Next, in your target, add OryClient to your dependencies:

```swift
.target(name: "MyTarget", dependencies: [
.product(name: "OryCLient", package: "OryClient"),
]),
```



## Usage

When creating an Ory client, use CustomDateTranscoder to support date with internet date time and fractional seconds.

```swift
import OpenAPIRuntime
import OpenAPIURLSession
import Foundation

let serverURL = URL(string: "https://{your-project-slug}.projects.oryapis.com")
let customDateTranscoder = CustomDateTranscoder()
let transport = URLSessionTransport()

let oryClient = Client(serverURL: serverURL!,
configuration: Configuration(dateTranscoder: customDateTranscoder),
transport: transport)

let response = try await oryClient.createNativeLoginFlow()
print(try response.ok)
```
Loading