Skip to content

Commit c39030d

Browse files
authored
Merge pull request #51 from appwrite/dev
Dev
2 parents d19fb1f + 799f169 commit c39030d

File tree

115 files changed

+2812
-603
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

115 files changed

+2812
-603
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2023 Appwrite (https://appwrite.io) and individual contributors.
1+
Copyright (c) 2024 Appwrite (https://appwrite.io) and individual contributors.
22
All rights reserved.
33

44
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

Package.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,17 @@ let package = Package(
1313
products: [
1414
.library(
1515
name: "Appwrite",
16-
targets: ["Appwrite", "AppwriteModels", "JSONCodable"]
16+
targets: [
17+
"Appwrite",
18+
"AppwriteEnums",
19+
"AppwriteModels",
20+
"JSONCodable"
21+
]
1722
),
1823
],
1924
dependencies: [
2025
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.9.0"),
21-
.package(url: "https://github.com/apple/swift-nio.git", from: "2.32.0")
26+
.package(url: "https://github.com/apple/swift-nio.git", from: "2.32.0"),
2227
],
2328
targets: [
2429
.target(
@@ -27,6 +32,7 @@ let package = Package(
2732
.product(name: "AsyncHTTPClient", package: "async-http-client"),
2833
.product(name: "NIOWebSocket", package: "swift-nio"),
2934
"AppwriteModels",
35+
"AppwriteEnums",
3036
"JSONCodable"
3137
]
3238
),
@@ -36,6 +42,9 @@ let package = Package(
3642
"JSONCodable"
3743
]
3844
),
45+
.target(
46+
name: "AppwriteEnums"
47+
),
3948
.target(
4049
name: "JSONCodable"
4150
),

README.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
![Swift Package Manager](https://img.shields.io/github/v/release/appwrite/sdk-for-apple.svg?color=green&style=flat-square)
44
![License](https://img.shields.io/github/license/appwrite/sdk-for-apple.svg?style=flat-square)
5-
![Version](https://img.shields.io/badge/api%20version-1.4.12-blue.svg?style=flat-square)
5+
![Version](https://img.shields.io/badge/api%20version-1.5.0-blue.svg?style=flat-square)
66
[![Build Status](https://img.shields.io/travis/com/appwrite/sdk-generator?style=flat-square)](https://travis-ci.com/appwrite/sdk-generator)
77
[![Twitter Account](https://img.shields.io/twitter/follow/appwrite?color=00acee&label=twitter&style=flat-square)](https://twitter.com/appwrite)
88
[![Discord](https://img.shields.io/discord/564160730845151244?label=discord&style=flat-square)](https://appwrite.io/discord)
99

10-
**This SDK is compatible with Appwrite server version 1.4.x. For older versions, please check [previous releases](https://github.com/appwrite/sdk-for-apple/releases).**
10+
**This SDK is compatible with Appwrite server version 1.5.x. For older versions, please check [previous releases](https://github.com/appwrite/sdk-for-apple/releases).**
1111

1212
Appwrite is an open-source backend as a service server that abstract and simplify complex and repetitive development tasks behind a very simple to use REST API. Appwrite aims to help you develop your apps faster and in a more secure way. Use the Apple SDK to integrate your app with the Appwrite server to easily start interacting with all of Appwrite backend APIs and tools. For full API documentation and tutorials go to [https://appwrite.io/docs](https://appwrite.io/docs)
1313

14-
![Appwrite](https://appwrite.io/images/github.png)
14+
![Appwrite](https://github.com/appwrite/appwrite/raw/main/public/images/github.png)
1515

1616
## Installation
1717

@@ -31,7 +31,7 @@ Add the package to your `Package.swift` dependencies:
3131

3232
```swift
3333
dependencies: [
34-
.package(url: "[email protected]:appwrite/sdk-for-apple.git", from: "4.0.2"),
34+
.package(url: "[email protected]:appwrite/sdk-for-apple.git", from: "5.0.0"),
3535
],
3636
```
3737

@@ -125,9 +125,10 @@ let account = Account(client)
125125

126126
do {
127127
let user = try await account.create(
128-
userId: ID.unique(),
129-
email: "[email protected]",
130-
password: "password"
128+
userId: ID.unique(),
129+
130+
password: "password",
131+
name: "Walter O'Brien"
131132
)
132133
print(String(describing: user.toMap()))
133134
} catch {
@@ -150,9 +151,10 @@ func main() {
150151

151152
do {
152153
let user = try await account.create(
153-
userId: ID.unique(),
154-
email: "[email protected]",
155-
password: "password"
154+
userId: ID.unique(),
155+
156+
password: "password",
157+
name: "Walter O'Brien"
156158
)
157159
print(String(describing: account.toMap()))
158160
} catch {

Sources/Appwrite/Client.swift

Lines changed: 72 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,31 @@ open class Client {
1414
// MARK: Properties
1515
public static var chunkSize = 5 * 1024 * 1024 // 5MB
1616

17-
open var endPoint = "https://HOSTNAME/v1"
17+
open var endPoint = "https://cloud.appwrite.io/v1"
1818

1919
open var endPointRealtime: String? = nil
2020

2121
open var headers: [String: String] = [
22-
"content-type": "",
22+
"content-type": "application/json",
2323
"x-sdk-name": "Apple",
2424
"x-sdk-platform": "client",
2525
"x-sdk-language": "apple",
26-
"x-sdk-version": "4.0.2",
27-
"X-Appwrite-Response-Format": "1.4.0"
26+
"x-sdk-version": "5.0.0",
27+
"x-appwrite-response-format": "1.5.0"
2828
]
2929

30-
open var config: [String: String] = [:]
30+
internal var config: [String: String] = [:]
3131

32-
open var selfSigned: Bool = false
32+
internal var selfSigned: Bool = false
3333

34-
open var http: HTTPClient
34+
internal var http: HTTPClient
3535

36-
private static let boundaryChars =
37-
"abcdefghijklmnopqrstuvwxyz1234567890"
36+
37+
private static let boundaryChars = "abcdefghijklmnopqrstuvwxyz1234567890"
3838

3939
private static let boundary = randomBoundary()
4040

41-
private static var eventLoopGroupProvider =
42-
HTTPClient.EventLoopGroupProvider.createNew
41+
private static var eventLoopGroupProvider = HTTPClient.EventLoopGroupProvider.singleton
4342

4443
// MARK: Methods
4544

@@ -80,7 +79,6 @@ open class Client {
8079
decompression: .enabled(limit: .none)
8180
)
8281
)
83-
8482
}
8583

8684
deinit {
@@ -134,6 +132,21 @@ open class Client {
134132
return self
135133
}
136134

135+
///
136+
/// Set Session
137+
///
138+
/// The user session to authenticate with
139+
///
140+
/// @param String value
141+
///
142+
/// @return Client
143+
///
144+
open func setSession(_ value: String) -> Client {
145+
config["session"] = value
146+
_ = addHeader(key: "X-Appwrite-Session", value: value)
147+
return self
148+
}
149+
137150

138151
///
139152
/// Set self signed
@@ -295,64 +308,54 @@ open class Client {
295308
withSink bufferSink: ((ByteBuffer) -> Void)? = nil,
296309
converter: ((Any) -> T)? = nil
297310
) async throws -> T {
298-
func complete(with response: HTTPClientResponse) async throws -> T {
299-
switch response.status.code {
300-
case 0..<400:
301-
if response.headers["Set-Cookie"].count > 0 {
302-
UserDefaults.standard.set(
303-
response.headers["Set-Cookie"],
304-
forKey: URL(string: request.url)!.host! + "-cookies"
305-
)
306-
}
307-
switch T.self {
308-
case is Bool.Type:
309-
return true as! T
310-
case is ByteBuffer.Type:
311-
return try await response.body.collect(upTo: Int.max) as! T
312-
default:
313-
let data = try await response.body.collect(upTo: Int.max)
314-
if data.readableBytes == 0 {
315-
return true as! T
316-
}
317-
let dict = try JSONSerialization.jsonObject(with: data) as? [String: Any]
311+
let response = try await http.execute(
312+
request,
313+
timeout: .seconds(30)
314+
)
318315

319-
return converter?(dict!) ?? dict! as! T
320-
}
316+
switch response.status.code {
317+
case 0..<400:
318+
if response.headers["Set-Cookie"].count > 0 {
319+
let domain = URL(string: request.url)!.host!
320+
let existing = UserDefaults.standard.stringArray(forKey: domain)
321+
let new = response.headers["Set-Cookie"]
322+
323+
UserDefaults.standard.set(new, forKey: domain)
324+
}
325+
switch T.self {
326+
case is Bool.Type:
327+
return true as! T
328+
case is ByteBuffer.Type:
329+
return try await response.body.collect(upTo: Int.max) as! T
321330
default:
322-
var message = ""
323-
var data = try await response.body.collect(upTo: Int.max)
324-
var type = ""
325-
326-
do {
327-
let dict = try JSONSerialization.jsonObject(with: data) as? [String: Any]
328-
329-
message = dict?["message"] as? String ?? response.status.reasonPhrase
330-
type = dict?["type"] as? String ?? ""
331-
} catch {
332-
message = data.readString(length: data.readableBytes)!
331+
let data = try await response.body.collect(upTo: Int.max)
332+
if data.readableBytes == 0 {
333+
return true as! T
333334
}
335+
let dict = try JSONSerialization.jsonObject(with: data) as? [String: Any]
334336

335-
throw AppwriteError(
336-
message: message,
337-
code: Int(response.status.code),
338-
type: type
339-
)
337+
return converter?(dict!) ?? dict! as! T
340338
}
341-
}
339+
default:
340+
var message = ""
341+
var data = try await response.body.collect(upTo: Int.max)
342+
var type = ""
343+
344+
do {
345+
let dict = try JSONSerialization.jsonObject(with: data) as? [String: Any]
342346

343-
if bufferSink == nil {
344-
let response = try await http.execute(
345-
request,
346-
timeout: .seconds(30)
347+
message = dict?["message"] as? String ?? response.status.reasonPhrase
348+
type = dict?["type"] as? String ?? ""
349+
} catch {
350+
message = data.readString(length: data.readableBytes)!
351+
}
352+
353+
throw AppwriteError(
354+
message: message,
355+
code: Int(response.status.code),
356+
type: type
347357
)
348-
return try await complete(with: response)
349358
}
350-
351-
let response = try await http.execute(
352-
request,
353-
timeout: .seconds(30)
354-
)
355-
return try await complete(with: response)
356359
}
357360

358361
func chunkedUpload<T>(
@@ -411,7 +414,7 @@ open class Client {
411414
while offset < size {
412415
let slice = (input.data as! ByteBuffer).getSlice(at: offset, length: Client.chunkSize)
413416
?? (input.data as! ByteBuffer).getSlice(at: offset, length: Int(size - offset))
414-
417+
415418
params[paramName] = InputFile.fromBuffer(slice!, filename: input.filename, mimeType: input.mimeType)
416419
headers["content-range"] = "bytes \(offset)-\(min((offset + Client.chunkSize) - 1, size - 1))/\(size)"
417420

@@ -466,7 +469,12 @@ open class Client {
466469
|| param is [Bool: Any] {
467470
encodedParams[key] = param
468471
} else {
469-
encodedParams[key] = try! (param as! Encodable).toJson()
472+
let value = try! (param as! Encodable).toJson()
473+
474+
let range = value.index(value.startIndex, offsetBy: 1)..<value.index(value.endIndex, offsetBy: -1)
475+
let substring = value[range]
476+
477+
encodedParams[key] = substring
470478
}
471479
}
472480

@@ -603,24 +611,3 @@ extension Client {
603611
return device
604612
}
605613
}
606-
607-
extension Client {
608-
609-
public enum HTTPStatus: Int {
610-
case unknown = -1
611-
case ok = 200
612-
case created = 201
613-
case accepted = 202
614-
case movedPermanently = 301
615-
case found = 302
616-
case badRequest = 400
617-
case notAuthorized = 401
618-
case paymentRequired = 402
619-
case forbidden = 403
620-
case notFound = 404
621-
case methodNotAllowed = 405
622-
case notAcceptable = 406
623-
case internalServerError = 500
624-
case notImplemented = 501
625-
}
626-
}

Sources/Appwrite/Extensions/HTTPClientRequest+Cookies.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ extension HTTPClientRequest {
1717

1818
extension HTTPHeaders {
1919
public mutating func addDomainCookies(for domain: String) {
20-
guard let cookies = UserDefaults.standard.stringArray(forKey: "\(domain)-cookies") else {
20+
guard let cookies = UserDefaults.standard.stringArray(forKey: domain) else {
2121
return
2222
}
2323
for cookie in cookies {

Sources/Appwrite/OAuth/WebAuthComponent.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,10 @@ public class WebAuthComponent {
9292
cookie += "; secure"
9393
}
9494

95-
UserDefaults.standard.set([cookie], forKey: "\(domain)-cookies")
95+
let existing = UserDefaults.standard.stringArray(forKey: domain)
96+
let new = [cookie]
97+
98+
UserDefaults.standard.set(new, forKey: domain)
9699

97100
WebAuthComponent.onCallback(
98101
scheme: components.scheme!,

0 commit comments

Comments
 (0)