Skip to content

Instantly share code, notes, and snippets.

@Slowhand0309
Last active October 10, 2020 16:56
Show Gist options
  • Save Slowhand0309/44f963cea2f108cc956434ba19b0a31e to your computer and use it in GitHub Desktop.
Save Slowhand0309/44f963cea2f108cc956434ba19b0a31e to your computer and use it in GitHub Desktop.
[APIKitのBasic認証] #iOS
import APIKit
import Foundation
protocol APIRequest: Request {}
// https://github.com/ishkawa/APIKit/issues/121
// https://stackoverflow.com/questions/47983026/error-copying-matching-creds-swift-rest-api-call
extension APIRequest {
var baseURL: URL {
return URL(string: "https://api.xxx.com")!
}
func intercept(urlRequest: URLRequest) throws -> URLRequest {
var urlRequest = urlRequest
urlRequest.timeoutInterval = 10.0
// Basic Auth
let plain = "username:userpass"
let plainData = plain.data(using: .utf8)
if let encodedString = plainData?.base64EncodedString() {
urlRequest.addValue("Basic \(encodedString)", forHTTPHeaderField: "Authorization")
}
return urlRequest
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment