MCPcopy Create free account
hub / github.com/Noumena-Network/code / fetchUtilization

Function fetchUtilization

src/services/api/usage.ts:27–59  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

25}
26
27export async function fetchUtilization(): Promise<Utilization | null> {
28 const session = getAuthRuntime().getCurrentSession()
29
30 const isManagedSubscriber =
31 session.principalSource === 'managed_oauth' &&
32 session.subscription.subscriptionType != null
33 const hasProfileScope = session.scopes.includes('user:profile')
34
35 if (!isManagedSubscriber || !hasProfileScope) {
36 return {}
37 }
38
39 // Skip API call if the managed session is not currently usable to avoid 401s.
40 if (session.sessionState !== 'usable' || !session.accessToken) {
41 return null
42 }
43
44 const authResult = getAuthHeaders()
45 if (authResult.error) {
46 throw new Error(`Auth error: ${authResult.error}`)
47 }
48
49 const headers = {
50 'Content-Type': 'application/json',
51 'User-Agent': getNcodeUserAgent(),
52 ...authResult.headers,
53 }
54
55 return await getIdentityClient().fetchUtilization({
56 headers,
57 timeout: 5000, // 5 second timeout
58 })
59}

Callers 4

usage.test.tsFile · 0.70
UsageFunction · 0.50
runExtraUsageFunction · 0.50
checkOverageGateFunction · 0.50

Calls 5

getAuthRuntimeFunction · 0.85
getNcodeUserAgentFunction · 0.85
getIdentityClientFunction · 0.85
getCurrentSessionMethod · 0.65
getAuthHeadersFunction · 0.50

Tested by

no test coverage detected