Created
January 14, 2021 13:45
-
-
Save hmhmsh/014039627c0a079a952e7b951fc46281 to your computer and use it in GitHub Desktop.
Example struct to generate api url
This file contains 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
protocol Api { | |
var path: String { get } | |
} | |
struct QiitaApi: Api { | |
var path: String { | |
protocolName + domain + "/" + version + "/" + directory | |
} | |
private let protocolName = "https://" | |
private var domain: String { | |
#if DEBUG | |
return "staging-qiita.com" | |
#elseif RELEASE | |
return "qiita.com" | |
#endif | |
} | |
private var version: String { | |
"api/v2" | |
} | |
let directory: String | |
} | |
extension QiitaApi { | |
static var items: QiitaApi { | |
.init(directory: "items") | |
} | |
/* | |
* Add other pattern | |
*/ | |
} | |
/* | |
* DataStore Example | |
*/ | |
func load<T: Api>(api: T) { | |
guard let url = URL(string: api.path) else { | |
return | |
} | |
// URLSession etc. | |
} | |
/* | |
* Usage | |
*/ | |
load(api: QiitaApi.items) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment