Pudu programming language
Menu
Package

@chrismichaelps / pudu-lang-httpclient

Named HTTP clients for Pudu: a client factory, delegating handlers, pooled keep-alive connections, handler lifetimes, logging, and resilience

0.1.1Apache-2.01

InstallClose

Events.pudu

Pudu62 lines3.1 KB

GitHub ↗
1/** @HttpClient.Events.Module — what happens to requests and pipelines, and who listens */2module PuduLangHttpClient.Events34import Std.Json as Json56/** @HttpClient.Events.RequestStarted — a request entered a client's pipeline */7export type RequestStarted = { client: Str, method: Str, uri: Str } derives Json.Encode89/** @HttpClient.Events.RequestCompleted — a request left a client's pipeline with a response */10export type RequestCompleted = { client: Str, method: Str, uri: Str, status: Int, elapsed: Int } derives Json.Encode1112/** @HttpClient.Events.RequestFailed — a request left a client's pipeline without a response */13export type RequestFailed = { client: Str, method: Str, uri: Str, failure: Str, elapsed: Int } derives Json.Encode1415/** @HttpClient.Events.Stage — what happened to a handler pipeline */16export type Stage = Created | Expired | Closed derives Json.Encode1718/** @HttpClient.Events.PipelineChanged — a client's handler pipeline was created, expired, or closed */19export type PipelineChanged = { client: Str, generation: Int, stage: Stage } derives Json.Encode2021/** @HttpClient.Events.Listeners — the functions told about each kind of event, in the order added */22export type Listeners = {23  started: Array[fn(RequestStarted) -> ()],24  completed: Array[fn(RequestCompleted) -> ()],25  failed: Array[fn(RequestFailed) -> ()],26  changed: Array[fn(PipelineChanged) -> ()]27}2829/// No listeners.30export fn listeners() -> Listeners { Listeners{started: [], completed: [], failed: [], changed: []} }3132/// Whether anyone listens to requests.33export fn hearsRequests(given: &Listeners) -> Bool {34  !given.started.isEmpty() || !given.completed.isEmpty() || !given.failed.isEmpty()35}3637/** @HttpClient.Events.Listening — listeners added one at a time */38export trait Listening {39  /// Also tells `listen` about every request entering a pipeline.40  fn onRequestStarted(self: &Self, listen: fn(RequestStarted) -> ()) -> Self41  /// Also tells `listen` about every request answered with a response.42  fn onRequestCompleted(self: &Self, listen: fn(RequestCompleted) -> ()) -> Self43  /// Also tells `listen` about every request that failed.44  fn onRequestFailed(self: &Self, listen: fn(RequestFailed) -> ()) -> Self45  /// Also tells `listen` about every pipeline created, expired, or closed.46  fn onPipelineChanged(self: &Self, listen: fn(PipelineChanged) -> ()) -> Self47}4849impl Listening for Listeners {50  /// Also tells `listen` about every request entering a pipeline.51  fn onRequestStarted(self: &Self, listen: fn(RequestStarted) -> ()) -> Self { Listeners{..*self, started: self.started.push(listen)} }5253  /// Also tells `listen` about every request answered with a response.54  fn onRequestCompleted(self: &Self, listen: fn(RequestCompleted) -> ()) -> Self { Listeners{..*self, completed: self.completed.push(listen)} }5556  /// Also tells `listen` about every request that failed.57  fn onRequestFailed(self: &Self, listen: fn(RequestFailed) -> ()) -> Self { Listeners{..*self, failed: self.failed.push(listen)} }5859  /// Also tells `listen` about every pipeline created, expired, or closed.60  fn onPipelineChanged(self: &Self, listen: fn(PipelineChanged) -> ()) -> Self { Listeners{..*self, changed: self.changed.push(listen)} }61}62