项目作者: dnsimple

项目描述 :
The DNSimple API client for Go.
高级语言: Go
项目地址: git://github.com/dnsimple/dnsimple-go.git
创建时间: 2014-12-12T21:32:30Z
项目社区:https://github.com/dnsimple/dnsimple-go

开源协议:MIT License

下载


DNSimple Go Client

A Go client for the DNSimple API v2.

Build Status
GoDoc

Installation

  1. go get github.com/dnsimple/dnsimple-go/v4/dnsimple

Usage

This library is a Go client you can use to interact with the DNSimple API v2. Here are some examples.

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "strconv"
  7. "github.com/dnsimple/dnsimple-go/v4/dnsimple"
  8. )
  9. func main() {
  10. tc := dnsimple.StaticTokenHTTPClient(context.Background(), "your-token")
  11. // new client
  12. client := dnsimple.NewClient(tc)
  13. // get the current authenticated account (if you don't know who you are)
  14. whoamiResponse, err := client.Identity.Whoami(context.Background())
  15. if err != nil {
  16. fmt.Printf("Whoami() returned error: %v\n", err)
  17. os.Exit(1)
  18. }
  19. fmt.Println(whoamiResponse.Data.Account)
  20. fmt.Println(whoamiResponse.Data.User)
  21. // either assign the account ID or fetch it from the response
  22. // if you are authenticated with an account token
  23. accountID := strconv.FormatInt(whoamiResponse.Data.Account.ID, 10)
  24. // get the list of domains
  25. domainsResponse, err := client.Domains.ListDomains(context.Background(), accountID, nil)
  26. if err != nil {
  27. fmt.Printf("Domains.ListDomains() returned error: %v\n", err)
  28. os.Exit(1)
  29. }
  30. // iterate over all the domains in the
  31. // paginated response.
  32. for _, domain := range domainsResponse.Data {
  33. fmt.Println(domain)
  34. }
  35. // List methods support a variety of options to paginate, sort and filter records.
  36. // Here's a few example:
  37. // get the list of domains filtered by name and sorted by expiration
  38. client.Domains.ListDomains(context.Background(), accountID, &dnsimple.DomainListOptions{NameLike: dnsimple.String("com"), ListOptions: {Sort: dnsimple.String("expiration:DESC")}})
  39. }

For more complete documentation, see godoc.

Authentication

When creating a new client you are required to provide an http.Client to use for authenticating the requests.
Supported authentication mechanisms are OAuth and HTTP Digest. We provide convenient helpers to generate a preconfigured HTTP client.

Authenticating with OAuth

  1. tc := dnsimple.StaticTokenHTTPClient(context.Background(), "your-token")
  2. // new client
  3. client := dnsimple.NewClient(tc)

Authenticating with HTTP Basic Auth

  1. hc := dnsimple.BasicAuthHTTPClient(context.Background(), "your-user", "your-password")
  2. client := dnsimple.NewClient(hc)

For requests made to authorize OAuth access, and to exchange the short lived authorization token for the OAuth token, use an HTTP client with a timeout:

  1. client := dnsimple.NewClient(&http.Client{Timeout: time.Second * 10})

For any other custom need you can define your own http.RoundTripper implementation and
pass a client that authenticated with the custom round tripper.

Sandbox Environment

We highly recommend testing against our sandbox environment before using our production environment. This will allow you to avoid real purchases, live charges on your credit card, and reduce the chance of your running up against rate limits.

The client supports both the production and sandbox environment. To switch to sandbox pass the sandbox API host using the base_url option when you construct the client:

  1. client := dnsimple.NewClient(tc)
  2. client.BaseURL = "https://api.sandbox.dnsimple.com"

You will need to ensure that you are using an access token created in the sandbox environment. Production tokens will not work in the sandbox environment.

Setting a custom User-Agent header

You can customize the User-Agent header for the calls made to the DNSimple API:

  1. client := dnsimple.NewClient(tc)
  2. client.SetUserAgent("my-app/1.0")

The value you provide will be prepended to the default User-Agent the client uses. For example, if you use my-app/1.0, the final header value will be my-app/1.0 dnsimple-go/0.14.0 (note that it will vary depending on the client version).

We recommend to customize the user agent. If you are building a library or integration on top of the official client, customizing the client will help us to understand what is this client used for, and allow to contribute back or get in touch.

Contributing

For instructions about contributing and testing, visit the CONTRIBUTING file.

License

Copyright (c) 2014-2024 DNSimple Corporation. This is Free Software distributed under the MIT license.