Refactor debug to dev

This commit is contained in:
Mike Cheng 2015-05-02 21:56:41 -04:00
parent d7ed09175c
commit 217aef902b
4 changed files with 18 additions and 8 deletions

View file

@ -34,10 +34,14 @@ type resultResponse struct {
Message string `json:"message"`
}
func NewAPI(c Config) API {
a := API{username: c.Username, token: c.Token}
// Constructs a new Name.com API
//
// If dev = true, then the API uses the development
// API, instead of the production API.
func NewNameAPI(username, token string, dev bool) API {
a := API{username: username, token: token}
if c.Debug {
if dev {
a.baseUrl = devUrl
} else {
a.baseUrl = productionUrl
@ -46,6 +50,11 @@ func NewAPI(c Config) API {
return a
}
// Constructs a new Name.com API from a configuration
func NewAPIFromConfig(c Config) API {
return NewNameAPI(c.Username, c.Token, c.Dev)
}
func (api API) performRequest(method, url string, body io.Reader) (response []byte, err error) {
var client http.Client
req, err := http.NewRequest(method, url, body)
@ -65,6 +74,7 @@ func (api API) performRequest(method, url string, body io.Reader) (response []by
return ioutil.ReadAll(resp.Body)
}
// Returns a slice of DNS records associated with a given hostname.
func (api API) GetRecords(hostname string) (records []DNSRecord, err error) {
resp, err := api.performRequest("GET", fmt.Sprintf("%s%s%s", api.baseUrl, "api/dns/list/", hostname), nil)
if err != nil {

View file

@ -6,7 +6,7 @@ import (
)
type Config struct {
Debug bool `json:"debug"`
Dev bool `json:"dev"`
Domains []string `json:"domains"`
Interval int `json:"interval"`
Token string `json:"token"`

View file

@ -10,10 +10,10 @@ var expectedConfigs []Config
func init() {
expectedConfigs = []Config{
Config{
Username: "debug-account",
Username: "dev-account",
Token: "asdasdasdasdasdad",
Interval: 60,
Debug: true,
Dev: true,
Domains: []string{"test.com", "fake.com"},
},
Config{

View file

@ -1,10 +1,10 @@
{
"configs": [
{
"username": "debug-account",
"username": "dev-account",
"token": "asdasdasdasdasdad",
"interval": 60,
"debug": true,
"dev": true,
"domains": [ "test.com", "fake.com" ]
},
{