Skip to content
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

#14 - Updated type names to be consistent with CloudKit framework #15

Merged
merged 1 commit into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,20 @@ The goal of this project is to provide developers with easy access to CloudKit w

## Getting Started

To get started with CloudKit Web Services, first create a `CKWSContainer`.
This package is designed to mirror Apple's CloudKit framework when possible. This will allow the library to be a drop-in replacement for the native CloudKit client when it isn't available.

To get started with CloudKit Web Services, first create a `CKContainer`.

```

// Configuration

import CloudKitWebServices

let identifier = "iCloud.{your-container-name}"
let token = "{your-token-generated-from-cloudkit-console}"

let container = CKWSContainer(identifier: identifier, token: token, enviornment: .development)
let container = CKContainer(identifier: identifier, token: token, environment: .development)

```

Expand All @@ -36,7 +41,7 @@ Creating a query request
```

// Create an operation that queries all records of the "ExampleType"
let queryOperation = CKWSQueryOperation(query: CKWSQuery(recordType: "ExampleType"))
let queryOperation = CKQueryOperation(query: CKQuery(recordType: "ExampleType"))

queryOperation.recordMatchedBlock = { recordID, result in
switch result {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// CKWSAsset.swift
// CKAsset.swift
// CloudKitWebServices
//
// Created by Eric Dorphy on 6/19/21.
Expand All @@ -9,7 +9,7 @@
import Foundation

/// An external file that belongs to a record.
public struct CKWSAsset {
public struct CKAsset {
/// The URL for accessing the asset.
///
/// After you create an asset, use the URL in this property to access the asset's contents. The URL in this property is different from the one you specified when creating the asset.
Expand All @@ -22,4 +22,4 @@ public struct CKWSAsset {
}
}

extension CKWSAsset: CKWSRecordValueProtocol { }
extension CKAsset: CKRecordValueProtocol { }
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// CKWSRemoteAsset.swift
// CKRemoteAsset.swift
// CloudKitWebServices
//
// Created by Eric Dorphy on 12/26/21.
Expand All @@ -8,7 +8,7 @@

import Foundation

public struct CKWSRemoteAsset: CKWSAssetProtocol {
public struct CKRemoteAsset: CKAssetProtocol {
public let downloadURL: URL

init(assetDictionary: AssetDictionary) {
Expand All @@ -20,4 +20,4 @@ public struct CKWSRemoteAsset: CKWSAssetProtocol {
}
}

extension CKWSRemoteAsset: CKWSRecordValueProtocol { }
extension CKRemoteAsset: CKRecordValueProtocol { }
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// CKWSContainer-Configuration.swift
// CKContainer-Configuration.swift
// CloudKitWebServices
//
// Created by Eric Dorphy on 1/2/22.
Expand All @@ -8,7 +8,7 @@

import Foundation

public extension CKWSContainer {
public extension CKContainer {
struct Configuration {
public let containerIdentifier: ContainerIdentifier
public let apiToken: APIToken
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// CKWSContainer-CurrentUser.swift
// CKContainer-CurrentUser.swift
// CloudKitWebServices
//
// Created by Eric Dorphy on 12/24/21.
Expand All @@ -8,33 +8,33 @@

import Foundation

extension CKWSContainer {
extension CKContainer {

// TODO: This error object is temporary and will be hidden internally once WSWebAuthenticationSession code is working with something like a `showSignIn` func.
public struct LoginError: Error {
public let redirectURL: URL
}

public func fetchUserRecordID(completionHandler: @escaping (Result<CKWSRecord.ID, Error>) -> Void) {
let operation = CKWSFetchCurrentUserRecordOperation(session: self.session, containerURL: self.getContainerURL(), apiToken: self.apiToken) { result in
public func fetchUserRecordID(completionHandler: @escaping (Result<CKRecord.ID, Error>) -> Void) {
let operation = CKFetchCurrentUserRecordOperation(session: self.session, containerURL: self.getContainerURL(), apiToken: self.apiToken) { result in
completionHandler(result)
}

self.add(operation)
}
}

private class CKWSFetchCurrentUserRecordOperation: CKWSOperation {
private class CKFetchCurrentUserRecordOperation: CKOperation {

private let session: URLSession

private let containerURL: URL

private let apiToken: CKWSContainer.APIToken
private let apiToken: CKContainer.APIToken

private let resultBlock: (Result<CKWSRecord.ID, Error>) -> Void
private let resultBlock: (Result<CKRecord.ID, Error>) -> Void

init(session: URLSession, containerURL: URL, apiToken: CKWSContainer.APIToken, resultBlock: @escaping (Result<CKWSRecord.ID, Error>) -> Void) {
init(session: URLSession, containerURL: URL, apiToken: CKContainer.APIToken, resultBlock: @escaping (Result<CKRecord.ID, Error>) -> Void) {
self.session = session
self.containerURL = containerURL
self.apiToken = apiToken
Expand Down Expand Up @@ -62,7 +62,7 @@ private class CKWSFetchCurrentUserRecordOperation: CKWSOperation {
do {
let responseBody = try JSONDecoder().decode(OkResponseBody.self, from: data)

self.resultBlock(.success(CKWSRecord.ID(recordName: responseBody.userRecordName)))
self.resultBlock(.success(CKRecord.ID(recordName: responseBody.userRecordName)))
} catch {
assertionFailure("failed to decode ok response body with error: \(error)")
self.resultBlock(.failure(error))
Expand All @@ -86,7 +86,7 @@ private class CKWSFetchCurrentUserRecordOperation: CKWSOperation {

print("OAuth Token: \(responseBody.redirectURL.absoluteString)")

self.resultBlock(.failure(CKWSContainer.LoginError(redirectURL: responseBody.redirectURL)))
self.resultBlock(.failure(CKContainer.LoginError(redirectURL: responseBody.redirectURL)))
} catch {

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// CKWSContainer.swift
// CKContainer.swift
// CloudKitWebServices
//
// Created by Eric Dorphy on 6/12/21.
Expand All @@ -8,7 +8,7 @@

import Foundation

public class CKWSContainer {
public class CKContainer {

// MARK: - Types

Expand All @@ -27,8 +27,8 @@ public class CKWSContainer {

public let environment: Environment

public private(set) lazy var publicCloudDatabase: CKWSDatabase = {
CKWSDatabase(container: self, scope: .public)
public private(set) lazy var publicCloudDatabase: CKDatabase = {
CKDatabase(container: self, scope: .public)
}()

internal let apiToken: APIToken
Expand All @@ -53,21 +53,21 @@ public class CKWSContainer {

// MARK: - Public Functions

public func database(with scope: CKWSDatabase.Scope) -> CKWSDatabase {
public func database(with scope: CKDatabase.Scope) -> CKDatabase {
switch scope {
case .public:
return self.publicCloudDatabase
}
}

public func add(_ operation: CKWSOperation) {
public func add(_ operation: CKOperation) {

// TODO: Inspect the configuration and apply them to the operation before enqueuing.
self.operationQueue.addOperation(operation)
}
}

extension CKWSContainer {
extension CKContainer {

func getContainerURL() -> URL {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// CKWSDatabase.swift
// CKDatabase.swift
// CloudKitWebServices
//
// Created by Eric Dorphy on 6/12/21.
Expand All @@ -8,7 +8,7 @@

import Foundation

public class CKWSDatabase {
public class CKDatabase {

// MARK: - Types

Expand All @@ -27,18 +27,18 @@ public class CKWSDatabase {

// MARK: - Properties

internal private(set) weak var container: CKWSContainer?
internal private(set) weak var container: CKContainer?

public let scope: Scope

// MARK: - Initialization

init(container: CKWSContainer, scope: Scope) {
init(container: CKContainer, scope: Scope) {
self.container = container
self.scope = scope
}

public func add(_ operation: CKWSDatabaseOperation) {
public func add(_ operation: CKDatabaseOperation) {
guard operation.isCancelled == false else {
// TODO: Check CloudKit behavior and see which completion handlers are still invoked in this case.
return
Expand All @@ -57,7 +57,7 @@ public class CKWSDatabase {
}
}

internal extension CKWSDatabase {
internal extension CKDatabase {
func getURL() -> URL {
guard let container = container else {
fatalError("database is missing container")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// CKWSError.swift
// CKError.swift
// CloudKitWebServices
//
// Created by Eric Dorphy on 12/26/21.
Expand All @@ -8,7 +8,7 @@

import Foundation

public struct CKWSError: Error {
public struct CKError: Error {
public let code: Code
public let userInfo: [String: Any]

Expand All @@ -18,7 +18,7 @@ public struct CKWSError: Error {
}
}

public extension CKWSError {
public extension CKError {

// https://developer.apple.com/documentation/cloudkit/ckerror/code

Expand Down Expand Up @@ -61,11 +61,11 @@ public extension CKWSError {
}
}

extension CKWSError: LocalizedError {
extension CKError: LocalizedError {
// TODO: Implement this
}

extension CKWSError: CustomNSError {
extension CKError: CustomNSError {

public var errorCode: Int {
code.rawValue
Expand All @@ -76,11 +76,11 @@ extension CKWSError: CustomNSError {
}

public static var errorDomain: String {
"CKWSErrorDomain"
"CKErrorDomain"
}
}

internal extension CKWSError {
internal extension CKError {
init(errorDictionary: RecordFetchErrorDictionary) {
switch errorDictionary.serverErrorCode {
case .notFound:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// CKWSOperation.swift
// CKOperation.swift
// CloudKitWebServices
//
// Created by Eric Dorphy on 6/12/21.
Expand All @@ -8,7 +8,7 @@

import Foundation

public class CKWSOperation: Operation {
public class CKOperation: Operation {

// MARK: - Types

Expand Down Expand Up @@ -45,7 +45,7 @@ public class CKWSOperation: Operation {

// MARK: - Operation Support

public extension CKWSOperation {
public extension CKOperation {

override var isAsynchronous: Bool {
true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// CKWSDatabaseOperation.swift
// CKDatabaseOperation.swift
// CloudKitWebServices
//
// Created by Eric Dorphy on 6/12/21.
Expand All @@ -8,12 +8,12 @@

import Foundation

public class CKWSDatabaseOperation: CKWSOperation {
var database: CKWSDatabase?
public class CKDatabaseOperation: CKOperation {
var database: CKDatabase?
}

internal extension CKWSDatabaseOperation {
func getRecordsURL(database: CKWSDatabase) -> URL {
internal extension CKDatabaseOperation {
func getRecordsURL(database: CKDatabase) -> URL {
let url = database.getURL()
return url.appendingPathComponent("records")
}
Expand Down
Loading