Skip to content

Commit

Permalink
Client works
Browse files Browse the repository at this point in the history
  • Loading branch information
bitfield committed Jun 13, 2018
1 parent 866f666 commit 1809fc2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
29 changes: 23 additions & 6 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package uptimerobot

import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"time"
)

Expand All @@ -18,6 +20,16 @@ type Client struct {
http HTTPClient
}

// Error represents an API error.
type Error map[string]interface{}

// Respone represents an API response.
type Response struct {
Stat string `json:"stat"`
Account Account `json:"account"`
Error Error `json:"error"`
}

// Account represents an UptimeRobot account.
type Account struct {
Email string `json:"email"`
Expand All @@ -43,12 +55,12 @@ func (c *Client) GetAccountDetails() (Account, error) {
Host: "api.uptimerobot.com",
Path: "/v2/getAccountDetails",
}
q := u.Query()
q.Set("api_key", c.apiKey)
q.Set("format", "json")
q.Set("noJsonCallback", "1")
u.RawQuery = q.Encode()
req, err := http.NewRequest("POST", u.String(), nil)
form := url.Values{}
form.Add("api_key", c.apiKey)
form.Add("format", "json")
req, err := http.NewRequest("POST", u.String(), strings.NewReader(form.Encode()))
req.Header.Add("content-type", "application/x-www-form-urlencoded")

if err != nil {
return Account{}, err
}
Expand All @@ -60,9 +72,14 @@ func (c *Client) GetAccountDetails() (Account, error) {
r := struct {
Stat string `json:"stat"`
Account Account `json:"account"`
Error
}{}
if err = json.NewDecoder(resp.Body).Decode(&r); err != nil {
return Account{}, err
}
if r.Stat != "ok" {
e, _ := json.MarshalIndent(r.Error, "", " ")
return Account{}, fmt.Errorf("API error: %s", e)
}
return r.Account, nil
}
7 changes: 6 additions & 1 deletion cmd/uptimerobot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"log"
"os"

"github.com/bitfield/uptimerobot"
Expand All @@ -10,5 +11,9 @@ import (
func main() {
apiKey := os.Args[1]
utr := uptimerobot.New(apiKey)
fmt.Println(utr.GetAccountDetails())
a, err := utr.GetAccountDetails()
if err != nil {
log.Fatal(err)
}
fmt.Println(a)
}

0 comments on commit 1809fc2

Please sign in to comment.