Skip to content

Instantly share code, notes, and snippets.

@hmhmsh
Created January 14, 2021 13:45
Show Gist options
  • Save hmhmsh/014039627c0a079a952e7b951fc46281 to your computer and use it in GitHub Desktop.
Save hmhmsh/014039627c0a079a952e7b951fc46281 to your computer and use it in GitHub Desktop.
Example struct to generate api url
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