Skip to content

Instantly share code, notes, and snippets.

@huseyinbabal
Created May 22, 2023 13:33
Show Gist options
  • Save huseyinbabal/67c7b4896217e8b8147b6293b863305a to your computer and use it in GitHub Desktop.
Save huseyinbabal/67c7b4896217e8b8147b6293b863305a to your computer and use it in GitHub Desktop.
Keptn Client Implementation
// client.go
package keptn
import (
api "github.com/keptn/go-utils/pkg/api/utils/v2"
"time"
)
// Client Keptn client
type Client struct {
// API refers to Keptn client. https://github.com/keptn/go-utils
API *api.APISet
}
type GetEventsRequest struct {
Project string
FromTime time.Time
}
type Event struct {
ID string
Source string
Type string
Data Data
}
type Data struct {
Message string
Project string
Service string
Status string
Stage string
Result string
}
...
// NewClient initializes Keptn client
func NewClient(url, token string) (*Client, error) {
client, err := api.New(url, api.WithAuthToken(token))
if err != nil {
return nil, err
}
return &Client{
API: client,
}, nil
}
// Events returns only new events.
func (c *Client) Events(ctx context.Context, request *GetEventsRequest) ([]Event, error) {
fromTime := request.FromTime.UTC().Format(time.RFC3339)
var events []Event
res, err := c.API.Events().GetEvents(ctx, &api.EventFilter{
Project: request.Project,
FromTime: fromTime,
}, api.EventsGetEventsOptions{})
if err != nil {
return nil, err.ToError()
}
for _, ev := range res {
data := Data{}
err := ev.DataAs(&data)
if err != nil {
return nil, err
}
events = append(events, Event{
ID: ev.ID,
Source: *ev.Source,
Type: *ev.Type,
Data: data,
})
}
return events, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment