@@ -14,32 +14,28 @@ open class Client {
1414 // MARK: Properties
1515 public static var chunkSize = 5 * 1024 * 1024 // 5MB
1616
17- open var endPoint = " https://HOSTNAME/v1 "
18-
19- open var endPointRealtime : String ? = nil
17+ open var endPoint = " https://cloud.appwrite.io/v1 "
2018
2119 open var headers : [ String : String ] = [
22- " content-type " : " " ,
20+ " content-type " : " application/json " ,
2321 " x-sdk-name " : " Swift " ,
2422 " x-sdk-platform " : " server " ,
2523 " x-sdk-language " : " swift " ,
26- " x-sdk-version " : " 4.1.0 " ,
27- " X-Appwrite-Response-Format " : " 1.4.0 "
24+ " x-sdk-version " : " 5.0.0-rc.2 " ,
25+ " x-appwrite-response-format " : " 1.4.0 "
2826 ]
2927
30- open var config : [ String : String ] = [ : ]
28+ internal var config : [ String : String ] = [ : ]
3129
32- open var selfSigned : Bool = false
30+ internal var selfSigned : Bool = false
3331
34- open var http : HTTPClient
32+ internal var http : HTTPClient
3533
36- private static let boundaryChars =
37- " abcdefghijklmnopqrstuvwxyz1234567890 "
34+ private static let boundaryChars = " abcdefghijklmnopqrstuvwxyz1234567890 "
3835
3936 private static let boundary = randomBoundary ( )
4037
41- private static var eventLoopGroupProvider =
42- HTTPClient . EventLoopGroupProvider. createNew
38+ private static var eventLoopGroupProvider = HTTPClient . EventLoopGroupProvider. singleton
4339
4440 // MARK: Methods
4541
@@ -149,49 +145,75 @@ open class Client {
149145 return self
150146 }
151147
148+ ///
149+ /// Set Session
150+ ///
151+ /// The user session to authenticate with
152+ ///
153+ /// @param String value
154+ ///
155+ /// @return Client
156+ ///
157+ open func setSession( _ value: String ) -> Client {
158+ config [ " session " ] = value
159+ _ = addHeader ( key: " X-Appwrite-Session " , value: value)
160+ return self
161+ }
152162
153163 ///
154- /// Set self signed
164+ /// Set ForwardedFor
155165 ///
156- /// @param Bool status
166+ /// The IP address of the client that made the request
167+ ///
168+ /// @param String value
157169 ///
158170 /// @return Client
159171 ///
160- open func setSelfSigned( _ status: Bool = true ) -> Client {
161- self . selfSigned = status
162- try ! http. syncShutdown ( )
163- http = Client . createHTTP ( selfSigned: status)
172+ open func setForwardedFor( _ value: String ) -> Client {
173+ config [ " forwardedfor " ] = value
174+ _ = addHeader ( key: " X-Forwarded-For " , value: value)
164175 return self
165176 }
166177
167178 ///
168- /// Set endpoint
179+ /// Set ForwardedUserAgent
169180 ///
170- /// @param String endPoint
181+ /// The user agent string of the client that made the request
182+ ///
183+ /// @param String value
171184 ///
172185 /// @return Client
173186 ///
174- open func setEndpoint( _ endPoint: String ) -> Client {
175- self . endPoint = endPoint
187+ open func setForwardedUserAgent( _ value: String ) -> Client {
188+ config [ " forwardeduseragent " ] = value
189+ _ = addHeader ( key: " X-Forwarded-User-Agent " , value: value)
190+ return self
191+ }
176192
177- if ( self . endPointRealtime == nil && endPoint. starts ( with: " http " ) ) {
178- self . endPointRealtime = endPoint
179- . replacingOccurrences ( of: " http:// " , with: " ws:// " )
180- . replacingOccurrences ( of: " https:// " , with: " wss:// " )
181- }
182193
194+ ///
195+ /// Set self signed
196+ ///
197+ /// @param Bool status
198+ ///
199+ /// @return Client
200+ ///
201+ open func setSelfSigned( _ status: Bool = true ) -> Client {
202+ self . selfSigned = status
203+ try ! http. syncShutdown ( )
204+ http = Client . createHTTP ( selfSigned: status)
183205 return self
184206 }
185207
186208 ///
187- /// Set realtime endpoint.
209+ /// Set endpoint
188210 ///
189211 /// @param String endPoint
190212 ///
191213 /// @return Client
192214 ///
193- open func setEndpointRealtime ( _ endPoint: String ) -> Client {
194- self . endPointRealtime = endPoint
215+ open func setEndpoint ( _ endPoint: String ) -> Client {
216+ self . endPoint = endPoint
195217
196218 return self
197219 }
@@ -310,64 +332,47 @@ open class Client {
310332 withSink bufferSink: ( ( ByteBuffer ) -> Void ) ? = nil ,
311333 converter: ( ( Any ) -> T ) ? = nil
312334 ) async throws -> T {
313- func complete( with response: HTTPClientResponse ) async throws -> T {
314- switch response. status. code {
315- case 0 ..< 400 :
316- if response. headers [ " Set-Cookie " ] . count > 0 {
317- UserDefaults . standard. set (
318- response. headers [ " Set-Cookie " ] ,
319- forKey: URL ( string: request. url) !. host! + " -cookies "
320- )
321- }
322- switch T . self {
323- case is Bool . Type :
324- return true as! T
325- case is ByteBuffer . Type :
326- return try await response. body. collect ( upTo: Int . max) as! T
327- default :
328- let data = try await response. body. collect ( upTo: Int . max)
329- if data. readableBytes == 0 {
330- return true as! T
331- }
332- let dict = try JSONSerialization . jsonObject ( with: data) as? [ String : Any ]
335+ let response = try await http. execute (
336+ request,
337+ timeout: . seconds( 30 )
338+ )
333339
334- return converter ? ( dict!) ?? dict! as! T
335- }
340+ switch response. status. code {
341+ case 0 ..< 400 :
342+ switch T . self {
343+ case is Bool . Type :
344+ return true as! T
345+ case is ByteBuffer . Type :
346+ return try await response. body. collect ( upTo: Int . max) as! T
336347 default :
337- var message = " "
338- var data = try await response. body. collect ( upTo: Int . max)
339- var type = " "
340-
341- do {
342- let dict = try JSONSerialization . jsonObject ( with: data) as? [ String : Any ]
343-
344- message = dict ? [ " message " ] as? String ?? response. status. reasonPhrase
345- type = dict ? [ " type " ] as? String ?? " "
346- } catch {
347- message = data. readString ( length: data. readableBytes) !
348+ let data = try await response. body. collect ( upTo: Int . max)
349+ if data. readableBytes == 0 {
350+ return true as! T
348351 }
352+ let dict = try JSONSerialization . jsonObject ( with: data) as? [ String : Any ]
349353
350- throw AppwriteError (
351- message: message,
352- code: Int ( response. status. code) ,
353- type: type
354- )
354+ return converter ? ( dict!) ?? dict! as! T
355355 }
356- }
356+ default :
357+ var message = " "
358+ var data = try await response. body. collect ( upTo: Int . max)
359+ var type = " "
357360
358- if bufferSink == nil {
359- let response = try await http. execute (
360- request,
361- timeout: . seconds( 30 )
361+ do {
362+ let dict = try JSONSerialization . jsonObject ( with: data) as? [ String : Any ]
363+
364+ message = dict ? [ " message " ] as? String ?? response. status. reasonPhrase
365+ type = dict ? [ " type " ] as? String ?? " "
366+ } catch {
367+ message = data. readString ( length: data. readableBytes) !
368+ }
369+
370+ throw AppwriteError (
371+ message: message,
372+ code: Int ( response. status. code) ,
373+ type: type
362374 )
363- return try await complete ( with: response)
364375 }
365-
366- let response = try await http. execute (
367- request,
368- timeout: . seconds( 30 )
369- )
370- return try await complete ( with: response)
371376 }
372377
373378 func chunkedUpload< T> (
@@ -426,7 +431,7 @@ open class Client {
426431 while offset < size {
427432 let slice = ( input. data as! ByteBuffer ) . getSlice ( at: offset, length: Client . chunkSize)
428433 ?? ( input. data as! ByteBuffer ) . getSlice ( at: offset, length: Int ( size - offset) )
429-
434+
430435 params [ paramName] = InputFile . fromBuffer ( slice!, filename: input. filename, mimeType: input. mimeType)
431436 headers [ " content-range " ] = " bytes \( offset) - \( min ( ( offset + Client. chunkSize) - 1 , size - 1 ) ) / \( size) "
432437
@@ -481,7 +486,12 @@ open class Client {
481486 || param is [ Bool : Any ] {
482487 encodedParams [ key] = param
483488 } else {
484- encodedParams [ key] = try ! ( param as! Encodable ) . toJson ( )
489+ let value = try ! ( param as! Encodable ) . toJson ( )
490+
491+ let range = value. index ( value. startIndex, offsetBy: 1 ) ..< value. index ( value. endIndex, offsetBy: - 1 )
492+ let substring = value [ range]
493+
494+ encodedParams [ key] = substring
485495 }
486496 }
487497
@@ -618,24 +628,3 @@ extension Client {
618628 return device
619629 }
620630}
621-
622- extension Client {
623-
624- public enum HTTPStatus : Int {
625- case unknown = - 1
626- case ok = 200
627- case created = 201
628- case accepted = 202
629- case movedPermanently = 301
630- case found = 302
631- case badRequest = 400
632- case notAuthorized = 401
633- case paymentRequired = 402
634- case forbidden = 403
635- case notFound = 404
636- case methodNotAllowed = 405
637- case notAcceptable = 406
638- case internalServerError = 500
639- case notImplemented = 501
640- }
641- }
0 commit comments