Exemple of the handling of a http request : Is there a way to abstract away the store definition ? (or « can a method return an object of type Store?) #566
-
Exemple of the handling of a http request : Is there a way to abstract away the store definition ? More precisely, in this example Question : Is there an approach, using Mint, to a avoid rewriting the snippet hereunder over and over again (one for each URL), like a function taking a URL as an argument an returning the corresponding "store" object? Alternatively, does the "constructor" of a store object accept parameters (say, like the constructor of a class in Crystal, for instance)? Beyond those questions lies really the question of the verbosity of the http request exemple from the blog. This example ought probably to be rewritten owing to the following changes in the syntax: fun makeRequest : Promise(String) {
result =
await Http.send(Http.get("https://www.example.com/"))
case (result) {
Result::Ok(response) => response.body
Result::Err => "Something went wrong!"
}
} store StarWars {
state status : Status = Status::Initial
fun load : Promise(Never, Void) {
sequence {
next { status = Status::Loading }
response =
"https://swapi.dev/api/planets"
|> Http.get()
|> Http.send()
object =
response.body
|> Json.parse()
|> Maybe.toResult("")
decodedResults =
decode object as Data
next { status = Status::Ok(decodedResults) }
} catch Http.ErrorResponse => error {
next { status = Status::Error("Something went wrong with the request.") }
} catch Object.Error => error {
next { status = Status::Error("The data is not what is expected.") }
} catch String => error {
next { status = Status::Error("Invalid JSON data.") }
}
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
The mint-realworld repository contains a reusable version of HTTP call: https://github.com/mint-lang/mint-realworld/blob/master/source/Api.mint Basically there is a type
There is no way to make a store generic, the way to make it abstract is to base it around a custom type (see the previous response).
Well verbosity is kind of necessary to handle all the erroneous branches, but with the syntax changes it would look a little better: fun makeRequest : Promise(String) {
result =
await Http.send(Http.get("https://www.example.com/"))
case (result) {
Result::Err => Result::Err("Something went wrong!")
Result::Ok(response) =>
case (Json.parse(response.body) {
Maybe::Nothing => Result::Err("could not parse json")
Maybe::Just(object) =>
case (decode object as Data) {
Result::Err => Result::Err("Could not decode")
Result::Ok(decoded) => Result::Ok(decoded)
}
}
}
} |
Beta Was this translation helpful? Give feedback.
-
The version with the new syntax looks fantastic ! |
Beta Was this translation helpful? Give feedback.
The mint-realworld repository contains a reusable version of HTTP call: https://github.com/mint-lang/mint-realworld/blob/master/source/Api.mint Basically there is a type
Api.Status(a)
and a module that has functions that relate to that type.