Compare commits
No commits in common. "9ffcf68695be31ac99182ee52e8098955dfefacb" and "f6c4d634ab026e400f038dd8bcd385d496f13bc2" have entirely different histories.
9ffcf68695
...
f6c4d634ab
@ -26,55 +26,3 @@ fn main = jsonClient(
|
|||||||
get '/greeting'
|
get '/greeting'
|
||||||
map || ( it.body.greeting )
|
map || ( it.body.greeting )
|
||||||
fold println
|
fold println
|
||||||
|
|
||||||
pub int HttpReqOpts {
|
|
||||||
headers: Map<String, Any>
|
|
||||||
}
|
|
||||||
|
|
||||||
pub int GetOpts : HttpReqOpts
|
|
||||||
|
|
||||||
pub int HttpClient {
|
|
||||||
baseUrl: String
|
|
||||||
fn get(path: String, opts?: GetOpts): IOEither<HttpRequestError, HttpResponse>
|
|
||||||
fn post(path: String, body: Any, opts: PostOpts): IOEither<HttpRequestError, HttpResponse>
|
|
||||||
}
|
|
||||||
|
|
||||||
pub int JsonClient : HttpClient
|
|
||||||
|
|
||||||
pub int JsonClientOpts {
|
|
||||||
baseUrl: String
|
|
||||||
fn getAuth(#[self] client: JsonClient) -> HttpClientAuth
|
|
||||||
}
|
|
||||||
|
|
||||||
pub int HttpClientAuth = (reqBuilder: HttpRequest::Builder) -> Void
|
|
||||||
|
|
||||||
impl : JsonClient {
|
|
||||||
baseUrl: String
|
|
||||||
fld auth: HttpClientAuth
|
|
||||||
|
|
||||||
ctor (opts: JsonClientOpts) {
|
|
||||||
self.baseUrl = opts.baseUrl
|
|
||||||
self.auth = opts.getAuth(self)
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fn get(path, opts) = IOEither::of || (
|
|
||||||
HttpRequest::builder() tap || {
|
|
||||||
url = baseUrl + path
|
|
||||||
method = 'GET'
|
|
||||||
headers = opts?.headers ?: [:]
|
|
||||||
cookies = opts?.cookies ?: [:]
|
|
||||||
auth(self)
|
|
||||||
}()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn jsonClient(opts: JsonClientOpts): JsonClient = JsonClientImpl(opts)
|
|
||||||
|
|
||||||
pub fn bearerToken(tokenGetter: (#[self] client: JsonClient) -> IOEither<HttpRequestError, String>) {
|
|
||||||
|client: JsonClient| {
|
|
||||||
let token = tokenGetter.hydrate(self: client)().unwrap()
|
|
||||||
|reqBuilder: HttpRequest::Builder| {
|
|
||||||
reqBuilder.headers['Authorization'] = "Bearer $token"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user