Skip to content

Commit e7f2951

Browse files
markturanskyclaude
andcommitted
fix(sdk-generator): derive base path from OpenAPI spec instead of hardcoding
Generator now parses the base path (e.g. /api/ambient/v1) from the spec's path keys instead of hardcoding /api/ambient-api-server/v1. Updates model, parser, and all three language templates (Go, Python, TypeScript). Regenerates all SDK output with the correct /api/ambient/v1 base path. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 241edbf commit e7f2951

41 files changed

Lines changed: 132 additions & 114 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
271 Bytes
Binary file not shown.

components/ambient-sdk/generator/model.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type Field struct {
3636
}
3737

3838
type Spec struct {
39+
BasePath string
3940
Resources []Resource
4041
}
4142

components/ambient-sdk/generator/parser.go

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,29 @@ type openAPIDoc struct {
1717
} `yaml:"components"`
1818
}
1919

20+
func extractBasePath(paths map[string]interface{}) string {
21+
for path := range paths {
22+
parts := strings.SplitN(path, "/", -1)
23+
// Expect paths like /api/ambient/v1/resource or /api/ambient-api-server/v1/resource
24+
// Find the common prefix by looking for the version segment (v\d+)
25+
for i, part := range parts {
26+
if len(part) > 1 && part[0] == 'v' && len(part) > 1 {
27+
allDigits := true
28+
for _, c := range part[1:] {
29+
if c < '0' || c > '9' {
30+
allDigits = false
31+
break
32+
}
33+
}
34+
if allDigits {
35+
return strings.Join(parts[:i+1], "/")
36+
}
37+
}
38+
}
39+
}
40+
return "/api/ambient/v1"
41+
}
42+
2043
type subSpecDoc struct {
2144
Paths map[string]interface{} `yaml:"paths"`
2245
Components struct {
@@ -76,7 +99,9 @@ func parseSpec(specPath string) (*Spec, error) {
7699
return resources[i].Name < resources[j].Name
77100
})
78101

79-
return &Spec{Resources: resources}, nil
102+
basePath := extractBasePath(mainDoc.Paths)
103+
104+
return &Spec{BasePath: basePath, Resources: resources}, nil
80105
}
81106

82107
func extractResource(name, pathSegment string, doc *subSpecDoc) (*Resource, error) {
@@ -313,7 +338,8 @@ func extractPatchFields(schemaMap map[string]interface{}) ([]Field, []string, er
313338
}
314339

315340
func checkHasPatch(paths map[string]interface{}, pathSegment string) bool {
316-
idPath := fmt.Sprintf("/api/ambient-api-server/v1/%s/{id}", pathSegment)
341+
basePath := extractBasePath(paths)
342+
idPath := fmt.Sprintf("%s/%s/{id}", basePath, pathSegment)
317343
pathVal, ok := paths[idPath]
318344
if !ok {
319345
return false
@@ -329,7 +355,8 @@ func checkHasPatch(paths map[string]interface{}, pathSegment string) bool {
329355
}
330356

331357
func checkHasDelete(paths map[string]interface{}, pathSegment string) bool {
332-
idPath := fmt.Sprintf("/api/ambient-api-server/v1/%s/{id}", pathSegment)
358+
basePath := extractBasePath(paths)
359+
idPath := fmt.Sprintf("%s/%s/{id}", basePath, pathSegment)
333360
pathVal, ok := paths[idPath]
334361
if !ok {
335362
return false
@@ -345,10 +372,11 @@ func checkHasDelete(paths map[string]interface{}, pathSegment string) bool {
345372
}
346373

347374
func detectActions(paths map[string]interface{}, pathSegment string) []string {
375+
basePath := extractBasePath(paths)
348376
knownActions := []string{"start", "stop"}
349377
var found []string
350378
for _, action := range knownActions {
351-
actionPath := fmt.Sprintf("/api/ambient-api-server/v1/%s/{id}/%s", pathSegment, action)
379+
actionPath := fmt.Sprintf("%s/%s/{id}/%s", basePath, pathSegment, action)
352380
pathVal, ok := paths[actionPath]
353381
if !ok {
354382
continue

components/ambient-sdk/generator/templates/go/http_client.go.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func NewClientFromEnv(opts ...ClientOption) (*Client, error) {
116116
}
117117

118118
func (c *Client) do(ctx context.Context, method, path string, body []byte, expectedStatus int, result interface{}) error {
119-
url := c.baseURL + "/api/ambient-api-server/v1" + path
119+
url := c.baseURL + "{{.Spec.BasePath}}" + path
120120

121121
req, err := http.NewRequestWithContext(ctx, method, url, nil)
122122
if err != nil {

components/ambient-sdk/generator/templates/python/http_client.py.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ if TYPE_CHECKING:
2424
class AmbientClient:
2525
"""HTTP client for the Ambient Platform API."""
2626

27-
_base_path = "/api/ambient-api-server/v1"
27+
_base_path = "{{.Spec.BasePath}}"
2828

2929
def __init__(
3030
self,
@@ -110,7 +110,7 @@ class AmbientClient:
110110
expect_json: bool = True,
111111
) -> Any:
112112
"""Make HTTP request to the API."""
113-
url = self._base_url + "/api/ambient-api-server/v1" + path
113+
url = self._base_url + "{{.Spec.BasePath}}" + path
114114

115115
headers = {
116116
"Authorization": f"Bearer {self._token}",

components/ambient-sdk/generator/templates/ts/base.ts.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export async function ambientFetch<T>(
8181
body?: unknown,
8282
requestOpts?: RequestOptions,
8383
): Promise<T> {
84-
const url = `${config.baseUrl}/api/ambient-api-server/v1${path}`;
84+
const url = `${config.baseUrl}{{.Spec.BasePath}}${path}`;
8585
const headers: Record<string, string> = {
8686
'Authorization': `Bearer ${config.token}`,
8787
'X-Ambient-Project': config.project,

components/ambient-sdk/go-sdk/client/client.go

Lines changed: 11 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/ambient-sdk/go-sdk/client/iterator.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/ambient-sdk/go-sdk/client/project_api.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/ambient-sdk/go-sdk/client/project_settings_api.go

Lines changed: 4 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)