Last active
October 10, 2020 16:56
-
-
Save Slowhand0309/44f963cea2f108cc956434ba19b0a31e to your computer and use it in GitHub Desktop.
[APIKitのBasic認証] #iOS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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