@@ -3,6 +3,7 @@ package client
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "strconv"
6
7
7
8
"github.com/harness/harness-mcp/client/dto"
8
9
)
@@ -15,27 +16,64 @@ const (
15
16
)
16
17
17
18
type TemplateService struct {
18
- Client * Client
19
- UseInternalPaths bool
19
+ Client * Client
20
20
}
21
21
22
22
func (ts * TemplateService ) buildPath (basePath string ) string {
23
23
return basePath
24
24
}
25
25
26
- // ListAccount lists templates in the account scope
27
- func (ts * TemplateService ) ListAccount (ctx context.Context , opts * dto.TemplateListOptions ) (* dto.TemplateMetaDataList , error ) {
28
- endpoint := ts .buildPath (templateAccountPath )
29
-
26
+ // buildTemplateParams converts template list options to query parameters map
27
+ func buildTemplateParams (opts * dto.TemplateListOptions ) map [string ]string {
30
28
params := make (map [string ]string )
29
+
30
+ if opts == nil {
31
+ opts = & dto.TemplateListOptions {}
32
+ }
33
+
34
+ // Pagination params
35
+ params ["page" ] = strconv .Itoa (opts .Page )
36
+ params ["limit" ] = strconv .Itoa (opts .Limit )
37
+
38
+ // Sorting params
39
+ if opts .Sort != "" {
40
+ params ["sort" ] = opts .Sort
41
+ }
42
+ if opts .Order != "" {
43
+ params ["order" ] = opts .Order
44
+ }
45
+
46
+ // Filtering params
31
47
if opts .SearchTerm != "" {
32
- params ["searchTerm" ] = opts .SearchTerm
48
+ params ["search_term" ] = opts .SearchTerm
49
+ }
50
+ if opts .Type != "" {
51
+ params ["type" ] = opts .Type
33
52
}
34
- if opts .TemplateListType != "" {
35
- params ["templateListType" ] = opts .TemplateListType
53
+ if opts .Recursive {
54
+ params ["recursive" ] = "true"
55
+ }
56
+
57
+ appendArrayParams (params , "names" , opts .Names )
58
+ appendArrayParams (params , "identifiers" , opts .Identifiers )
59
+ appendArrayParams (params , "entity_types" , opts .EntityTypes )
60
+ appendArrayParams (params , "child_types" , opts .ChildTypes )
61
+
62
+ return params
63
+ }
64
+
65
+ func appendArrayParams (params map [string ]string , paramName string , values []string ) {
66
+ for i , value := range values {
67
+ // Use strconv.Itoa instead of fmt.Sprintf for integer to string conversion
68
+ key := paramName + "[" + strconv .Itoa (i ) + "]"
69
+ params [key ] = value
36
70
}
37
- params ["page" ] = fmt .Sprintf ("%d" , opts .Page )
38
- params ["size" ] = fmt .Sprintf ("%d" , opts .Size )
71
+ }
72
+
73
+ // ListAccount lists templates in the account scope
74
+ func (ts * TemplateService ) ListAccount (ctx context.Context , opts * dto.TemplateListOptions ) (* dto.TemplateMetaDataList , error ) {
75
+ endpoint := ts .buildPath (templateAccountPath )
76
+ params := buildTemplateParams (opts )
39
77
40
78
var result dto.TemplateMetaDataList
41
79
err := ts .Client .Get (ctx , endpoint , params , map [string ]string {}, & result )
@@ -49,17 +87,7 @@ func (ts *TemplateService) ListAccount(ctx context.Context, opts *dto.TemplateLi
49
87
// ListOrg lists templates in the organization scope
50
88
func (ts * TemplateService ) ListOrg (ctx context.Context , scope dto.Scope , opts * dto.TemplateListOptions ) (* dto.TemplateMetaDataList , error ) {
51
89
endpoint := ts .buildPath (fmt .Sprintf (templateOrgPath , scope .OrgID ))
52
-
53
- params := make (map [string ]string )
54
- addScope (scope , params )
55
- if opts .SearchTerm != "" {
56
- params ["searchTerm" ] = opts .SearchTerm
57
- }
58
- if opts .TemplateListType != "" {
59
- params ["templateListType" ] = opts .TemplateListType
60
- }
61
- params ["page" ] = fmt .Sprintf ("%d" , opts .Page )
62
- params ["size" ] = fmt .Sprintf ("%d" , opts .Size )
90
+ params := buildTemplateParams (opts )
63
91
64
92
var result dto.TemplateMetaDataList
65
93
err := ts .Client .Get (ctx , endpoint , params , map [string ]string {}, & result )
@@ -73,17 +101,7 @@ func (ts *TemplateService) ListOrg(ctx context.Context, scope dto.Scope, opts *d
73
101
// ListProject lists templates in the project scope
74
102
func (ts * TemplateService ) ListProject (ctx context.Context , scope dto.Scope , opts * dto.TemplateListOptions ) (* dto.TemplateMetaDataList , error ) {
75
103
endpoint := ts .buildPath (fmt .Sprintf (templateProjectPath , scope .OrgID , scope .ProjectID ))
76
-
77
- params := make (map [string ]string )
78
- addScope (scope , params )
79
- if opts .SearchTerm != "" {
80
- params ["searchTerm" ] = opts .SearchTerm
81
- }
82
- if opts .TemplateListType != "" {
83
- params ["templateListType" ] = opts .TemplateListType
84
- }
85
- params ["page" ] = fmt .Sprintf ("%d" , opts .Page )
86
- params ["size" ] = fmt .Sprintf ("%d" , opts .Size )
104
+ params := buildTemplateParams (opts )
87
105
88
106
var result dto.TemplateMetaDataList
89
107
err := ts .Client .Get (ctx , endpoint , params , map [string ]string {}, & result )
0 commit comments