-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
445 lines (389 loc) · 14.1 KB
/
main.go
File metadata and controls
445 lines (389 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
// Package main demonstrates comprehensive usage of the ResourcesService
// from the go-contextforge SDK. This example highlights API inconsistencies
// between create (snake_case fields) and update (camelCase fields), plus
// the ListTemplates method. Uses a mock HTTP server for self-contained demonstration.
//
// Run: go run examples/resources/main.go
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httptest"
"strings"
"time"
"github.com/leefowlercu/go-contextforge/contextforge"
)
func main() {
// Create mock server with all necessary endpoints
mux := http.NewServeMux()
setupMockEndpoints(mux)
server := httptest.NewServer(mux)
defer server.Close()
fmt.Println("=== ContextForge SDK - Resources Service ===")
// Step 1: Authentication
fmt.Println("1. Authenticating...")
token := authenticate(server.URL)
fmt.Printf(" ✓ Obtained JWT token: %s...\n\n", token[:20])
// Step 2: Create client
// To use a real ContextForge instance, replace server.URL with:
// "https://your-contextforge-instance.com"
client, err := contextforge.NewClient(nil, server.URL, token)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
ctx := context.Background()
// Step 3: List available resource templates
fmt.Println("2. Listing available resource templates...")
templates, _, err := client.Resources.ListTemplates(ctx)
if err != nil {
log.Fatalf("Failed to list templates: %v", err)
}
fmt.Printf(" ✓ Found %d template(s):\n", len(templates.Templates))
for i, template := range templates.Templates {
fmt.Printf(" %d. %s - %s\n", i+1, template.Name, template.Description)
fmt.Printf(" URI: %s\n", template.URI)
fmt.Printf(" MIME Type: %s\n", template.MimeType)
}
fmt.Println()
// Step 4: Create a resource
fmt.Println("3. Creating a new resource...")
// IMPORTANT: ResourceCreate uses snake_case fields (mime_type)
newResource := &contextforge.ResourceCreate{
URI: "file:///etc/app/config.json",
Name: "example-config-file",
Content: `{"setting": "value"}`,
Description: contextforge.String("A configuration file resource for demonstration"),
MimeType: contextforge.String("application/json"),
Tags: []string{"config", "json", "example"},
}
// TeamID and Visibility can be passed via options
opts := &contextforge.ResourceCreateOptions{
TeamID: contextforge.String("team-example"),
Visibility: contextforge.String("public"),
}
createdResource, resp, err := client.Resources.Create(ctx, newResource, opts)
if err != nil {
log.Fatalf("Failed to create resource: %v", err)
}
fmt.Printf(" ✓ Created resource: %s (ID: %s)\n", createdResource.Name, (*createdResource.ID).String())
fmt.Printf(" ✓ URI: %s\n", createdResource.URI)
if createdResource.MimeType != nil {
fmt.Printf(" ✓ MIME Type: %s\n", *createdResource.MimeType)
}
fmt.Printf(" ✓ Active: %v\n", createdResource.IsActive)
fmt.Printf(" ✓ Rate limit: %d/%d remaining\n\n", resp.Rate.Remaining, resp.Rate.Limit)
// Step 5: List resources with filtering
fmt.Println("4. Listing resources with filters...")
listOpts := &contextforge.ResourceListOptions{
ListOptions: contextforge.ListOptions{
Limit: 10,
},
IncludeInactive: true,
Tags: "config,json",
TeamID: "team-example",
Visibility: "public",
}
resources, resp, err := client.Resources.List(ctx, listOpts)
if err != nil {
log.Fatalf("Failed to list resources: %v", err)
}
fmt.Printf(" ✓ Found %d resource(s)\n", len(resources))
for i, resource := range resources {
fmt.Printf(" %d. %s (ID: %s, Active: %v)\n", i+1, resource.Name, (*resource.ID).String(), resource.IsActive)
fmt.Printf(" URI: %s\n", resource.URI)
}
fmt.Println()
// Step 6: Update the resource
fmt.Println("5. Updating resource...")
// IMPORTANT: ResourceUpdate uses camelCase fields (mimeType)
// This is an API inconsistency - Create uses snake_case, Update uses camelCase
updateResource := &contextforge.ResourceUpdate{
Description: contextforge.String("An advanced configuration with additional metadata"),
Tags: []string{"config", "json", "example", "advanced"},
// Note: MimeType would use camelCase if we were updating it
}
updatedResource, _, err := client.Resources.Update(ctx, (*createdResource.ID).String(), updateResource)
if err != nil {
log.Fatalf("Failed to update resource: %v", err)
}
if updatedResource.Description != nil {
fmt.Printf(" ✓ Updated description: %s\n", *updatedResource.Description)
}
fmt.Printf(" ✓ Updated tags: %v\n\n", updatedResource.Tags)
// Step 7: Pagination example
fmt.Println("6. Demonstrating pagination...")
page := 1
cursor := ""
for {
pageOpts := &contextforge.ResourceListOptions{
ListOptions: contextforge.ListOptions{
Limit: 2,
Cursor: cursor,
},
}
pageResources, pageResp, err := client.Resources.List(ctx, pageOpts)
if err != nil {
log.Fatalf("Failed to list page: %v", err)
}
fmt.Printf(" Page %d: %d resource(s)\n", page, len(pageResources))
if pageResp.NextCursor == "" || len(pageResources) == 0 {
break
}
cursor = pageResp.NextCursor
page++
if page > 3 { // Limit pagination demo
fmt.Println(" (stopping after 3 pages for demo)")
break
}
}
fmt.Println()
// Step 8: Toggle resource (deactivate)
fmt.Println("7. Toggling resource (deactivating)...")
// Note: Resources toggle has complex response unwrapping due to snake_case response
toggledResource, _, err := client.Resources.Toggle(ctx, (*createdResource.ID).String(), false)
if err != nil {
log.Fatalf("Failed to toggle resource: %v", err)
}
fmt.Printf(" ✓ Resource is now active: %v\n\n", toggledResource.IsActive)
// Step 9: Toggle resource (reactivate)
fmt.Println("8. Toggling resource (reactivating)...")
toggledResource, _, err = client.Resources.Toggle(ctx, (*createdResource.ID).String(), true)
if err != nil {
log.Fatalf("Failed to toggle resource: %v", err)
}
fmt.Printf(" ✓ Resource is now active: %v\n\n", toggledResource.IsActive)
// Step 10: Delete the resource
fmt.Println("9. Deleting resource...")
_, err = client.Resources.Delete(ctx, (*createdResource.ID).String())
if err != nil {
log.Fatalf("Failed to delete resource: %v", err)
}
fmt.Printf(" ✓ Resource deleted successfully\n\n")
fmt.Println("=== Example completed successfully! ===")
fmt.Println("\nKey API Quirks Demonstrated:")
fmt.Println("• ResourceCreate uses snake_case (mime_type)")
fmt.Println("• ResourceUpdate uses camelCase (mimeType)")
fmt.Println("• Toggle response has complex unwrapping (snake_case)")
fmt.Println("• ListTemplates provides available resource templates")
fmt.Println("\nNote: This is an API inconsistency that the SDK handles internally")
}
// authenticate performs mock authentication and returns a JWT token
func authenticate(address string) string {
loginURL := address + "/auth/login"
payload := strings.NewReader(`{"email":"admin@example.com","password":"secret"}`)
resp, err := http.Post(loginURL, "application/json", payload)
if err != nil {
log.Fatalf("Authentication failed: %v", err)
}
defer resp.Body.Close()
var authResp struct {
AccessToken string `json:"access_token"`
}
if err := json.NewDecoder(resp.Body).Decode(&authResp); err != nil {
log.Fatalf("Failed to decode auth response: %v", err)
}
return authResp.AccessToken
}
// setupMockEndpoints configures all the mock HTTP endpoints
func setupMockEndpoints(mux *http.ServeMux) {
// Mock authentication endpoint
mux.HandleFunc("/auth/login", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]any{
"access_token": "mock-jwt-token-11111",
"token_type": "bearer",
})
})
// Mock resource storage (in-memory)
resources := make(map[string]*contextforge.Resource)
var resourceCounter int
// GET /resources/templates/list - List templates
mux.HandleFunc("/resources/templates/list", func(w http.ResponseWriter, r *http.Request) {
templates := &contextforge.ListResourceTemplatesResult{
Templates: []contextforge.ResourceTemplate{
{
Name: "File Resource",
Description: "A file-based resource template",
URI: "file://{path}",
MimeType: "text/plain",
},
{
Name: "HTTP Resource",
Description: "An HTTP/HTTPS resource template",
URI: "https://{domain}/{path}",
MimeType: "application/json",
},
{
Name: "Database Resource",
Description: "A database connection resource template",
URI: "postgres://{host}:{port}/{database}",
MimeType: "application/sql",
},
},
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(templates)
})
// POST /resources - Create resource
// GET /resources - List resources
mux.HandleFunc("/resources", func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodPost:
var req struct {
Resource *contextforge.ResourceCreate `json:"resource"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
resourceCounter++
id := contextforge.FlexibleID(fmt.Sprintf("resource-%d", resourceCounter))
now := time.Now()
resource := &contextforge.Resource{
ID: &id,
URI: req.Resource.URI,
Name: req.Resource.Name,
Description: req.Resource.Description,
MimeType: req.Resource.MimeType,
Tags: contextforge.NewTags(req.Resource.Tags),
IsActive: true,
CreatedAt: &contextforge.Timestamp{Time: now},
UpdatedAt: &contextforge.Timestamp{Time: now},
}
resources[id.String()] = resource
w.Header().Set("Content-Type", "application/json")
w.Header().Set("X-RateLimit-Limit", "1000")
w.Header().Set("X-RateLimit-Remaining", "995")
w.Header().Set("X-RateLimit-Reset", fmt.Sprintf("%d", now.Add(time.Hour).Unix()))
// Return resource directly (not wrapped)
json.NewEncoder(w).Encode(resource)
case http.MethodGet:
query := r.URL.Query()
result := []*contextforge.Resource{}
for _, resource := range resources {
// Apply filters
if query.Get("include_inactive") != "true" && !resource.IsActive {
continue
}
if teamID := query.Get("team_id"); teamID != "" && resource.TeamID != nil && *resource.TeamID != teamID {
continue
}
if visibility := query.Get("visibility"); visibility != "" && resource.Visibility != nil && *resource.Visibility != visibility {
continue
}
result = append(result, resource)
}
w.Header().Set("Content-Type", "application/json")
// Pagination simulation
limit := 10
if l := query.Get("limit"); l != "" {
fmt.Sscanf(l, "%d", &limit)
}
if len(result) > limit {
w.Header().Set("X-Next-Cursor", "mock-cursor-next-page")
result = result[:limit]
}
json.NewEncoder(w).Encode(result)
}
})
// GET /resources/{id} - Get resource
// PUT /resources/{id} - Update resource
// DELETE /resources/{id} - Delete resource
mux.HandleFunc("/resources/", func(w http.ResponseWriter, r *http.Request) {
parts := strings.Split(r.URL.Path, "/")
if len(parts) < 3 {
http.Error(w, "Invalid path", http.StatusBadRequest)
return
}
// Skip /resources/templates/list
if parts[2] == "templates" {
return
}
resourceID := parts[2]
// Handle toggle endpoint
if len(parts) == 4 && parts[3] == "toggle" {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
resource, exists := resources[resourceID]
if !exists {
http.Error(w, `{"message":"Resource not found"}`, http.StatusNotFound)
return
}
// Extract activate parameter from query string
activate := r.URL.Query().Get("activate") == "true"
resource.IsActive = activate
resource.UpdatedAt = &contextforge.Timestamp{Time: time.Now()}
// Resources toggle has complex response with status/message and snake_case
// Note: The SDK handles converting this to the standard Resource format
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]any{
"status": "success",
"message": "Resource toggled successfully",
"resource": map[string]any{
"id": (*resource.ID).String(),
"uri": resource.URI,
"name": resource.Name,
"description": resource.Description,
"mime_type": resource.MimeType, // Note: snake_case in toggle response
"is_active": resource.IsActive, // Note: snake_case in toggle response
"tags": resource.Tags,
"created_at": resource.CreatedAt,
"updated_at": resource.UpdatedAt,
},
})
return
}
switch r.Method {
case http.MethodGet:
resource, exists := resources[resourceID]
if !exists {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusNotFound)
json.NewEncoder(w).Encode(map[string]any{
"message": "Resource not found",
})
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(resource)
case http.MethodPut:
resource, exists := resources[resourceID]
if !exists {
http.Error(w, `{"message":"Resource not found"}`, http.StatusNotFound)
return
}
// Note: Update request is NOT wrapped (unlike Create)
var req contextforge.ResourceUpdate
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Update fields
if req.Description != nil {
resource.Description = req.Description
}
if req.Tags != nil {
resource.Tags = contextforge.NewTags(req.Tags)
}
if req.MimeType != nil {
resource.MimeType = req.MimeType
}
resource.UpdatedAt = &contextforge.Timestamp{Time: time.Now()}
w.Header().Set("Content-Type", "application/json")
// Return resource directly (not wrapped)
json.NewEncoder(w).Encode(resource)
case http.MethodDelete:
if _, exists := resources[resourceID]; !exists {
http.Error(w, `{"message":"Resource not found"}`, http.StatusNotFound)
return
}
delete(resources, resourceID)
w.WriteHeader(http.StatusNoContent)
}
})
}