@@ -16,26 +16,30 @@ open class Client {
1616
1717 open var endPoint = " https://HOSTNAME/v1 "
1818
19+ open var endPointRealtime : String ? = nil
20+
1921 open var headers : [ String : String ] = [
20- " content-type " : " application/json " ,
22+ " content-type " : " " ,
2123 " x-sdk-name " : " Swift " ,
2224 " x-sdk-platform " : " server " ,
2325 " x-sdk-language " : " swift " ,
24- " x-sdk-version " : " 5.0.0-rc.1 " ,
25- " x-appwrite-response-format " : " 1.4.0 "
26+ " x-sdk-version " : " 4.1.0 " ,
27+ " X-Appwrite-Response-Format " : " 1.4.0 "
2628 ]
2729
28- internal var config : [ String : String ] = [ : ]
30+ open var config : [ String : String ] = [ : ]
2931
30- internal var selfSigned : Bool = false
32+ open var selfSigned : Bool = false
3133
32- internal var http : HTTPClient
34+ open var http : HTTPClient
3335
34- private static let boundaryChars = " abcdefghijklmnopqrstuvwxyz1234567890 "
36+ private static let boundaryChars =
37+ " abcdefghijklmnopqrstuvwxyz1234567890 "
3538
3639 private static let boundary = randomBoundary ( )
3740
38- private static var eventLoopGroupProvider = HTTPClient . EventLoopGroupProvider. singleton
41+ private static var eventLoopGroupProvider =
42+ HTTPClient . EventLoopGroupProvider. createNew
3943
4044 // MARK: Methods
4145
@@ -145,75 +149,49 @@ open class Client {
145149 return self
146150 }
147151
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- }
162152
163153 ///
164- /// Set ForwardedFor
165- ///
166- /// The IP address of the client that made the request
154+ /// Set self signed
167155 ///
168- /// @param String value
156+ /// @param Bool status
169157 ///
170158 /// @return Client
171159 ///
172- open func setForwardedFor( _ value: String ) -> Client {
173- config [ " forwardedfor " ] = value
174- _ = addHeader ( key: " X-Forwarded-For " , value: value)
160+ open func setSelfSigned( _ status: Bool = true ) -> Client {
161+ self . selfSigned = status
162+ try ! http. syncShutdown ( )
163+ http = Client . createHTTP ( selfSigned: status)
175164 return self
176165 }
177166
178167 ///
179- /// Set ForwardedUserAgent
180- ///
181- /// The user agent string of the client that made the request
168+ /// Set endpoint
182169 ///
183- /// @param String value
170+ /// @param String endPoint
184171 ///
185172 /// @return Client
186173 ///
187- open func setForwardedUserAgent( _ value: String ) -> Client {
188- config [ " forwardeduseragent " ] = value
189- _ = addHeader ( key: " X-Forwarded-User-Agent " , value: value)
190- return self
191- }
174+ open func setEndpoint( _ endPoint: String ) -> Client {
175+ self . endPoint = endPoint
192176
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+ }
193182
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)
205183 return self
206184 }
207185
208186 ///
209- /// Set endpoint
187+ /// Set realtime endpoint.
210188 ///
211189 /// @param String endPoint
212190 ///
213191 /// @return Client
214192 ///
215- open func setEndpoint ( _ endPoint: String ) -> Client {
216- self . endPoint = endPoint
193+ open func setEndpointRealtime ( _ endPoint: String ) -> Client {
194+ self . endPointRealtime = endPoint
217195
218196 return self
219197 }
@@ -332,47 +310,64 @@ open class Client {
332310 withSink bufferSink: ( ( ByteBuffer ) -> Void ) ? = nil ,
333311 converter: ( ( Any ) -> T ) ? = nil
334312 ) async throws -> T {
335- let response = try await http. execute (
336- request,
337- timeout: . seconds( 30 )
338- )
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 ]
339333
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
334+ return converter ? ( dict!) ?? dict! as! T
335+ }
347336 default :
348- let data = try await response. body. collect ( upTo: Int . max)
349- if data. readableBytes == 0 {
350- return true as! T
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) !
351348 }
352- let dict = try JSONSerialization . jsonObject ( with: data) as? [ String : Any ]
353349
354- return converter ? ( dict!) ?? dict! as! T
355- }
356- default :
357- var message = " "
358- var data = try await response. body. collect ( upTo: Int . max)
359- var type = " "
360-
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) !
350+ throw AppwriteError (
351+ message: message,
352+ code: Int ( response. status. code) ,
353+ type: type
354+ )
368355 }
356+ }
369357
370- throw AppwriteError (
371- message : message ,
372- code : Int ( response . status . code ) ,
373- type : type
358+ if bufferSink == nil {
359+ let response = try await http . execute (
360+ request ,
361+ timeout : . seconds ( 30 )
374362 )
363+ return try await complete ( with: response)
375364 }
365+
366+ let response = try await http. execute (
367+ request,
368+ timeout: . seconds( 30 )
369+ )
370+ return try await complete ( with: response)
376371 }
377372
378373 func chunkedUpload< T> (
@@ -431,7 +426,7 @@ open class Client {
431426 while offset < size {
432427 let slice = ( input. data as! ByteBuffer ) . getSlice ( at: offset, length: Client . chunkSize)
433428 ?? ( input. data as! ByteBuffer ) . getSlice ( at: offset, length: Int ( size - offset) )
434-
429+
435430 params [ paramName] = InputFile . fromBuffer ( slice!, filename: input. filename, mimeType: input. mimeType)
436431 headers [ " content-range " ] = " bytes \( offset) - \( min ( ( offset + Client. chunkSize) - 1 , size - 1 ) ) / \( size) "
437432
@@ -486,12 +481,7 @@ open class Client {
486481 || param is [ Bool : Any ] {
487482 encodedParams [ key] = param
488483 } else {
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
484+ encodedParams [ key] = try ! ( param as! Encodable ) . toJson ( )
495485 }
496486 }
497487
@@ -628,3 +618,24 @@ extension Client {
628618 return device
629619 }
630620}
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