SDKgen is a powerful code generator to automatically build client SDKs for your REST API.
At the following page you can take a look at the generated code, the code is based on this TypeAPI specification. This is only a demo to give you a first insight, for live examples feel free to login at the backend and use the live generator.
// Client automatically generated by SDKgen please do not edit this file manually
// @see https://sdkgen.app
package main
import (
"github.com/apioo/sdkgen-go"
)
type Client struct {
internal *sdkgen.ClientAbstract
}
func (client *Client) Test() *TestTag {
return NewTestTag(client.internal.HttpClient, client.internal.Parser)
}
func NewClient(baseUrl string, credentials sdkgen.CredentialsInterface) (*Client, error) {
var client, err = sdkgen.NewClient(baseUrl, credentials)
if err != nil {
return &Client{}, err
}
return &Client{
internal: client,
}, nil
}
func Build(credentials sdkgen.CredentialsInterface) (*Client, error) {
return NewClient("https://api.sdkgen.app/", credentials)
}
// error automatically generated by SDKgen please do not edit this file manually
// @see https://sdkgen.app
package main
type Error struct {
Success bool `json:"success"`
Message string `json:"message"`
}
// ErrorException automatically generated by SDKgen please do not edit this file manually
// @see https://sdkgen.app
package main
import (
"encoding/json"
"fmt"
)
type ErrorException struct {
Payload Error
}
func (e *ErrorException) Error() string {
raw, err := json.Marshal(e.Payload)
if err != nil {
return "could not marshal provided JSON data"
}
return fmt.Sprintf("The server returned an error: %s", raw)
}
// hello_world automatically generated by SDKgen please do not edit this file manually
// @see https://sdkgen.app
package main
type HelloWorld struct {
Message string `json:"message"`
}
// response automatically generated by SDKgen please do not edit this file manually
// @see https://sdkgen.app
package main
type Response struct {
Success bool `json:"success"`
Message string `json:"message"`
}
// TestTag automatically generated by SDKgen please do not edit this file manually
// @see https://sdkgen.app
package main
import (
"bytes"
"encoding/json"
"errors"
"github.com/apioo/sdkgen-go"
"io"
"net/http"
"net/url"
)
type TestTag struct {
internal *sdkgen.TagAbstract
}
// GetHello Returns a hello world message
func (client *TestTag) GetHello() (HelloWorld, error) {
pathParams := make(map[string]interface{})
queryParams := make(map[string]interface{})
var queryStructNames []string
u, err := url.Parse(client.internal.Parser.Url("/hello/world", pathParams))
if err != nil {
return HelloWorld{}, err
}
u.RawQuery = client.internal.Parser.QueryWithStruct(queryParams, queryStructNames).Encode()
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return HelloWorld{}, err
}
resp, err := client.internal.HttpClient.Do(req)
if err != nil {
return HelloWorld{}, err
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return HelloWorld{}, err
}
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
var response HelloWorld
err = json.Unmarshal(respBody, &response)
if err != nil {
return HelloWorld{}, err
}
return response, nil
}
switch resp.StatusCode {
default:
return HelloWorld{}, errors.New("the server returned an unknown status code")
}
}
// GetEntries Returns available todo entries
func (client *TestTag) GetEntries(startIndex int, count int) (Todos, error) {
pathParams := make(map[string]interface{})
queryParams := make(map[string]interface{})
queryParams["startIndex"] = startIndex
queryParams["count"] = count
var queryStructNames []string
u, err := url.Parse(client.internal.Parser.Url("/todo", pathParams))
if err != nil {
return Todos{}, err
}
u.RawQuery = client.internal.Parser.QueryWithStruct(queryParams, queryStructNames).Encode()
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return Todos{}, err
}
resp, err := client.internal.HttpClient.Do(req)
if err != nil {
return Todos{}, err
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return Todos{}, err
}
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
var response Todos
err = json.Unmarshal(respBody, &response)
if err != nil {
return Todos{}, err
}
return response, nil
}
switch resp.StatusCode {
default:
return Todos{}, errors.New("the server returned an unknown status code")
}
}
// Insert Inserts a new todo entry
func (client *TestTag) Insert(payload Todo) (Response, error) {
pathParams := make(map[string]interface{})
queryParams := make(map[string]interface{})
var queryStructNames []string
u, err := url.Parse(client.internal.Parser.Url("/todo", pathParams))
if err != nil {
return Response{}, err
}
u.RawQuery = client.internal.Parser.QueryWithStruct(queryParams, queryStructNames).Encode()
raw, err := json.Marshal(payload)
if err != nil {
return Response{}, err
}
var reqBody = bytes.NewReader(raw)
req, err := http.NewRequest("POST", u.String(), reqBody)
if err != nil {
return Response{}, err
}
req.Header.Set("Content-Type", "application/json")
resp, err := client.internal.HttpClient.Do(req)
if err != nil {
return Response{}, err
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return Response{}, err
}
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
var response Response
err = json.Unmarshal(respBody, &response)
if err != nil {
return Response{}, err
}
return response, nil
}
switch resp.StatusCode {
default:
return Response{}, errors.New("the server returned an unknown status code")
}
}
// ThrowException Returns a hello world message
func (client *TestTag) ThrowException() (HelloWorld, error) {
pathParams := make(map[string]interface{})
queryParams := make(map[string]interface{})
var queryStructNames []string
u, err := url.Parse(client.internal.Parser.Url("/exception", pathParams))
if err != nil {
return HelloWorld{}, err
}
u.RawQuery = client.internal.Parser.QueryWithStruct(queryParams, queryStructNames).Encode()
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return HelloWorld{}, err
}
resp, err := client.internal.HttpClient.Do(req)
if err != nil {
return HelloWorld{}, err
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return HelloWorld{}, err
}
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
var response HelloWorld
err = json.Unmarshal(respBody, &response)
if err != nil {
return HelloWorld{}, err
}
return response, nil
}
switch resp.StatusCode {
case 500:
var response Error
err = json.Unmarshal(respBody, &response)
if err != nil {
return HelloWorld{}, err
}
return HelloWorld{}, &ErrorException{
Payload: response,
}
default:
return HelloWorld{}, errors.New("the server returned an unknown status code")
}
}
func NewTestTag(httpClient *http.Client, parser *sdkgen.Parser) *TestTag {
return &TestTag{
internal: &sdkgen.TagAbstract{
HttpClient: httpClient,
Parser: parser,
},
}
}
// todo automatically generated by SDKgen please do not edit this file manually
// @see https://sdkgen.app
package main
type Todo struct {
Title string `json:"title"`
}
// todos automatically generated by SDKgen please do not edit this file manually
// @see https://sdkgen.app
package main
type Todos struct {
TotalResults int `json:"totalResults"`
StartIndex int `json:"startIndex"`
ItemsPerPage int `json:"itemsPerPage"`
Entry []Todo `json:"entry"`
}