Skip to content

feat: add interfaces for the go sdk #157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions scripts/generate-sdk/languages/go.sh
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ generate_go_sdk() {
--git-user-id ${GIT_USER_ID} \
--git-repo-id ${GIT_REPO_ID} \
--global-property apis,models,modelTests=true,modelDocs=false,apiDocs=false,supportingFiles \
--additional-properties=isGoSubmodule=true,enumClassPrefix=true,$regional_api \
--http-user-agent stackit-sdk-go/${service}
--additional-properties=isGoSubmodule=true,enumClassPrefix=true,generateInterfaces=true,$regional_api \
--http-user-agent stackit-sdk-go/${service}

# Remove unnecessary files
rm ${SERVICES_FOLDER}/${service}/.openapi-generator-ignore
Expand Down
81 changes: 50 additions & 31 deletions templates/go/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,58 @@ import (

type {{classname}} interface {
{{#operation}}

/*
{{operationId}} {{{summary}}}{{^summary}}Method for {{operationId}}{{/summary}}
{{#notes}}

{{#isDeprecated}}Deprecated: {{/isDeprecated}}{{{unescapedNotes}}}
{{/notes}}
{{operationId}} {{{summary}}}{{^summary}}Method for {{operationId}}{{/summary}}
{{#notes}}
{{#isDeprecated}}Deprecated: {{/isDeprecated}}{{{unescapedNotes}}}
{{/notes}}

@param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().{{#pathParams}}
@param {{paramName}}{{#description}} {{{.}}}{{/description}}{{/pathParams}}
@return {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request
@param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().{{#pathParams}}
@param {{paramName}}{{#description}} {{{.}}}{{/description}}{{/pathParams}}
@return {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request
*/
{{{nickname}}}(ctx context.Context{{#pathParams}}, {{paramName}} {{{dataType}}}{{/pathParams}}) {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request

/*
{{nickname}}Execute executes the request{{#returnType}}
@return {{{.}}}{{/returnType}}

{{#isDeprecated}}
Deprecated: {{{unescapedNotes}}}
{{/isDeprecated}}
{{nickname}}Execute executes the request{{#returnType}}

@param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().{{#pathParams}}
@param {{paramName}}{{#description}} {{{.}}}{{/description}}{{/pathParams}}
@return {{{.}}}{{/returnType}}

{{#isDeprecated}}
Deprecated: {{{unescapedNotes}}}
{{/isDeprecated}}
*/
{{nickname}}Execute(r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request) ({{#returnType}}{{^isArray}}{{^returnTypeIsPrimitive}}{{^isResponseFile}}*{{/isResponseFile}}{{/returnTypeIsPrimitive}}{{/isArray}}{{{.}}}, {{/returnType}}*http.Response, error)
{{nickname}}Execute(ctx context.Context{{#pathParams}}, {{paramName}} {{{dataType}}}{{/pathParams}}) ({{#returnType}}{{^isArray}}{{^returnTypeIsPrimitive}}{{^isResponseFile}}*{{/isResponseFile}}{{/returnTypeIsPrimitive}}{{/isArray}}{{{.}}}, {{/returnType}} error)
{{/operation}}
}

{{#operation}}
type {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request interface {
{{#allParams}}
{{^isPathParam}}
{{#description}}
// {{.}}
{{/description}}
{{#isDeprecated}}
// Deprecated
{{/isDeprecated}}
{{vendorExtensions.x-export-param-name}}({{paramName}} {{{dataType}}}) {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request
{{/isPathParam}}
{{/allParams}}
Execute() ({{#returnType}}{{^isArray}}{{^returnTypeIsPrimitive}}{{^isResponseFile}}*{{/isResponseFile}}{{/returnTypeIsPrimitive}}{{/isArray}}{{{.}}}, {{/returnType}} error)
}

{{/operation}}
{{/generateInterfaces}}

// {{classname}}Service {{classname}} service
type {{classname}}Service service
{{#operation}}

type {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request struct {
ctx context.Context{{#generateInterfaces}}
apiService {{classname}}
{{/generateInterfaces}}{{^generateInterfaces}}
type {{#structPrefix}}{{&classname}}{{/structPrefix}}{{operationId}}Request struct {
ctx context.Context
apiService *{{classname}}Service
{{/generateInterfaces}}
{{#allParams}}
{{paramName}} {{^isPathParam}}{{^isFile}}*{{/isFile}}{{/isPathParam}}{{{dataType}}}
{{/allParams}}
Expand All @@ -69,15 +84,15 @@ type {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/stru
// Deprecated
{{/isDeprecated}}

func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request) {{vendorExtensions.x-export-param-name}}({{paramName}} {{{dataType}}}) {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request {
func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{operationId}}Request) {{vendorExtensions.x-export-param-name}}({{paramName}} {{{dataType}}}) {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request {
r.{{paramName}} = {{^isFile}}&{{/isFile}}{{paramName}}
return r
}

{{/isPathParam}}
{{/allParams}}

func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request) Execute() ({{#returnType}}{{^isArray}}{{^returnTypeIsPrimitive}}{{^isResponseFile}}*{{/isResponseFile}}{{/returnTypeIsPrimitive}}{{/isArray}}{{{.}}}, {{/returnType}} error) {
func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{operationId}}Request) Execute() ({{#returnType}}{{^isArray}}{{^returnTypeIsPrimitive}}{{^isResponseFile}}*{{/isResponseFile}}{{/returnTypeIsPrimitive}}{{/isArray}}{{{.}}}, {{/returnType}} error) {
var (
localVarHTTPMethod = http.Method{{httpMethod}}
localVarPostBody interface{}
Expand All @@ -87,7 +102,11 @@ func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/s
{{/returnType}}
)
a := r.apiService
localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "{{{classname}}}Service.{{{nickname}}}")
client, ok := a.client.(*APIClient)
if !ok {
return {{#returnType}}nil, {{/returnType}} fmt.Errorf("could not parse client to type APIClient")
}
localBasePath, err := client.cfg.ServerURLWithContext(r.ctx, "{{{classname}}}Service.{{{nickname}}}")
if err != nil {
return {{#returnType}}localVarReturnValue, {{/returnType}} &oapierror.GenericOpenAPIError{ErrorMessage: err.Error()}
}
Expand Down Expand Up @@ -301,7 +320,7 @@ func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/s
{{/isKeyInCookie}}
{{/isApiKey}}
{{/authMethods}}
req, err := a.client.prepareRequest(r.ctx, localVarPath, localVarHTTPMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, formFiles)
req, err := client.prepareRequest(r.ctx, localVarPath, localVarHTTPMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, formFiles)
if err != nil {
return {{#returnType}}localVarReturnValue, {{/returnType}} err
}
Expand All @@ -311,7 +330,7 @@ func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/s
*contextHTTPRequest = req
}

localVarHTTPResponse, err := a.client.callAPI(req)
localVarHTTPResponse, err := client.callAPI(req)
contextHTTPResponse, ok := r.ctx.Value(config.ContextHTTPResponse).(**http.Response)
if ok {
*contextHTTPResponse = localVarHTTPResponse
Expand Down Expand Up @@ -354,7 +373,7 @@ func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/s
{{/wildcard}}
{{/range}}
var v {{{dataType}}}
err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
err = client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
if err != nil {
newErr.ErrorMessage = err.Error()
return {{#returnType}}localVarReturnValue, {{/returnType}} newErr
Expand All @@ -375,7 +394,7 @@ func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/s
}

{{#returnType}}
err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
err = client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
if err != nil {
newErr := &oapierror.GenericOpenAPIError{
StatusCode: localVarHTTPResponse.StatusCode,
Expand All @@ -401,7 +420,7 @@ func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/s
@return {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request
*/
func (a *APIClient) {{{nickname}}}(ctx context.Context{{#pathParams}}, {{paramName}} {{{dataType}}}{{/pathParams}}) {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request {
return {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request{
return {{#structPrefix}}{{&classname}}{{/structPrefix}}{{operationId}}Request{
apiService: a.defaultApi,
ctx: ctx,
{{#pathParams}}
Expand All @@ -416,7 +435,7 @@ Deprecated: {{{unescapedNotes}}}
*/
{{/isDeprecated}}
func (a *APIClient) {{{nickname}}}Execute(ctx context.Context{{#pathParams}}, {{paramName}} {{{dataType}}}{{/pathParams}}) ({{#returnType}}{{^isArray}}{{^returnTypeIsPrimitive}}{{^isResponseFile}}*{{/isResponseFile}}{{/returnTypeIsPrimitive}}{{/isArray}}{{{.}}}, {{/returnType}} error){
r := {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request{
r := {{#structPrefix}}{{&classname}}{{/structPrefix}}{{operationId}}Request{
apiService: a.defaultApi,
ctx: ctx,
{{#pathParams}}
Expand Down
2 changes: 1 addition & 1 deletion templates/go/client.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type APIClient struct {
}

type service struct {
client *APIClient
client DefaultApi
}

// NewAPIClient creates a new API client.
Expand Down
Loading