Skip to content

Commit

Permalink
feat(api): accept application/json content-type for ingest (#1856)
Browse files Browse the repository at this point in the history
  • Loading branch information
tothandras authored Nov 16, 2024
1 parent 16e4975 commit b76480c
Show file tree
Hide file tree
Showing 8 changed files with 1,695 additions and 1,463 deletions.
1,464 changes: 769 additions & 695 deletions api/api.gen.go

Large diffs are not rendered by default.

1,550 changes: 822 additions & 728 deletions api/client/go/client.gen.go

Large diffs are not rendered by default.

17 changes: 11 additions & 6 deletions api/client/node/schemas/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3535,6 +3535,13 @@ export interface components {
*/
usage: number
}
/**
* @description The body of the events request.
* Either a single event or a batch of events.
*/
IngestEventsBody:
| components['schemas']['Event']
| components['schemas']['Event'][]
/**
* @description An ingested event with optional validation error.
* @example {
Expand Down Expand Up @@ -8472,15 +8479,13 @@ export interface operations {
content: {
'application/cloudevents+json': components['schemas']['Event']
'application/cloudevents-batch+json': components['schemas']['Event'][]
'application/json': components['schemas']['IngestEventsBody']
}
}
responses: {
/** @description The request has succeeded. */
200: {
content: {
'application/cloudevents+json': components['schemas']['Event']
'application/cloudevents-batch+json': components['schemas']['Event'][]
}
/** @description There is no content to send for this request, but the headers may be useful. */
204: {
content: never
}
/** @description The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing). */
400: {
Expand Down
15 changes: 9 additions & 6 deletions api/client/web/src/client/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4341,6 +4341,11 @@ export interface components {
*/
usage: number
}
/** @description The body of the events request.
* Either a single event or a batch of events. */
IngestEventsBody:
| components['schemas']['Event']
| components['schemas']['Event'][]
/**
* @description An ingested event with optional validation error.
* @example {
Expand Down Expand Up @@ -10044,18 +10049,16 @@ export interface operations {
content: {
'application/cloudevents+json': components['schemas']['Event']
'application/cloudevents-batch+json': components['schemas']['Event'][]
'application/json': components['schemas']['IngestEventsBody']
}
}
responses: {
/** @description The request has succeeded. */
200: {
/** @description There is no content to send for this request, but the headers may be useful. */
204: {
headers: {
[name: string]: unknown
}
content: {
'application/cloudevents+json': components['schemas']['Event']
'application/cloudevents-batch+json': components['schemas']['Event'][]
}
content?: never
}
/** @description The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing). */
400: {
Expand Down
27 changes: 15 additions & 12 deletions api/openapi.cloud.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2540,19 +2540,10 @@ paths:
operationId: ingestEvents
parameters: []
description: Ingests an event or batch of events following the CloudEvents specification.
summary: Ingest events
summary: 'Ingest events '
responses:
'200':
description: The request has succeeded.
content:
application/cloudevents+json:
schema:
$ref: '#/components/schemas/Event'
application/cloudevents-batch+json:
schema:
type: array
items:
$ref: '#/components/schemas/Event'
'204':
description: 'There is no content to send for this request, but the headers may be useful. '
'400':
description: The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).
content:
Expand Down Expand Up @@ -2602,6 +2593,9 @@ paths:
type: array
items:
$ref: '#/components/schemas/Event'
application/json:
schema:
$ref: '#/components/schemas/IngestEventsBody'
/api/v1/features:
get:
operationId: listFeatures
Expand Down Expand Up @@ -11766,6 +11760,15 @@ components:
example: 100
description: The usage in the period
description: Usage Record
IngestEventsBody:
anyOf:
- $ref: '#/components/schemas/Event'
- type: array
items:
$ref: '#/components/schemas/Event'
description: |-
The body of the events request.
Either a single event or a batch of events.
IngestedEvent:
type: object
required:
Expand Down
27 changes: 15 additions & 12 deletions api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2540,19 +2540,10 @@ paths:
operationId: ingestEvents
parameters: []
description: Ingests an event or batch of events following the CloudEvents specification.
summary: Ingest events
summary: 'Ingest events '
responses:
'200':
description: The request has succeeded.
content:
application/cloudevents+json:
schema:
$ref: '#/components/schemas/Event'
application/cloudevents-batch+json:
schema:
type: array
items:
$ref: '#/components/schemas/Event'
'204':
description: 'There is no content to send for this request, but the headers may be useful. '
'400':
description: The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).
content:
Expand Down Expand Up @@ -2602,6 +2593,9 @@ paths:
type: array
items:
$ref: '#/components/schemas/Event'
application/json:
schema:
$ref: '#/components/schemas/IngestEventsBody'
/api/v1/features:
get:
operationId: listFeatures
Expand Down Expand Up @@ -11440,6 +11434,15 @@ components:
example: 100
description: The usage in the period
description: Usage Record
IngestEventsBody:
anyOf:
- $ref: '#/components/schemas/Event'
- type: array
items:
$ref: '#/components/schemas/Event'
description: |-
The body of the events request.
Either a single event or a batch of events.
IngestedEvent:
type: object
required:
Expand Down
30 changes: 26 additions & 4 deletions api/spec/src/events.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ interface Events {
@header contentType: "application/cloudevents+json",
@body body: Event,
): {
@header contentType: "application/cloudevents+json";
@body _: Event;
@statusCode _: 204;
} | CommonErrors;

@post
Expand All @@ -118,9 +117,32 @@ interface Events {
@header contentType: "application/cloudevents-batch+json",
@body body: Event[],
): {
@header contentType: "application/cloudevents-batch+json";
@body _: Event[];
@statusCode _: 204;
} | CommonErrors;

@post
@operationId("ingestEvents")
@summary("")
@sharedRoute
ingestEventsJson(
@header contentType: "application/json",
@body body: IngestEventsBody,
): {
@statusCode _: 204;
} | CommonErrors;
}

/**
* The body of the events request.
* Either a single event or a batch of events.
*/
@friendlyName("IngestEventsBody")
union IngestEventsBody {
@friendlyName("Event")
Event: Event,

@friendlyName("Events")
Events: Event[],
}

/**
Expand Down
28 changes: 28 additions & 0 deletions openmeter/ingest/ingestdriver/http_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,34 @@ func (d ingestEventsRequestDecoder) decode(ctx context.Context, r *http.Request)
contentType := r.Header.Get("Content-Type")

switch contentType {
case "application/json":
var apiRequest api.IngestEventsBody

err := json.NewDecoder(r.Body).Decode(&apiRequest)
if err != nil {
return req, ErrorInvalidEvent{
Err: err,
}
}

// Try to parse as a single event
e, err := apiRequest.AsEvent()
if err == nil {
req.Events = []event.Event{e}
} else {
// Try to parse as a batch of events
e, err := apiRequest.AsIngestEventsBody1()
if err == nil {
req.Events = e
}
}

// If we still don't have any events, return an error
if len(req.Events) == 0 {
return req, ErrorInvalidEvent{
Err: errors.New("no events found"),
}
}
case "application/cloudevents+json":
var apiRequest api.IngestEventsApplicationCloudeventsPlusJSONRequestBody

Expand Down

0 comments on commit b76480c

Please sign in to comment.