~/bend-docscommunity

usage.bend relies on unsafe/foreign

raw source on the hub · import cswap-bend-cli@0.1.0.0/usage.bend as Usage

7 imports
import Base
import ./json.bend as Json
import ./clock.bend as Clock
import ./usage/norm.bend as UNorm
import ./usage/req.bend as UReq
import ./usage/cred.bend as UCred
import ./usage/gate.bend as UGate

Types

type Outcome source · line 23 · raw

Data

usage: the normalised result (JNull when there is none); error: the kind (http-429, http-403, timeout, network, bad-response, no-access-token, invalid_grant, no_refresh_token, invalid_client, refresh-failed, ...); retry: Retry-After seconds; struck: the credential text whose refresh token the server rejected for good (the store quarantines on it).

type Fetched source · line 28 · raw

Data

credential: the rotated credential when a refresh succeeded, for the store to persist.

Definitions

def done source · line 31 · raw

@o:Outcome -> @upd:Maybe<&2, String> -> IO(Fetched)

def out.ok source · line 34 · raw

@+data:0x0717344745a30ebad1f19045228e8f18/json.Json -> @+now:U32 -> @+off:U32 -> Outcome

def out.err source · line 37 · raw

@e:0x0717344745a30ebad1f19045228e8f18/usage/req.Err -> Outcome

def out.kind source · line 42 · raw

@k:String -> @struck:Maybe<&2, String> -> Outcome

def dead source · line 45 · raw

@+k:String -> Bool

def final.pick source · line 48 · raw

@r:Result<&2, &2, 0x0717344745a30ebad1f19045228e8f18/usage/req.Err, 0x0717344745a30ebad1f19045228e8f18/json.Json> -> @+now:U32 -> @+off:U32 -> @upd:Maybe<&2, String> -> IO(Fetched)

def deterministic source · line 57 · raw

@+k:String -> Bool

Refusals that will not resolve by retrying this pass: the caller must not fall through to the usage endpoint with the known-expired token.

def retry.k source · line 60 · raw

@+k:String -> String

def struck.pick source · line 63 · raw

@d:Bool -> @cred:String -> Maybe<&2, String>

def retry.struck source · line 70 · raw

@+k:String -> @+cred:String -> Maybe<&2, String>

def retry.tok source · line 73 · raw

@m:Maybe<&2, String> -> @new:String -> @+now:U32 -> @+off:U32 -> IO(Fetched)

def retry.fail source · line 81 · raw

@e:Maybe<&2, String> -> @consumed:Maybe<&2, String> -> @+cred:String -> @upd:Maybe<&2, String> -> IO(Fetched)

def retry.got source · line 88 · raw

@c:Maybe<&2, String> -> @e:Maybe<&2, String> -> @consumed:Maybe<&2, String> -> @+cred:String -> @+now:U32 -> @+off:U32 -> @upd:Maybe<&2, String> -> IO(Fetched)

def retry.pick source · line 98 · raw

@g:0x0717344745a30ebad1f19045228e8f18/usage/gate.Gated -> @+cred:String -> @+now:U32 -> @+off:U32 -> @upd:Maybe<&2, String> -> IO(Fetched)

After a 401 and a refresh: failures keep their identity only when the reference does (dead lineage, rejected client, consume-busy), else refresh-failed.

def fail.refresh source · line 103 · raw

@retry:Bool -> @e:0x0717344745a30ebad1f19045228e8f18/usage/req.Err -> @gate:0x0717344745a30ebad1f19045228e8f18/usage/gate.Gate -> @+cred:String -> @+now:U32 -> @+off:U32 -> @upd:Maybe<&2, String> -> IO(Fetched)

def is_401 source · line 110 · raw

@e:0x0717344745a30ebad1f19045228e8f18/usage/req.Err -> Bool

def fetch.after source · line 116 · raw

@r:Result<&2, &2, 0x0717344745a30ebad1f19045228e8f18/usage/req.Err, 0x0717344745a30ebad1f19045228e8f18/json.Json> -> @gate:0x0717344745a30ebad1f19045228e8f18/usage/gate.Gate -> @+cred:String -> @+o:0x0717344745a30ebad1f19045228e8f18/json.Json -> @+active:Bool -> @+now:U32 -> @+off:U32 -> @upd:Maybe<&2, String> -> IO(Fetched)

401 on an inactive account with a refresh token: refresh once, retry once.

def fetch.req source · line 123 · raw

@+gate:0x0717344745a30ebad1f19045228e8f18/usage/gate.Gate -> @+cred:String -> @+o:0x0717344745a30ebad1f19045228e8f18/json.Json -> @+token:String -> @+active:Bool -> @+now:U32 -> @+off:U32 -> @upd:Maybe<&2, String> -> @inf:Bool -> IO(Fetched)

def pre.stop source · line 127 · raw

@stop:Bool -> @+k:String -> @gate:0x0717344745a30ebad1f19045228e8f18/usage/gate.Gate -> @+cred:String -> @+o:0x0717344745a30ebad1f19045228e8f18/json.Json -> @+token:String -> @+active:Bool -> @+now:U32 -> @+off:U32 -> @consumed:Maybe<&2, String> -> IO(Fetched)

def pre.fail source · line 134 · raw

@e:Maybe<&2, String> -> @consumed:Maybe<&2, String> -> @gate:0x0717344745a30ebad1f19045228e8f18/usage/gate.Gate -> @+cred:String -> @+o:0x0717344745a30ebad1f19045228e8f18/json.Json -> @+token:String -> @+active:Bool -> @+now:U32 -> @+off:U32 -> IO(Fetched)

def pre.new source · line 145 · raw

@gate:0x0717344745a30ebad1f19045228e8f18/usage/gate.Gate -> @+new:String -> @+token:String -> @+active:Bool -> @+now:U32 -> @+off:U32 -> IO(Fetched)

Before the request: an expired token of an inactive account is refreshed. A dead lineage or a deterministic refusal stops here; a transient failure goes on with the old token. A refreshed token is a new token: the usage endpoint first, even for a setup-token scope.

def pre.got source · line 148 · raw

@c:Maybe<&2, String> -> @e:Maybe<&2, String> -> @consumed:Maybe<&2, String> -> @gate:0x0717344745a30ebad1f19045228e8f18/usage/gate.Gate -> @+cred:String -> @+o:0x0717344745a30ebad1f19045228e8f18/json.Json -> @+token:String -> @+active:Bool -> @+now:U32 -> @+off:U32 -> IO(Fetched)

def fetch.pre source · line 155 · raw

@g:0x0717344745a30ebad1f19045228e8f18/usage/gate.Gated -> @gate:0x0717344745a30ebad1f19045228e8f18/usage/gate.Gate -> @+cred:String -> @+o:0x0717344745a30ebad1f19045228e8f18/json.Json -> @+token:String -> @+active:Bool -> @+now:U32 -> @+off:U32 -> IO(Fetched)

def gate.pick source · line 160 · raw

@needs:Bool -> @+gate:0x0717344745a30ebad1f19045228e8f18/usage/gate.Gate -> @+cred:String -> @+o:0x0717344745a30ebad1f19045228e8f18/json.Json -> @+token:String -> @+active:Bool -> @+now:U32 -> @+off:U32 -> IO(Fetched)

def fetch.gate source · line 167 · raw

@+gate:0x0717344745a30ebad1f19045228e8f18/usage/gate.Gate -> @+cred:String -> @+o:0x0717344745a30ebad1f19045228e8f18/json.Json -> @token:String -> @+active:Bool -> @+now:U32 -> @+off:U32 -> IO(Fetched)

def fetch.start source · line 170 · raw

@m:Maybe<&2, String> -> @gate:0x0717344745a30ebad1f19045228e8f18/usage/gate.Gate -> @+cred:String -> @+o:0x0717344745a30ebad1f19045228e8f18/json.Json -> @+active:Bool -> @+now:U32 -> @+off:U32 -> IO(Fetched)

def offset.of source · line 178 · raw

@oe:Pair(String, String) -> U32

The local UTC offset, biased by 86400 (86400 = UTC when date is unusable).

def offset.run source · line 182 · raw

@r:Pair(U32, Pair(String, String)) -> U32

def offset.pick source · line 186 · raw

@r:Result<&1, &1, Pair(U32, String), Pair(U32, Pair(String, String))> -> U32

def local_offset source · line 193 · raw

IO(U32)

def fetch_with source · line 202 · raw

@gate:0x0717344745a30ebad1f19045228e8f18/usage/gate.Gate -> @+credential:String -> @is_active:Bool -> IO(Fetched)

fetch_usage plus the 401 refresh of try_fetch_usage_for_account, over the credential JSON text. Refreshes only inactive accounts. The rotated credential comes back in Fetched.credential for the store to persist. gate picks the refresh path: NoGate{} posts directly (tests); Gate{slot, email, root} runs it under the slot's consume lock.

def fetch_usage source · line 208 · raw

@credential:String -> @is_active:Bool -> IO(Fetched)

def fetch_usage_gated source · line 212 · raw

@credential:String -> @is_active:Bool -> @slot:String -> @email:String -> @root:String -> IO(Fetched)

The same, refreshing through the consume gate on <root>/credentials.