jokeapi/api
Types
Represents errors that can occur while interacting with the JokeAPI. This type encapsulates different error scenarios that can arise during decoding or making requests to the API.
pub type APIError {
DecodeError(reason: String)
GenericError(reason: String)
RequestError(reason: String)
}
Constructors
-
DecodeError(reason: String)
-
GenericError(reason: String)
-
RequestError(reason: String)
Represents the flags associated with a joke, indicating various sensitivities.
pub type Flags {
Flags(
nsfw: Bool,
religious: Bool,
political: Bool,
racist: Bool,
sexist: Bool,
explicit: Bool,
)
}
Constructors
-
Flags( nsfw: Bool, religious: Bool, political: Bool, racist: Bool, sexist: Bool, explicit: Bool, )
Represents a joke response from the JokeAPI.
pub type JokeResponse {
JokeResponse(
id: Int,
category: String,
flags: Flags,
safe: Bool,
lang: String,
type_: String,
setup: String,
delivery: String,
error: Bool,
)
}
Constructors
-
JokeResponse( id: Int, category: String, flags: Flags, safe: Bool, lang: String, type_: String, setup: String, delivery: String, error: Bool, )
Values
pub fn api_decoder(in: String) -> Result(JokeResponse, APIError)
Decodes a JSON string into a JokeResponse, handling potential decoding errors.
This function attempts to parse a JSON string and convert it into a JokeResponse using a predefined decoder. It maps any JSON decoding errors to APIError variants.
Arguments
- in: A JSON string representing a joke response
Returns
A Result containing either a successfully decoded JokeResponse or an APIError if decoding fails
pub fn display_joke(joke: JokeResponse) -> Nil
Displays a joke in a formatted manner.
pub fn get_any() -> Result(JokeResponse, APIError)
Fetches a random joke from the JokeAPI. This function constructs a URI for the “Any” endpoint of the JokeAPI, sends a request to that endpoint, and returns a JokeResponse.
pub fn get_joke_from_category(
category: String,
) -> Result(JokeResponse, APIError)
Fetches a joke in a specific category from the JokeAPI. This function constructs a URI for the “Programming” endpoint of the JokeAPI, sends a request to that endpoint, and returns a JokeResponse.