@@ -14,32 +14,31 @@ open class Client {
14
14
// MARK: Properties
15
15
public static var chunkSize = 5 * 1024 * 1024 // 5MB
16
16
17
- open var endPoint = " https://HOSTNAME /v1 "
17
+ open var endPoint = " https://cloud.appwrite.io /v1 "
18
18
19
19
open var endPointRealtime : String ? = nil
20
20
21
21
open var headers : [ String : String ] = [
22
- " content-type " : " " ,
22
+ " content-type " : " application/json " ,
23
23
" x-sdk-name " : " Apple " ,
24
24
" x-sdk-platform " : " client " ,
25
25
" 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 "
28
28
]
29
29
30
- open var config : [ String : String ] = [ : ]
30
+ internal var config : [ String : String ] = [ : ]
31
31
32
- open var selfSigned : Bool = false
32
+ internal var selfSigned : Bool = false
33
33
34
- open var http : HTTPClient
34
+ internal var http : HTTPClient
35
35
36
- private static let boundaryChars =
37
- " abcdefghijklmnopqrstuvwxyz1234567890 "
36
+
37
+ private static let boundaryChars = " abcdefghijklmnopqrstuvwxyz1234567890 "
38
38
39
39
private static let boundary = randomBoundary ( )
40
40
41
- private static var eventLoopGroupProvider =
42
- HTTPClient . EventLoopGroupProvider. createNew
41
+ private static var eventLoopGroupProvider = HTTPClient . EventLoopGroupProvider. singleton
43
42
44
43
// MARK: Methods
45
44
@@ -80,7 +79,6 @@ open class Client {
80
79
decompression: . enabled( limit: . none)
81
80
)
82
81
)
83
-
84
82
}
85
83
86
84
deinit {
@@ -134,6 +132,21 @@ open class Client {
134
132
return self
135
133
}
136
134
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
+
137
150
138
151
///
139
152
/// Set self signed
@@ -295,64 +308,54 @@ open class Client {
295
308
withSink bufferSink: ( ( ByteBuffer ) -> Void ) ? = nil ,
296
309
converter: ( ( Any ) -> T ) ? = nil
297
310
) 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
+ )
318
315
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
321
330
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
333
334
}
335
+ let dict = try JSONSerialization . jsonObject ( with: data) as? [ String : Any ]
334
336
335
- throw AppwriteError (
336
- message: message,
337
- code: Int ( response. status. code) ,
338
- type: type
339
- )
337
+ return converter ? ( dict!) ?? dict! as! T
340
338
}
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 ]
342
346
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
347
357
)
348
- return try await complete ( with: response)
349
358
}
350
-
351
- let response = try await http. execute (
352
- request,
353
- timeout: . seconds( 30 )
354
- )
355
- return try await complete ( with: response)
356
359
}
357
360
358
361
func chunkedUpload< T> (
@@ -411,7 +414,7 @@ open class Client {
411
414
while offset < size {
412
415
let slice = ( input. data as! ByteBuffer ) . getSlice ( at: offset, length: Client . chunkSize)
413
416
?? ( input. data as! ByteBuffer ) . getSlice ( at: offset, length: Int ( size - offset) )
414
-
417
+
415
418
params [ paramName] = InputFile . fromBuffer ( slice!, filename: input. filename, mimeType: input. mimeType)
416
419
headers [ " content-range " ] = " bytes \( offset) - \( min ( ( offset + Client. chunkSize) - 1 , size - 1 ) ) / \( size) "
417
420
@@ -466,7 +469,12 @@ open class Client {
466
469
|| param is [ Bool : Any ] {
467
470
encodedParams [ key] = param
468
471
} 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
470
478
}
471
479
}
472
480
@@ -603,24 +611,3 @@ extension Client {
603
611
return device
604
612
}
605
613
}
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
- }
0 commit comments