Azion Go SDK
The Azion SDK for Go facilitates the use of the Azion API services by providing a set of tools that make the development of Go applications smoother.
Requirements
For using the Go SDK, you need to:
- Have Go 1.17 or later installed.
- Have a go project with a
main.go
file created. - Initialize a Go module with
go mod init <name>
.
Downloading and installing Azion SDK
The Azion API Go SDK is available on GitHub.
- On your go project’s root directory, open the terminal and run:
go get github.com/aziontech/azionapi-go-sdk
Or simply:
go mod tidy
Implementation
This is a complete practical example that can be copied and pasted to your main.go
file.
After that, you need to run the project with:
go run main.go
Creating an Edge DNS zone
package main
import ( "bufio" "context" "fmt" "io" "net/http" "os" "strings"
sdk "github.com/aziontech/azionapi-go-sdk/idns" )
const ( intelligentDnsURL = "https://api.azionapi.net/" )
type Client struct { apiClient sdk.APIClient }
func main() { var personalToken string
fmt.Println("Hey, there! Welcome to iDNS example")
fmt.Println("Please, provide your Personal Token: ") fmt.Scanf("%s", &personalToken)
err := IDNSHandler(personalToken) if err != nil { fmt.Println(err) }
}
// IDNSHandler // // - I/O operations - asks and reads domain name e zone // - Instantiates a new client // - Calls NewIdns func IDNSHandler(personalToken string) error { var domainName, dnsZone string active := true
reader := bufio.NewReader(os.Stdin)
fmt.Println("Plase, provide the Domain Name: ") domainName, err := reader.ReadString('\n') if err != nil { return err } domainName = strings.Replace(domainName, "\n", "", -1)
fmt.Println("Enter a dns zone: ") dnsZone, err = reader.ReadString('\n') if err != nil { return err } dnsZone = strings.Replace(dnsZone, "\n", "", -1)
var f *http.Client
client := NewClient(f, intelligentDnsURL, personalToken) err = client.NewIdns(&domainName, &dnsZone, &active) if err != nil { fmt.Println(err) return err } return nil }
// NewIdns // - Instantiate a sdk.Zone object // - Instantiate the request and exec it // - Prints success or error func (c *Client) NewIdns(domainName, domain *string, active *bool) error { ctx := context.Background() idns := new(sdk.Zone) idns.Name = domainName idns.Domain = domain idns.IsActive = active
fmt.Println("Creating iDNS zone....")
req := c.apiClient.ZonesApi.PostZone(ctx).Zone(*idns) _, httpResp, err := req.Execute() if err != nil { fmt.Println(httpResp) return err } bytes, err := io.ReadAll(httpResp.Body) if err != nil { return err }
fmt.Println(string(bytes)) return nil }
// NewClient // - Instantiates a new skd-api client // - Set headers func NewClient(f *http.Client, url string, token string) *Client {
conf := sdk.NewConfiguration()
conf.HTTPClient = f conf.AddDefaultHeader("Authorization", "token "+token) conf.AddDefaultHeader("Accept", "application/json;version=3") conf.Servers = sdk.ServerConfigurations{ {URL: url}, }
return &Client{ apiClient: *sdk.NewAPIClient(conf), } }
Explaining the example
Learn about the implementation of the Azion API SDK by analyzing the steps of the creation of a new Edge DNS zone.
Instantiating a Client
The Go client abstracts away the complexity of the underlying service or API, making it easier for developers to integrate with it.
To instantiate a client, you have to:
- Import the package you want to use. In this example, you’ll create a new Edge DNS zone.
import( sdk "github.com/aziontech/azionapi-go-sdk/idns" )
- Instantiate the client:
// Client is used to instantiate a Azion API Client type Client struct { apiClient sdk.APIClient } // NewClient // - Receives: // A *http.Client // The base path to the API you want to consume. ex: https://api.azionapi.net/ // The Azion Personal Token func NewClient(f * http.Client, url string, token string) * Client { conf: = sdk.NewConfiguration() conf.HTTPClient = f conf.AddDefaultHeader("Authorization", "token " + token) conf.AddDefaultHeader("Accept", "application/json;version=3") conf.Servers = sdk.ServerConfigurations { { URL: url }, } // Returns a ready to use client return &Client { apiClient: * sdk.NewAPIClient(conf), } }
Creating an Edge DNS zone
Take a look at the creation of an Edge DNS zone using the SDK:
// NewIdns // - Instantiate a sdk.Zone object // - Instantiate the request and exec it func(c * Client) NewIdns(domainName, domain * string, active * bool) error { // Instantiate a ctx variable ctx: = context.Background() // From the section iDNS of the SDK, we initialize a sdk.Zone object // Informing the required values for creating an iDNS zone. idns: = new(sdk.Zone) idns.Name = domainName idns.Domain = domain idns.IsActive = active // Make use of the PostZone Method, passing the idns zone object req: = c.apiClient.ZonesApi.PostZone(ctx).Zone( * idns) res, httpResp, err: = req.Execute() if err != nil { return err } // Here, the logic related to the response is applied...
fmt.Println(res, httpResp) return nil }
Available APIs
The Azion SDK offers management to the following services:
Contributors