Building and normalizing URI with URLComponents in Swift

Use URLComponents to construct long web addresses

var components = URLComponents()
components.scheme = "https"
components.host = "api.twitter.com"
components.path = "/2/oauth2/token"
components.queryItems = [
  URLQueryItem(name: "code", value: code.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!),
  URLQueryItem(name: "grant_type", value: "authorization_code"),
  URLQueryItem(name: "client_id", value: TwitterCredentials.CLIENT_ID.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!),
  URLQueryItem(name: "redirect_uri", value: TwitterCredentials.REDIRECT_URI.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!),
  URLQueryItem(name: "code_verifier", value: TwitterCredentials.CODE_VERIFIER.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)
]
var request = URLRequest(url: components.url!)
request.httpMethod = "POST"
request.allHTTPHeaderFields = [
  "Content-Type": "application/x-www-form-urlencoded"
]