@@ -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- }
0 commit comments