Advertisement
Guest User

meh

a guest
Jul 15th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.93 KB | None | 0 0
  1. class ViewController: UIViewController {
  2.  
  3.     var urlSession = URLSession(configuration: .default)
  4.    
  5.     override func viewDidLoad() {
  6.         super.viewDidLoad()
  7.        
  8.         authorize(withCode: "REMOVED") { (error) in
  9.             print(error)
  10.         }
  11.     }
  12.    
  13.    
  14.     func buildURL(appendingPath: String, withGetParameters: [String: String]) -> URL {
  15.        
  16.         var items = Array<URLQueryItem>()
  17.         var components = URLComponents(string: appendingPath)!
  18.        
  19.         for key in withGetParameters {
  20.             let queryItem = URLQueryItem(name: key.0, value: key.1)
  21.             items.append(queryItem)
  22.         }
  23.  
  24.         components.queryItems = items;
  25.         return components.url!
  26.     }
  27.    
  28.     func buildPOSTTask(onURLSession urlSession: URLSession, appendingPath path: String, withPostParameters postParams: [String : String]?, getParameters getParams: [String : String]?, httpHeaders: [String : String]?, completionHandler completion: (Data?, URLResponse?, NSError?) -> Void) -> URLSessionUploadTask {
  29.         let fullURL: URL
  30.         if let gets = getParams {
  31.             fullURL = buildURL(appendingPath: "https://anilist.co/api/" + path, withGetParameters: gets)
  32.         }
  33.         else {
  34.             fullURL = URL(string: path, relativeTo: URL(string: "https://anilist.co/api")!)!
  35.         }
  36.        
  37.         var request = URLRequest(url: fullURL)
  38.         request.httpMethod = "POST"
  39.        
  40.         var postParameters: Data? = nil
  41.        
  42.         if let posts = postParams {
  43.             do {
  44.                 postParameters = try JSONSerialization.data(withJSONObject: posts, options: [])
  45.             } catch let error as NSError {
  46.                 fatalError("[\(#function) \(#line)]: Could not build POST task: \(error.localizedDescription)")
  47.             }
  48.         }
  49.  
  50.         return urlSession.uploadTask(with: request, from: postParameters, completionHandler: completion)
  51.     }
  52.    
  53.     func authorize(withCode code: String?, completion: (NSError?) -> Void) {
  54.        
  55.         let body: [String: String] = ["client_secret": "REMOVED",
  56.                                       "grant_type": "authorization_code",
  57.                                       "redirect_uri": "genericwebsitethatshouldntexist.bo",
  58.                                       "client_id": "ibanez-hod6w",
  59.                                       "code": code ?? "REMOVED"];
  60.        
  61.         let obtainTokenTask = buildPOSTTask(onURLSession: self.urlSession, appendingPath: "auth/access_token", withPostParameters: nil, getParameters: body, httpHeaders: nil) { (data, response, error) in
  62.             if let err = error {
  63.                 completion(err)
  64.             } else {
  65.                 print("Response is \(response)")
  66.                 completion(nil)
  67.             }
  68.         }
  69.        
  70.         obtainTokenTask.resume()
  71.     }
  72.  
  73.     override func didReceiveMemoryWarning() {
  74.         super.didReceiveMemoryWarning()
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement