generated from coulomb/repo-seed
feat(WP-0010): IHF Phase 9 — External API Surface and Consumer SDKs
Some checks failed
Test / test (push) Has been cancelled
Some checks failed
Test / test (push) Has been cancelled
Delivers the full Phase 9 external API layer: - Versioned REST API (/api/v2/) with OpenAPI 3.1 spec; enum arrays for widget_type, event_type, annotation category drawn live from registry tables - OAuth 2.0 client credentials flow (/api/v2/token); hub:*:write scopes gated on active HubCapabilityManifest FK - API key management: SHA256-hashed tokens, key_prefix for display, one-time reveal on creation, revocation support - TypeScript and Python consumer SDKs generated from registry tables (/api/v2/sdk/ihf-client.ts, /api/v2/sdk/ihf-client.py) - Webhook delivery: HMAC-SHA256 signing, append-only webhook_deliveries, fire-and-forget dispatch via forkIO, 3-retry logic - Admin API dashboard with 24h stats (request count, error rate, last seen) - Rate limiting (per-minute) and daily quota enforcement via api_request_log - Schema migration: api_consumers, api_keys, webhook_subscriptions (CHECK constraint on 6 framework lifecycle topics), webhook_deliveries (append-only trigger), api_request_log - ARCHITECTURE-LAYERS.md scorecard: 3.34 → 3.41 (approaching Strong) - contracts/functional/interaction-reporting-v1.md extended with Phase 9 endpoint catalogue and 422 validation error format GAAF: no bare TEXT discriminators; webhook event_type uses CHECK constraint over 6 allowed framework lifecycle topic strings (not widget event types). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
88
Web/Controller/ApiConsumers.hs
Normal file
88
Web/Controller/ApiConsumers.hs
Normal file
@@ -0,0 +1,88 @@
|
||||
module Web.Controller.ApiConsumers where
|
||||
|
||||
import Web.Types
|
||||
import Web.View.ApiConsumers.Index
|
||||
import Web.View.ApiConsumers.Show
|
||||
import Web.View.ApiConsumers.New
|
||||
import Web.View.ApiConsumers.Edit
|
||||
import Generated.Types
|
||||
import IHP.Prelude
|
||||
import IHP.ControllerPrelude
|
||||
|
||||
instance Controller ApiConsumersController where
|
||||
beforeAction = ensureIsUser
|
||||
|
||||
action ApiConsumersAction = autoRefresh do
|
||||
consumers <- query @ApiConsumer
|
||||
|> orderByDesc #createdAt
|
||||
|> fetch
|
||||
render IndexView { consumers }
|
||||
|
||||
action ShowApiConsumerAction { apiConsumerId } = do
|
||||
consumer <- fetch apiConsumerId
|
||||
apiKeys <- query @ApiKey
|
||||
|> filterWhere (#apiConsumerId, consumer.id)
|
||||
|> orderByDesc #createdAt
|
||||
|> fetch
|
||||
webhooks <- query @WebhookSubscription
|
||||
|> filterWhere (#apiConsumerId, consumer.id)
|
||||
|> orderByAsc #eventType
|
||||
|> fetch
|
||||
mManifest <- case consumer.hubCapabilityManifestId of
|
||||
Nothing -> pure Nothing
|
||||
Just mId -> Just <$> fetch mId
|
||||
render ShowView { consumer, apiKeys, webhooks, mManifest }
|
||||
|
||||
action NewApiConsumerAction = do
|
||||
let consumer = newRecord @ApiConsumer
|
||||
manifests <- query @HubCapabilityManifest
|
||||
|> filterWhere (#status, "active")
|
||||
|> orderByAsc #createdAt
|
||||
|> fetch
|
||||
render NewView { consumer, manifests }
|
||||
|
||||
action CreateApiConsumerAction = do
|
||||
let consumer = newRecord @ApiConsumer
|
||||
consumer
|
||||
|> fill @["name", "description", "rateLimitPerMinute", "quotaPerDay"]
|
||||
|> ifValid \case
|
||||
Left consumerWithErrors -> do
|
||||
manifests <- query @HubCapabilityManifest
|
||||
|> filterWhere (#status, "active")
|
||||
|> fetch
|
||||
render NewView { consumer = consumerWithErrors, manifests }
|
||||
Right validConsumer -> do
|
||||
mManifestId <- paramOrNothing @(Id HubCapabilityManifest) "hubCapabilityManifestId"
|
||||
validConsumer
|
||||
|> set #hubCapabilityManifestId mManifestId
|
||||
|> createRecord
|
||||
redirectTo ApiConsumersAction
|
||||
|
||||
action EditApiConsumerAction { apiConsumerId } = do
|
||||
consumer <- fetch apiConsumerId
|
||||
manifests <- query @HubCapabilityManifest
|
||||
|> filterWhere (#status, "active")
|
||||
|> fetch
|
||||
render EditView { consumer, manifests }
|
||||
|
||||
action UpdateApiConsumerAction { apiConsumerId } = do
|
||||
consumer <- fetch apiConsumerId
|
||||
consumer
|
||||
|> fill @["name", "description", "rateLimitPerMinute", "quotaPerDay"]
|
||||
|> ifValid \case
|
||||
Left consumerWithErrors -> do
|
||||
manifests <- query @HubCapabilityManifest
|
||||
|> filterWhere (#status, "active")
|
||||
|> fetch
|
||||
render EditView { consumer = consumerWithErrors, manifests }
|
||||
Right validConsumer -> do
|
||||
mManifestId <- paramOrNothing @(Id HubCapabilityManifest) "hubCapabilityManifestId"
|
||||
validConsumer
|
||||
|> set #hubCapabilityManifestId mManifestId
|
||||
|> updateRecord
|
||||
redirectTo (ShowApiConsumerAction apiConsumerId)
|
||||
|
||||
action DeactivateApiConsumerAction { apiConsumerId } = do
|
||||
consumer <- fetch apiConsumerId
|
||||
consumer |> set #isActive False |> updateRecord
|
||||
redirectTo ApiConsumersAction
|
||||
Reference in New Issue
Block a user