subdomain

an extension to a domain name used to organize resources together

https://api.themoviedb.org/3/authentication/token/new?api_key=ENTER_YOUR_API_KEY_HERE

extension AppDelegate {
	
	func tmdbURLFromParameters{_ paramters: [String:AnyObject], withPathExtension: String? = nil) -> URL {

		var components = URLComponents()
		components.scheme = Constants.TMDB.ApiScheme
		components.host = Constants.TMDB.ApiHost
		components.path = Constants.TMDB.ApiPath + (withPathExtension ?? "")
		components.queryItems = [URLQueryItem]()

		for (key, value) in parameters {
			let queryUtem = URLQueryItem(name: key, value: "\(value)")
			components.queryItems!.append(queryItem)
		}
		return components.url!
	}}
}
private func getRequestToken() {

    /* TASK: Get a request token, then store it (appDelegate.requestToken) and login with the token */

    /* 1. Set the parameters */
    let methodParameters = [
        Constants.TMDBParameterKeys.ApiKey: Constants.TMDBParameterValues.ApiKey
    ]

    /* 2/3. Build the URL, Configure the request */
    let request = URLRequest(url: appDelegate.tmdbURLFromParameters(methodParameters as [String:AnyObject], withPathExtension: "/authentication/token/new"))

    /* 4. Make the request */
    let task = appDelegate.sharedSession.dataTask(with: request) { (data, response, error) in

        // if an error occurs, print it and re-enable the UI
        func displayError(_ error: String) {
            print(error)
            performUIUpdatesOnMain {
                self.setUIEnabled(true)
                self.debugTextLabel.text = "Login Failed (Request Token)."
            }
        }

        /* GUARD: Was there an error? */
        guard (error == nil) else {
            displayError("There was an error with your request: \(error)")
            return
        }

        /* GUARD: Did we get a successful 2XX response? */
        guard let statusCode = (response as? HTTPURLResponse)?.statusCode, statusCode >= 200 && statusCode <= 299 else {
            displayError("Your request returned a status code other than 2xx!")
            return
        }

        /* GUARD: Was there any data returned? */
        guard let data = data else {
            displayError("No data was returned by the request!")
            return
        }

        /* 5. Parse the data */
        let parsedResult: [String:AnyObject]!
        do {
            parsedResult = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as! [String:AnyObject]
        } catch {
            displayError("Could not parse the data as JSON: '\(data)'")
            return
        }

        /* GUARD: Did TheMovieDB return an error? */
        if let _ = parsedResult[Constants.TMDBResponseKeys.StatusCode] as? Int {
            displayError("TheMovieDB returned an error. See the '\(Constants.TMDBResponseKeys.StatusCode)' and '\(Constants.TMDBResponseKeys.StatusMessage)' in \(parsedResult)")
            return
        }

        /* GUARD: Is the "request_token" key in parsedResult? */
        guard let requestToken = parsedResult[Constants.TMDBResponseKeys.RequestToken] as? String else {
            displayError("Cannot find key '\(Constants.TMDBResponseKeys.RequestToken)' in \(parsedResult)")
            return
        }

        /* 6. Use the data! */
        self.appDelegate.requestToken = requestToken
        self.loginWithToken(self.appDelegate.requestToken!)
    }

    /* 7. Start the request */
    task.resume()
}