Azion Go SDK
O Azion SDK para a linguagem Go facilita o uso dos serviços das APIs da Azion, fornecendo ferramentas que simplificam o desenvolvimento de aplicações em Go.
Pré-requisitos
Para usar o SDK, você precisa:
- Ter a linguagem Go na versão 1.17 ou posterior instalada.
- Ter um projeto Go com um arquivo
main.go
criado. - Inicializar um módulo Go com
go mod init<name>
.
Baixar e instalar o SDK
O SDK está disponível no GitHub.
- No diretório raiz do seu projeto, abra o terminal e execute:
go get github.com/aziontech/azionapi-go-sdk
Ou simplesmente:
go mod tidy
Implementação
Este é um exemplo completo que pode ser copiado e colado no seu arquivo main.go
.
Depois disso, você precisa executar o projeto com:
go run main.go
Criar uma zona iDNS
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), } }
Explicação do exemplo
Aprenda mais sobre a implementação do Azion API SDK analisando as etapas da criação de uma nova zona iDNS.
Instanciar um client
O Go client abstrai a complexidade do serviço ou API, facilitando a integração dos desenvolvedores com os mesmos.
Para instanciar um client:
- Importe o pacote que você deseja usar. Neste exemplo, você criará uma nova zona iDNS.
import( sdk "github.com/aziontech/azionapi-go-sdk/idns" )
- Instancie o 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), } }
Criar uma zona iDNS
Veja a criação de uma zona iDNS usando o 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 }
APIs disponíveis
O SDK oferece gerenciamento para os seguintes serviços:
Contribuidores