8000 feat: add endpoint to REST API for creating an event, add UI for creating an event by abelanger5 · Pull Request #616 · hatchet-dev/hatchet · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: add endpoint to REST API for creating an event, add UI for creating an event #616

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api-contracts/openapi/components/schemas/_index.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ Event:
$ref: "./event.yaml#/Event"
EventData:
$ref: "./event.yaml#/EventData"
CreateEventRequest:
$ref: "./event.yaml#/CreateEventRequest"
EventWorkflowRunSummary:
$ref: "./event.yaml#/EventWorkflowRunSummary"
EventOrderByField:
Expand Down
15 changes: 15 additions & 0 deletions api-contracts/openapi/components/schemas/event.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ EventData:
required:
- data

CreateEventRequest:
properties:
key:
type: string
description: The key for the event.
data:
type: object
description: The data for the event.
additionalMetadata:
type: object
description: Additional metadata for the event.
required:
- key
- data

ReplayEventRequest:
properties:
eventIds:
Expand Down
49 changes: 49 additions & 0 deletions api-contracts/openapi/paths/event/event.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,55 @@ withTenant:
summary: List events
tags:
- Event
post:
x-resources: ["tenant"]
description: Creates a new event.
operationId: event:create
parameters:
- description: The tenant id
in: path
name: tenant
required: true
schema:
type: string
format: uuid
minLength: 36
maxLength: 36
requestBody:
content:
application/json:
schema:
$ref: "../../components/schemas/_index.yaml#/CreateEventRequest"
description: The event to create
required: true
responses:
"200":
content:
application/json:
schema:
$ref: "../../components/schemas/_index.yaml#/Event"
description: Successfully created the event
"400":
content:
application/json:
schema:
$ref: "../../components/schemas/_index.yaml#/APIErrors"
description: A malformed or bad request
"403":
content:
application/json:
schema:
$ref: "../../components/schemas/_index.yaml#/APIErrors"
description: Forbidden
"429":
content:
application/json:
schema:
$ref: "../../components/schemas/_index.yaml#/APIErrors"
description: Resource limit exceeded
summary: Create event
tags:
- Event

eventData:
get:
Expand Down
57 changes: 57 additions & 0 deletions api/v1/server/handlers/events/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package events

import (
"encoding/json"

"github.com/labstack/echo/v4"

"github.com/hatchet-dev/hatchet/api/v1/server/oas/apierrors"
"github.com/hatchet-dev/hatchet/api/v1/server/oas/gen"
"github.com/hatchet-dev/hatchet/api/v1/server/oas/transformers"
"github.com/hatchet-dev/hatchet/pkg/repository/metered"
"github.com/hatchet-dev/hatchet/pkg/repository/prisma/db"
"github.com/hatchet-dev/hatchet/pkg/repository/prisma/sqlchelpers"
)

func (t *EventService) EventCreate(ctx echo.Context, request gen.EventCreateRequestObject) (gen.EventCreateResponseObject, error) {
tenant := ctx.Get("tenant").(*db.TenantModel)

// marshal the data object to bytes
dataBytes, err := json.Marshal(request.Body.Data)

if err != nil {
return nil, err
}

var additionalMetadata []byte

if request.Body.AdditionalMetadata != nil {
additionalMetadata, err = json.Marshal(request.Body.AdditionalMetadata)

if err != nil {
return nil, err
}
}

newEvent, err := t.config.Ingestor.IngestEvent(ctx.Request().Context(), tenant.ID, request.Body.Key, dataBytes, additionalMetadata)

if err != nil {
if err == metered.ErrResourceExhausted {
return gen.EventCreate429JSONResponse(
apierrors.NewAPIErrors("Event limit exceeded"),
), nil
}

return nil, err
}

dbNewEvent, err := t.config.APIRepository.Event().GetEventById(sqlchelpers.UUIDToStr(newEvent.ID))

if err != nil {
return nil, err
}

return gen.EventCreate200JSONResponse(
*transformers.ToEvent(dbNewEvent),
), nil
}
Loading
Loading
0