Advertisement
Guest User

Untitled

a guest
Jul 27th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.03 KB | None | 0 0
  1. func uploadPokemonOnAPI(){
  2.         let headers = ["Authorization":Session.getUser()!.authString]
  3.        
  4.         let typeIds = choosenTypes.map({"\($0.typeId)"})
  5.         let moveIds = choosenMoves.map({"\($0.moveID)"} )
  6.        
  7.  
  8.         let attributes = [
  9.             "name":nameTextField.text!,
  10.             "height":heightTextField.text!,
  11.             "weight":weightTextField.text!,
  12.             "order":"30",
  13.             "is_default":"1",
  14.             "gender_id":"1",
  15.             "base_experience":"20",
  16.             "type_ids":typeIds,
  17.             "move_ids": moveIds,
  18.             "description": descriptionTextView.text!
  19.         ]
  20.        
  21.             showSpinner()
  22.            
  23.             Alamofire.upload(.POST, "https://pokeapi.infinum.co/api/v1/pokemons", headers: headers, multipartFormData: {
  24.                 multipartFormData in
  25.                 if let image = self.pokePicImageView.image {
  26.                     if let imageData = UIImageJPEGRepresentation(image, 0.8) {
  27.                         multipartFormData.appendBodyPart(data: imageData, name: "data[attributes][image]", fileName: "file.jpeg", mimeType: "image/jpeg")
  28.                     }
  29.                 }
  30.                 for (key, value) in attributes {
  31.                     if value is [String] {
  32.                         do {
  33.                             let jsonData = try NSJSONSerialization.dataWithJSONObject(value, options: NSJSONWritingOptions.PrettyPrinted)
  34.                             let string = NSString(data: jsonData, encoding: NSUTF8StringEncoding)!
  35.                             multipartFormData.appendBodyPart(data: string.dataUsingEncoding(NSUTF8StringEncoding)!, name: "data[attributes][" + (key as! String) + "]")
  36.                         }
  37.                         catch let error as NSError{
  38.                             print(error.description)
  39.                         }
  40.                     }
  41.                     else {
  42.                        multipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: "data[attributes][" + (key as! String) + "]")
  43.                     }
  44.                 }
  45.                 }, encodingCompletion: {
  46.                     encodingResult in
  47.                     switch encodingResult {
  48.                     case .Success(let upload, _, _):
  49.                         upload.responseString(completionHandler: { (response) in
  50.                             if let data = response.data {
  51.                                 print(String(data: data, encoding: NSUTF8StringEncoding))
  52.                             }
  53.                             print(response.result)
  54.                         })
  55.                         self.pokemonUploadedDelegate?.onNewPokemonUploaded()
  56.                         self.showSpinnerWithSuccessStatus("Pokemon created succesfully!")
  57.                         self.performSelector(#selector(self.goToHomeScreen), withObject: nil, afterDelay: 0.5)
  58.                     case .Failure(let encodingError):
  59.                         print(encodingError)
  60.                     }
  61.             })
  62.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement