Skip to content

Commit 07022b1

Browse files
authored
Merge pull request #399 from hookdeck/chore/event-id-examples
chore: publish response contains an ID + example updates
2 parents 24c2a7b + 016fe45 commit 07022b1

File tree

7 files changed

+298
-99
lines changed

7 files changed

+298
-99
lines changed

docs/apis/openapi.yaml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,15 @@ components:
721721
description: Any JSON payload for the event data.
722722
additionalProperties: true
723723
example: { "user_id": "userid", "status": "active" }
724+
PublishResponse:
725+
type: object
726+
required:
727+
- id
728+
properties:
729+
id:
730+
type: string
731+
description: The ID of the event that was accepted for publishing. This will be the ID provided in the request's `id` field if present, otherwise it's a server-generated UUID.
732+
example: "evt_abc123xyz789"
724733
Event:
725734
type: object
726735
properties:
@@ -1382,7 +1391,11 @@ paths:
13821391
$ref: "#/components/schemas/PublishRequest"
13831392
responses:
13841393
"202":
1385-
description: Event accepted for publishing. Empty body.
1394+
description: Event accepted for publishing. Returns the event ID.
1395+
content:
1396+
application/json:
1397+
schema:
1398+
$ref: "#/components/schemas/PublishResponse"
13861399
"400":
13871400
description: Invalid request body.
13881401
"401":

examples/sdk-go/README.md

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
## Outpost Go SDK Example
22

3-
This example demonstrates using the Outpost Go SDK.
3+
This example demonstrates using the Outpost Go SDK. It is structured into a few files:
4+
* `main.go`: Handles command-line arguments to select which example to run.
5+
* `auth.go`: Contains examples related to authentication and tenant JWTs.
6+
* `resources.go`: Contains examples for managing Outpost resources like tenants and destinations.
47

58
The source code for the Go SDK can be found in the [`sdks/outpost-go/`](../../sdks/outpost-go/) directory.
69

@@ -19,20 +22,36 @@ The source code for the Go SDK can be found in the [`sdks/outpost-go/`](../../sd
1922
go mod tidy
2023
```
2124

22-
### Running the Example
25+
### Running the Examples
2326

2427
1. **Configure environment variables:**
2528
Create a `.env` file in this directory (`examples/sdk-go`) with the following:
2629
```dotenv
27-
SERVER_URL="your_server_url"
30+
SERVER_URL="your_outpost_server_url"
2831
ADMIN_API_KEY="your_admin_api_key"
2932
TENANT_ID="your_tenant_id"
3033
```
31-
Replace the placeholder values with your Outpost server URL, Admin API key, and Tenant ID. (Note: You'll need to create a `.gitignore` file if you don't want to commit `.env`).
32-
33-
2. **Run the example:**
34-
```bash
35-
go run main.go
36-
```
37-
38-
This executes `main.go`, which loads configuration from `.env` and performs a health check using the SDK. Review `main.go` for details.
34+
Replace the placeholder values with your Outpost server URL, Admin API key, and a Tenant ID to use for the examples.
35+
(Note: You'll need to create a `.gitignore` file if you don't want to commit `.env`).
36+
37+
* `SERVER_URL`: The base URL of your Outpost server (e.g., `http://localhost:3333`).
38+
* `ADMIN_API_KEY`: Your Outpost Admin API key.
39+
* `TENANT_ID`: An identifier for a tenant (e.g., `my_organization`). This is used by the `auth` example and parts of the `manage` example.
40+
41+
2. **Run a specific example:**
42+
The `main.go` program now accepts an argument to specify which example to run.
43+
44+
* **To run the resource management example (from `resources.go`):**
45+
This example demonstrates creating tenants, destinations, and publishing events.
46+
```bash
47+
go run . manage
48+
```
49+
50+
* **To run the authentication example (from `auth.go`):**
51+
This example demonstrates using the Admin API key to fetch a tenant JWT and then using that JWT.
52+
```bash
53+
go run . auth
54+
```
55+
56+
If you run `go run .` without an argument, or with an unknown argument, it will display a usage message.
57+
Review the respective `.go` files (`auth.go`, `resources.go`, `main.go`) for details on what each example does.

examples/sdk-go/auth.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"os"
8+
9+
outpostgo "github.com/hookdeck/outpost/sdks/outpost-go"
10+
"github.com/hookdeck/outpost/sdks/outpost-go/models/components"
11+
"github.com/joho/godotenv"
12+
)
13+
14+
func withJwt(ctx context.Context, jwt string, serverURL string, tenantID string) {
15+
log.Println("--- Running with Tenant JWT ---")
16+
17+
apiServerURL := fmt.Sprintf("%s/api/v1", serverURL)
18+
19+
jwtClient := outpostgo.New(
20+
outpostgo.WithSecurity(components.Security{
21+
TenantJwt: outpostgo.String(jwt),
22+
}),
23+
outpostgo.WithServerURL(apiServerURL),
24+
)
25+
26+
destRes, err := jwtClient.Destinations.List(ctx, outpostgo.String(tenantID), nil, nil)
27+
if err != nil {
28+
log.Fatalf("Failed to list destinations with JWT: %v", err)
29+
}
30+
31+
if destRes != nil && destRes.Destinations != nil {
32+
log.Printf("Successfully listed %d destinations using JWT.", len(destRes.Destinations))
33+
} else {
34+
log.Println("List destinations with JWT returned no data or an unexpected response structure.")
35+
}
36+
}
37+
38+
func withAdminApiKey(ctx context.Context, serverURL string, adminAPIKey string, tenantID string) {
39+
log.Println("--- Running with Admin API Key ---")
40+
41+
apiServerURL := fmt.Sprintf("%s/api/v1", serverURL)
42+
43+
adminClient := outpostgo.New(
44+
outpostgo.WithSecurity(components.Security{
45+
AdminAPIKey: outpostgo.String(adminAPIKey),
46+
}),
47+
outpostgo.WithServerURL(apiServerURL),
48+
)
49+
50+
healthRes, err := adminClient.Health.Check(ctx)
51+
if err != nil {
52+
log.Fatalf("Health check failed: %v", err)
53+
}
54+
55+
if healthRes != nil && healthRes.Res != nil {
56+
log.Printf("Health check successful. Details: %s", *healthRes.Res)
57+
} else {
58+
log.Println("Health check returned no data or an unexpected response structure.")
59+
}
60+
61+
destRes, err := adminClient.Destinations.List(ctx, outpostgo.String(tenantID), nil, nil)
62+
if err != nil {
63+
log.Fatalf("Failed to list destinations with Admin Key: %v", err)
64+
}
65+
66+
if destRes != nil && destRes.Destinations != nil {
67+
log.Printf("Successfully listed %d destinations using Admin Key for tenant %s.", len(destRes.Destinations), tenantID)
68+
} else {
69+
log.Println("List destinations with Admin Key returned no data or an unexpected response structure.")
70+
}
71+
72+
tokenRes, err := adminClient.Tenants.GetToken(ctx, outpostgo.String(tenantID))
73+
if err != nil {
74+
log.Fatalf("Failed to get tenant token: %v", err)
75+
}
76+
77+
if tokenRes != nil && tokenRes.TenantToken != nil && tokenRes.TenantToken.Token != nil {
78+
log.Printf("Successfully obtained tenant JWT for tenant %s.", tenantID)
79+
withJwt(ctx, *tokenRes.TenantToken.Token, serverURL, tenantID)
80+
} else {
81+
log.Println("Get tenant token returned no data or an unexpected response structure.")
82+
}
83+
}
84+
85+
// Renamed main to runAuthExample to avoid conflict
86+
func runAuthExample() {
87+
err := godotenv.Load()
88+
if err != nil {
89+
log.Println("No .env file found, proceeding without it")
90+
}
91+
92+
serverURL := os.Getenv("SERVER_URL")
93+
adminAPIKey := os.Getenv("ADMIN_API_KEY")
94+
tenantID := os.Getenv("TENANT_ID")
95+
96+
if serverURL == "" {
97+
log.Fatal("SERVER_URL environment variable not set")
98+
}
99+
if adminAPIKey == "" {
100+
log.Fatal("ADMIN_API_KEY environment variable not set")
101+
}
102+
if tenantID == "" {
103+
log.Fatal("TENANT_ID environment variable not set")
104+
}
105+
106+
ctx := context.Background()
107+
withAdminApiKey(ctx, serverURL, adminAPIKey, tenantID)
108+
109+
log.Println("--- Auth example finished ---")
110+
}

examples/sdk-go/go.mod

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ module outpost-go-client-example
33
go 1.23.2
44

55
require (
6-
github.com/hookdeck/outpost/sdks/outpost-go v0.1.7
6+
github.com/hookdeck/outpost/sdks/outpost-go v0.2.0
77
github.com/joho/godotenv v1.5.1
88
)
99

10-
require github.com/ericlagergren/decimal v0.0.0-20221120152707-495c53812d05 // indirect
10+
require (
11+
github.com/ericlagergren/decimal v0.0.0-20240411145413-00de7ca16731 // indirect
12+
github.com/google/uuid v1.6.0 // indirect
13+
)

examples/sdk-go/go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
github.com/ericlagergren/decimal v0.0.0-20221120152707-495c53812d05 h1:S92OBrGuLLZsyM5ybUzgc/mPjIYk2AZqufieooe98uw=
22
github.com/ericlagergren/decimal v0.0.0-20221120152707-495c53812d05/go.mod h1:M9R1FoZ3y//hwwnJtO51ypFGwm8ZfpxPT/ZLtO1mcgQ=
3+
github.com/ericlagergren/decimal v0.0.0-20240411145413-00de7ca16731 h1:R/ZjJpjQKsZ6L/+Gf9WHbt31GG8NMVcpRqUE+1mMIyo=
4+
github.com/ericlagergren/decimal v0.0.0-20240411145413-00de7ca16731/go.mod h1:M9R1FoZ3y//hwwnJtO51ypFGwm8ZfpxPT/ZLtO1mcgQ=
5+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
6+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
37
github.com/hookdeck/outpost/sdks/outpost-go v0.1.7 h1:6jtGYk+W7jXuttO+xr6Hn1RPKiJZzsdJ8Gwj2dh5rys=
48
github.com/hookdeck/outpost/sdks/outpost-go v0.1.7/go.mod h1:OrLM7zcKVaPA7nwUK8VdmkviUt/XlQ/eyKsLy3QyvPM=
9+
github.com/hookdeck/outpost/sdks/outpost-go v0.2.0 h1:BysBcw6mjTo4EQOrPjHssqRV8z30c0bMXRfz5m1UxVU=
10+
github.com/hookdeck/outpost/sdks/outpost-go v0.2.0/go.mod h1:OrLM7zcKVaPA7nwUK8VdmkviUt/XlQ/eyKsLy3QyvPM=
511
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
612
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=

examples/sdk-go/main.go

Lines changed: 26 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,50 @@
11
package main
22

33
import (
4-
"context"
5-
"fmt"
64
"log"
75
"os"
86

9-
outpostgo "github.com/hookdeck/outpost/sdks/outpost-go"
10-
"github.com/hookdeck/outpost/sdks/outpost-go/models/components"
117
"github.com/joho/godotenv"
128
)
139

14-
func withJwt(ctx context.Context, jwt string, serverURL string, tenantID string) {
15-
log.Println("--- Running with Tenant JWT ---")
16-
17-
apiServerURL := fmt.Sprintf("%s/api/v1", serverURL)
18-
19-
jwtClient := outpostgo.New(
20-
outpostgo.WithSecurity(components.Security{
21-
TenantJwt: outpostgo.String(jwt),
22-
}),
23-
outpostgo.WithServerURL(apiServerURL),
24-
)
25-
26-
destRes, err := jwtClient.Destinations.List(ctx, outpostgo.String(tenantID), nil, nil)
27-
if err != nil {
28-
log.Fatalf("Failed to list destinations with JWT: %v", err)
29-
}
30-
31-
if destRes != nil && destRes.Destinations != nil {
32-
log.Printf("Successfully listed %d destinations using JWT.", len(destRes.Destinations))
33-
} else {
34-
log.Println("List destinations with JWT returned no data or an unexpected response structure.")
35-
}
36-
}
37-
38-
func withAdminApiKey(ctx context.Context, serverURL string, adminAPIKey string, tenantID string) {
39-
log.Println("--- Running with Admin API Key ---")
40-
41-
apiServerURL := fmt.Sprintf("%s/api/v1", serverURL)
42-
43-
adminClient := outpostgo.New(
44-
outpostgo.WithSecurity(components.Security{
45-
AdminAPIKey: outpostgo.String(adminAPIKey),
46-
}),
47-
outpostgo.WithServerURL(apiServerURL),
48-
)
49-
50-
healthRes, err := adminClient.Health.Check(ctx)
51-
if err != nil {
52-
log.Fatalf("Health check failed: %v", err)
53-
}
54-
55-
if healthRes != nil && healthRes.Res != nil {
56-
log.Printf("Health check successful. Details: %s", *healthRes.Res)
57-
} else {
58-
log.Println("Health check returned no data or an unexpected response structure.")
59-
}
60-
61-
destRes, err := adminClient.Destinations.List(ctx, outpostgo.String(tenantID), nil, nil)
62-
if err != nil {
63-
log.Fatalf("Failed to list destinations with Admin Key: %v", err)
64-
}
65-
66-
if destRes != nil && destRes.Destinations != nil {
67-
log.Printf("Successfully listed %d destinations using Admin Key for tenant %s.", len(destRes.Destinations), tenantID)
68-
} else {
69-
log.Println("List destinations with Admin Key returned no data or an unexpected response structure.")
70-
}
71-
72-
tokenRes, err := adminClient.Tenants.GetToken(ctx, outpostgo.String(tenantID))
73-
if err != nil {
74-
log.Fatalf("Failed to get tenant token: %v", err)
75-
}
76-
77-
if tokenRes != nil && tokenRes.TenantToken != nil && tokenRes.TenantToken.Token != nil {
78-
log.Printf("Successfully obtained tenant JWT for tenant %s.", tenantID)
79-
withJwt(ctx, *tokenRes.TenantToken.Token, serverURL, tenantID)
80-
} else {
81-
log.Println("Get tenant token returned no data or an unexpected response structure.")
82-
}
83-
}
84-
8510
func main() {
8611
err := godotenv.Load()
8712
if err != nil {
8813
log.Println("No .env file found, proceeding without it")
8914
}
9015

91-
serverURL := os.Getenv("SERVER_URL")
9216
adminAPIKey := os.Getenv("ADMIN_API_KEY")
93-
tenantID := os.Getenv("TENANT_ID")
94-
17+
serverURL := os.Getenv("SERVER_URL")
9518
if serverURL == "" {
96-
log.Fatal("SERVER_URL environment variable not set")
19+
serverURL = "http://localhost:3333"
20+
log.Printf("SERVER_URL not set, defaulting to %s", serverURL)
9721
}
22+
9823
if adminAPIKey == "" {
99-
log.Fatal("ADMIN_API_KEY environment variable not set")
24+
log.Println("Warning: ADMIN_API_KEY environment variable not set. Some examples might fail.")
10025
}
101-
if tenantID == "" {
102-
log.Fatal("TENANT_ID environment variable not set")
26+
27+
if len(os.Args) < 2 {
28+
log.Println("Usage: go run . <example_name>")
29+
log.Println("Available examples: manage, auth")
30+
os.Exit(1)
10331
}
10432

105-
ctx := context.Background()
106-
withAdminApiKey(ctx, serverURL, adminAPIKey, tenantID)
33+
exampleToRun := os.Args[1]
10734

108-
log.Println("--- Example finished ---")
35+
switch exampleToRun {
36+
case "manage":
37+
if adminAPIKey == "" {
38+
log.Fatal("ADMIN_API_KEY environment variable must be set to run the 'manage' example.")
39+
}
40+
log.Println("--- Running Manage Outpost Resources Example ---")
41+
manageOutpostResources(adminAPIKey, serverURL)
42+
case "auth":
43+
log.Println("--- Running Auth Example ---")
44+
runAuthExample()
45+
default:
46+
log.Printf("Unknown example: %s\n", exampleToRun)
47+
log.Println("Available examples: manage, auth")
48+
os.Exit(1)
49+
}
10950
}

0 commit comments

Comments
 (0)