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
import (
"github.com/apioo/sdkgen-go/v2"
)
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 BuildAnonymous(baseUrl string) (*Client, error) {
var credentials = sdkgen.Anonymous{}
return NewClient(baseUrl, credentials)
}
// error automatically generated by SDKgen please do not edit this file manually
// @see https://sdkgen.app
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
import (
"encoding/json"
"fmt"
)
type ErrorException struct {
Payload Error
Previous 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
type HelloWorld struct {
Message string `json:"message"`
}
// response automatically generated by SDKgen please do not edit this file manually
// @see https://sdkgen.app
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
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/apioo/sdkgen-go/v2"
"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 nil, err
}
u.RawQuery = client.internal.Parser.QueryWithStruct(queryParams, queryStructNames).Encode()
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return nil, err
}
resp, err := client.internal.HttpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
var data HelloWorld
err := json.Unmarshal(respBody, &data)
return &data, err
}
var statusCode = resp.StatusCode
return nil, errors.New(fmt.Sprint("The server returned an unknown status code: ", statusCode))
}
// 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 nil, err
}
u.RawQuery = client.internal.Parser.QueryWithStruct(queryParams, queryStructNames).Encode()
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return nil, err
}
resp, err := client.internal.HttpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
var data Todos
err := json.Unmarshal(respBody, &data)
return &data, err
}
var statusCode = resp.StatusCode
return nil, errors.New(fmt.Sprint("The server returned an unknown status code: ", statusCode))
}
// 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 nil, err
}
u.RawQuery = client.internal.Parser.QueryWithStruct(queryParams, queryStructNames).Encode()
raw, err := json.Marshal(payload)
if err != nil {
return nil, err
}
var reqBody = bytes.NewReader(raw)
req, err := http.NewRequest("POST", u.String(), reqBody)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
resp, err := client.internal.HttpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
var data Response
err := json.Unmarshal(respBody, &data)
return &data, err
}
var statusCode = resp.StatusCode
return nil, errors.New(fmt.Sprint("The server returned an unknown status code: ", statusCode))
}
// 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 nil, err
}
u.RawQuery = client.internal.Parser.QueryWithStruct(queryParams, queryStructNames).Encode()
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return nil, err
}
resp, err := client.internal.HttpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
var data HelloWorld
err := json.Unmarshal(respBody, &data)
return &data, err
}
var statusCode = resp.StatusCode
if statusCode == 500 {
var data Error
err := json.Unmarshal(respBody, &data)
return nil, &ErrorException{
Payload: data,
Previous: err,
}
}
return nil, errors.New(fmt.Sprint("The server returned an unknown status code: ", statusCode))
}
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
type Todo struct {
Title string `json:"title"`
}
// todos automatically generated by SDKgen please do not edit this file manually
// @see https://sdkgen.app
type Todos struct {
TotalResults int `json:"totalResults"`
StartIndex int `json:"startIndex"`
ItemsPerPage int `json:"itemsPerPage"`
Entry []Todo `json:"entry"`
}