@@ -42,7 +42,6 @@ Just.get("http://httpbin.org/get", params:["page": 3])
4242
4343var r = Just . get ( " http://httpbin.org/get " , params: [ " page " : 3 ] )
4444// … "r" becomes available here
45-
4645//: However, Just doesn't force you to choose. The same request can be made
4746//: asynchronously like this
4847
@@ -98,7 +97,7 @@ r.headers["Content-Length"] == r.headers["cOnTeNt-LeNgTh"] // true
9897//: The original request is preserved as a *NSURLRequest*:
9998
10099r. request // NSURLRequest sent
101- r. request? . HTTPMethod // GET
100+ r. request? . httpMethod // GET
102101
103102//: When things aren't going so well:
104103let erronous = Just . get ( " http://httpbin.org/does/not/exist " ) // oops
@@ -145,23 +144,20 @@ Just.get("http://httpbin.org/status/302", allowRedirects:false).isPermanentRedir
145144
146145import Foundation
147146
148-
149-
150- let elonPhotoURL = NSBundle . mainBundle ( ) . URLForResource ( " elon " , withExtension: " jpg " ) ! // assume the file exist
151- let uploadResult = Just . post ( " http://httpbin.org/post " , files: [ " elon " : . URL( elonPhotoURL, nil ) ] ) // <== that's it
147+ let elonPhotoURL = Bundle . main. url ( forResource: " elon " , withExtension: " jpg " ) !
148+ let uploadResult = Just . post ( " http://httpbin.org/post " , files: [ " elon " : . url( elonPhotoURL, nil ) ] ) // <== that's it
152149print ( uploadResult. text ?? " " )
153150
154151//: Here a file is specified with an NSURL. Alternatively, a file can be a NSData or just a string. Although in both cases, a filename is needed.
155-
156- let someData = " Marco " . dataUsingEncoding ( NSUTF8StringEncoding) ! // this shouldn't fail
152+ let someData = " Marco " . data ( using: String . Encoding. utf8) ! // this shouldn't fail
157153
158154if let text = Just . post (
159155 " http://httpbin.org/post " ,
160156 files: [
161- " a " : . Data ( " marco.text " , someData, nil ) , // file #1, an NSData
162- " b " : . Text ( " polo.txt " , " Polo " , nil ) // file #2, a String
157+ " a " : . data ( " marco.text " , someData, nil ) , // file #1, an NSData
158+ " b " : . text ( " polo.txt " , " Polo " , nil ) // file #2, a String
163159 ]
164- ) . text {
160+ ) . text {
165161 print ( text)
166162}
167163
@@ -175,8 +171,8 @@ if let text = Just.post(
175171if let json = Just . post (
176172 " http://httpbin.org/post " ,
177173 data: [ " lastName " : " Musk " ] ,
178- files: [ " elon " : . URL ( elonPhotoURL, nil ) ]
179- ) . json as? [ String : AnyObject ] {
174+ files: [ " elon " : . url ( elonPhotoURL, nil ) ]
175+ ) . json as? [ String : AnyObject ] {
180176 print ( json [ " form " ] ?? [ : ] ) // lastName:Musk
181177 print ( json [ " files " ] ?? [ : ] ) // elon
182178}
@@ -227,16 +223,17 @@ Just.get("http://httpbin.org/delay/5", timeout:0.2).reason
227223//: When dealing with large files, you may be interested in knowing the progress
228224//: of their uploading or downloading. You can do that by supplynig a call back
229225//: to the parameter **asyncProgressHandler**.
226+
230227Just . post (
231228 " http://httpbin.org/post " ,
232- files: [ " large file " : . Text ( " or " , " pretend this is a large file " , nil ) ] ,
233- asyncProgressHandler: { ( p ) in
229+ files: [ " large file " : . text ( " or " , " pretend this is a large file " , nil ) ] ,
230+ asyncProgressHandler: { p in
234231 p. type // either .Upload or .Download
235232 p. bytesProcessed
236233 p. bytesExpectedToProcess
237234 p. percent
238235 }
239- ) { ( r ) in
236+ ) { r in
240237 // finished
241238}
242239
@@ -253,14 +250,15 @@ Just.post(
253250//: To change these settings, one must create a separate instance of Just instead of using the
254251//: default one. Doing so opens up the oppurtunity to customize NSURLSession in
255252//: powerful ways. A `JustSessionDefaults` can be used to provide some customization points:
253+ //
256254
257255let myJustDefaults = JustSessionDefaults (
258- JSONReadingOptions: . MutableContainers , // NSJSONSerialization reading options
259- JSONWritingOptions: . PrettyPrinted , // NSJSONSerialization writing options
256+ JSONReadingOptions: . mutableContainers , // NSJSONSerialization reading options
257+ JSONWritingOptions: . prettyPrinted , // NSJSONSerialization writing options
260258 headers: [ " OH " : " MY " ] , // headers to include in every request
261259 multipartBoundary: " Ju5tH77P15Aw350m3 " , // multipart post request boundaries
262- credentialPersistence: . None , // NSURLCredential persistence options
263- encoding: NSUTF8StringEncoding // en(de)coding for HTTP body
260+ credentialPersistence: . none , // NSURLCredential persistence options
261+ encoding: String . Encoding . utf8 // en(de)coding for HTTP body
264262)
265263
266264//: Just initializer accepts an `defaults` argement. Use it like this:
0 commit comments