Skip to content

Latest commit

 

History

History
264 lines (202 loc) · 7.9 KB

v1.md

File metadata and controls

264 lines (202 loc) · 7.9 KB
layout title description meta_tags namespace permalink permalink_pt-br og_image meta_tag_robots_no_index
page-documentation-md
Azion Go SDK
With an SDK, developers don't have to build everything from scratch, saving them time and effort. They can focus on the core features and functionalities of their application, instead of wasting time on low-level details.
SDK
documentation_sdk_go
/documentation/products/sdk/go/
/documentacao/produtos/sdk/go/
false

Azion Go SDK

With an SDK, developers don't have to build everything from scratch, saving them time and effort. They can focus on the core features and functionalities of their application, instead of wasting time on low-level details.

The Azion API 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.

  1. Pre-requisites
  2. Downloading and installing Azion API SDK
  3. Instantiating a Client
  4. Creating an iDNS zone
  5. Complete example
  6. Available methods

Pre-requisites {#pre-requisites}

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 nameofthemodule.

Downloading and installing Azion API SDK {#using-sdk}

The AzionAPI 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

Instantiating a Client {#instantiate-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:

  1. Import the package you want to use. In this example, we're going to create a new iDNS zone.
    import(
        sdk "github.com/aziontech/azionapi-go-sdk/idns"
    )

{:start="2"}

  1. 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 iDNS zone {#creating-idnszone}

Let's take a look at the creation of an iDNS 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
    }

Complete example {#example}

At your-project/main.go

    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 {
        panic(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 {
        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 {
        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),
        }
    }

Available methods {#available-methods}

The Azion API SDK offers management to the following services:

API DESCRIPTION
Domains Domains API enables you to retrieve, create, remove or update Domains used by Edge Applications.
Edge Applications Edge Applications API allows you to check, remove and/or update your existing settings, as well as creating new ones.
Edge Functions Edge Functions API.
Edge Services Edge Services API, part of the Edge Orchestrator.
Intelligent DNS Intelligent DNS API.
Real-Time Purge Real-Time Purge API enables you to purge a cache entry before its TTL for Edge Caching or L2 Caching.
Storage-API General-purpose KV-Storage for Azion.

Related Documentation