Skip to content

Commit

Permalink
fix realtime authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
TorstenDittmann committed Oct 22, 2021
1 parent 254d0aa commit 2a55342
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 284 deletions.
15 changes: 1 addition & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Appwrite Apple SDK

![Cocoapods](https://img.shields.io/cocoapods/v/Appwrite.svg?color=green&style=flat-square)
![Swift Package Manager](https://img.shields.io/github/v/release/appwrite/sdk-for-apple.svg?color=green&style=flat-square)
![License](https://img.shields.io/github/license/appwrite/sdk-for-apple.svg?style=flat-square)
![Version](https://img.shields.io/badge/api%20version-0.11.0-blue.svg?style=flat-square)
Expand All @@ -20,25 +19,19 @@ Appwrite is an open-source backend as a service server that abstract and simplif

The Appwrite Swift SDK is available via multiple package managers, including Swift Package Manager. In order to use the Appwrite Swift SDK from Xcode, select File > Swift Packages > **Add Package Dependency**

>>IMAGE<<
In the dialog that appears, enter the Appwrite Swift SDK [package URL]([email protected]:appwrite/sdk-for-apple.git) and click **Next**.

Once the repository information is loaded, add your version rules and click **Next** again.

>>IMAGE<<
On the final screen, make sure you see `Appwrite` as a product selected for your target:

>>IMAGE<<
### Swift Package Manager

Add the package to your `Package.swift` dependencies:

```swift
dependencies: [
.package(url: "[email protected]:appwrite/sdk-for-apple.git", from: "0.1.0"),
.package(url: "[email protected]:appwrite/sdk-for-apple.git", from: "0.1.1"),
],
```

Expand All @@ -54,12 +47,6 @@ Then add it to your target:
),
```

### Cocoapods

```ruby
pod '', git: '[email protected]:appwrite/sdk-for-apple.git', tag: '0.1.0'
```


## Contribution

Expand Down
2 changes: 1 addition & 1 deletion Sources/Appwrite/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ open class Client {

open var headers: [String: String] = [
"content-type": "",
"x-sdk-version": "appwrite:swiftclient:0.1.0", "X-Appwrite-Response-Format": "0.11.0"
"x-sdk-version": "appwrite:swiftclient:0.1.1", "X-Appwrite-Response-Format": "0.11.0"
]

open var config: [String: String] = [:]
Expand Down
12 changes: 10 additions & 2 deletions Sources/Appwrite/Extensions/HTTPClientRequest+Cookies.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import AsyncHTTPClient
import Foundation
import NIO
import NIOHTTP1

extension HTTPClient.Request {
public mutating func addDomainCookies() {
let cookieJson = UserDefaults.standard.string(forKey: "\(url.host!)-cookies")
headers.addDomainCookies(for: url.host!)
}
}

extension HTTPHeaders {
public mutating func addDomainCookies(for domain: String) {
let cookieJson = UserDefaults.standard.string(forKey: "\(domain)-cookies")
let cookies: [HTTPClient.Cookie?]? = try? cookieJson?.fromJson(to: [HTTPClient.Cookie].self)
?? [(try? cookieJson?.fromJson(to: HTTPClient.Cookie.self))]

if let authCookie = cookies?.first(where: { $0?.name.starts(with: "a_session_") == true } ) {
headers.add(name: "cookie", value: "\(authCookie!.name)=\(authCookie!.value)")
add(name: "cookie", value: "\(authCookie!.name)=\(authCookie!.value)")
}
}
}
48 changes: 0 additions & 48 deletions Sources/Appwrite/Models/Query.swift

This file was deleted.

4 changes: 2 additions & 2 deletions Sources/Appwrite/Services/Realtime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ open class Realtime : Service {
if (socketClient != nil) {
reconnect = false
closeSocket()
} else {
socketClient = WebSocketClient(url, delegate: self)!
}

socketClient = WebSocketClient(url, delegate: self)!

try! socketClient?.connect()
}

Expand Down
1 change: 1 addition & 0 deletions Sources/Appwrite/WebSockets/HTTPHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ extension HTTPHandler : ChannelInboundHandler, RemovableChannelHandler {
headers.add(name: "Content-Type", value: "text/plain")
headers.add(name: "Content-Length", value: "\(1)")
headers.add(contentsOf: self.headers)
headers.addDomainCookies(for: client.host)
let requestHead = HTTPRequestHead(
version: .http1_1,
method: .GET,
Expand Down
217 changes: 0 additions & 217 deletions Tests/AppwriteTests/Tests.swift
Original file line number Diff line number Diff line change
@@ -1,222 +1,5 @@
import XCTest
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
import Appwrite
import AsyncHTTPClient
import NIO

class Tests: XCTestCase {

override func setUp() {
super.setUp()
self.writeToFile(string: "Test Started")
}

override func tearDown() {
super.tearDown()
}

func test() throws {
let group = DispatchGroup()

let client = Client()
.setEndpointRealtime("wss://demo.appwrite.io/v1")
.setProject("console")
.addHeader(key: "Origin", value: "http://localhost")
.setSelfSigned()

let foo = Foo(client)
let bar = Bar(client)
let general = General(client)
let realtime = Realtime(client)
var realtimeResponse = "Realtime failed!"

realtime.subscribe(channels: ["tests"]) { message in
realtimeResponse = message.payload!["response"] as! String
}

// Foo Tests
group.enter()
foo.get(x: "string", y: 123, z: ["string in array"]) { result in
switch result {
case .failure(let error): self.writeToFile(string: error.message)
case .success(let mock): self.writeToFile(string: mock.result)
}
group.leave()
}
group.wait()
group.enter()
foo.post(x: "string", y: 123, z: ["string in array"]) { result in
switch result {
case .failure(let error): self.writeToFile(string: error.message)
case .success(let mock): self.writeToFile(string: mock.result)
}
group.leave()
}
group.wait()
group.enter()
foo.put(x: "string", y: 123, z: ["string in array"]) { result in
switch result {
case .failure(let error): self.writeToFile(string: error.message)
case .success(let mock): self.writeToFile(string: mock.result)
}
group.leave()
}
group.wait()
group.enter()
foo.patch(x: "string", y: 123, z: ["string in array"]) { result in
switch result {
case .failure(let error): self.writeToFile(string: error.message)
case .success(let mock): self.writeToFile(string: mock.result)
}
group.leave()
}
group.wait()
group.enter()
foo.delete(x: "string", y: 123, z: ["string in array"]) { result in
switch result {
case .failure(let error): self.writeToFile(string: error.message)
case .success(let mock): self.writeToFile(string: mock.result)
}
group.leave()
}
group.wait()

// Bar Tests
group.enter()
bar.get(xrequired: "string", xdefault: 123, z: ["string in array"]) { result in
switch result {
case .failure(let error): self.writeToFile(string: error.message)
case .success(let mock): self.writeToFile(string: mock.result)
}
group.leave()
}
group.wait()
group.enter()
bar.post(xrequired: "string", xdefault: 123, z: ["string in array"]) { result in
switch result {
case .failure(let error): self.writeToFile(string: error.message)
case .success(let mock): self.writeToFile(string: mock.result)
}
group.leave()
}
group.wait()
group.enter()
bar.put(xrequired: "string", xdefault: 123, z: ["string in array"]) { result in
switch result {
case .failure(let error): self.writeToFile(string: error.message)
case .success(let mock): self.writeToFile(string: mock.result)
}
group.leave()
}
group.wait()
group.enter()
bar.patch(xrequired: "string", xdefault: 123, z: ["string in array"]) { result in
switch result {
case .failure(let error): self.writeToFile(string: error.message)
case .success(let mock): self.writeToFile(string: mock.result)
}
group.leave()
}
group.wait()
group.enter()
bar.delete(xrequired: "string", xdefault: 123, z: ["string in array"]) { result in
switch result {
case .failure(let error): self.writeToFile(string: error.message)
case .success(let mock): self.writeToFile(string: mock.result)
}
group.leave()
}
group.wait()

// General Tests
group.enter()
general.redirect() { result in
switch result {
case .failure(let error): self.writeToFile(string: error.message)
case .success(let mock): self.writeToFile(string: (mock as! [String: Any])["result"] as! String)
}
group.leave()
}
group.wait()
group.enter()

let url = URL(fileURLWithPath: "\(FileManager.default.currentDirectoryPath)/../../resources/file.png")
let buffer = ByteBuffer(data: try! Data(contentsOf: url))
let file = File(name: "file.png", buffer: buffer)
general.upload(x: "string", y: 123, z: ["string in array"], file: file) { result in
switch result {
case .failure(let error): self.writeToFile(string: error.message)
case .success(let mock): self.writeToFile(string: mock.result)
}
group.leave()
}
group.wait()

group.enter()
general.error400() { result in
switch result {
case .failure(let error): self.writeToFile(string: error.message)
case .success(let error): self.writeToFile(string: error.message)
}
group.leave()
}
group.wait()
group.enter()
general.error500() { result in
switch result {
case .failure(let error): self.writeToFile(string: error.message)
case .success(let error): self.writeToFile(string: error.message)
}
group.leave()
}
group.wait()
group.enter()
general.error502() { result in
switch result {
case .failure(let error): self.writeToFile(string: error.message)
case .success(let error): self.writeToFile(string: (error as! Error).message)
}
group.leave()
}
group.wait()

self.writeToFile(string: realtimeResponse)
}

private func writeToFile(string: String) {
let url = URL(fileURLWithPath: "\(FileManager.default.currentDirectoryPath)/../../../result.txt")
try! string.appendLine(to: url)
}
}

struct Response: Decodable {
let result: String?
let message: String?
}

extension String {
func appendLine(to url: URL) throws {
try self.appending("\n").append(to: url)
}

func append(to url: URL) throws {
let data = self.data(using: .utf8)
try data?.append(to: url)
}
}

extension Data {
func append(to url: URL) throws {
if let fileHandle = try? FileHandle(forWritingTo: url) {
defer {
fileHandle.closeFile()
}
fileHandle.seekToEndOfFile()
fileHandle.write(self)
} else {
try write(to: url)
}
}
}

0 comments on commit 2a55342

Please sign in to comment.