Skip to content

Commit 39821e3

Browse files
authored
Merge pull request #7 from dshafik/add-client
[WIP] Add edgegrid.Client
2 parents 2281412 + 5f96f08 commit 39821e3

File tree

9 files changed

+282
-10
lines changed

9 files changed

+282
-10
lines changed

Godeps/_workspace/src/github.com/go-ini/ini/struct.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Godeps/_workspace/src/github.com/pmezard/go-difflib/difflib/difflib.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Godeps/_workspace/src/github.com/stretchr/testify/assert/assertions.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Godeps/_workspace/src/golang.org/x/sys/unix/syscall_darwin.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client.go

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
package edgegrid
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"errors"
7+
"io/ioutil"
8+
"net/http"
9+
"net/url"
10+
"runtime"
11+
"strings"
12+
)
13+
14+
const (
15+
libraryVersion = "0.1.0"
16+
)
17+
18+
type Client struct {
19+
http.Client
20+
21+
// HTTP client used to communicate with the Akamai APIs.
22+
//client *http.Client
23+
24+
// Base URL for API requests.
25+
BaseURL *url.URL
26+
27+
// User agent for client
28+
UserAgent string
29+
30+
Config Config
31+
}
32+
33+
type Response http.Response
34+
type JsonBody map[string]interface{}
35+
36+
func New(httpClient *http.Client, config Config) (*Client, error) {
37+
c := NewClient(httpClient)
38+
c.Config = config
39+
40+
baseUrl, err := url.Parse("https://" + config.Host)
41+
42+
if err != nil {
43+
return nil, err
44+
}
45+
46+
c.BaseURL = baseUrl
47+
return c, nil
48+
}
49+
50+
func NewClient(httpClient *http.Client) *Client {
51+
if httpClient == nil {
52+
httpClient = http.DefaultClient
53+
}
54+
55+
client := &Client{
56+
Client: *httpClient,
57+
UserAgent: "Akamai-Open-Edgegrid-golang/" + libraryVersion +
58+
" golang/" + strings.TrimPrefix(runtime.Version(), "go"),
59+
}
60+
61+
return client
62+
}
63+
64+
// NewRequest creates an API request. A relative URL can be provided in urlStr, which will be resolved to the
65+
// BaseURL of the Client. If specified, the value pointed to by body is JSON encoded and included in as the request body.
66+
func (c *Client) NewRequest(method, urlStr string, body interface{}) (*http.Request, error) {
67+
var req *http.Request
68+
69+
urlStr = strings.TrimPrefix(urlStr, "/")
70+
71+
rel, err := url.Parse(urlStr)
72+
if err != nil {
73+
return nil, err
74+
}
75+
76+
u := c.BaseURL.ResolveReference(rel)
77+
78+
req, err = http.NewRequest(method, u.String(), nil)
79+
80+
req.Header.Add("User-Agent", c.UserAgent)
81+
82+
return req, nil
83+
}
84+
85+
func (c *Client) NewJsonRequest(method, urlStr string, body interface{}) (*http.Request, error) {
86+
buf := new(bytes.Buffer)
87+
err := json.NewEncoder(buf).Encode(body)
88+
if err != nil {
89+
return nil, err
90+
}
91+
92+
req, err := c.NewRequest(method, urlStr, buf)
93+
req.Header.Add("Content-Type", "application/json")
94+
req.Header.Add("Accept", "application/json,*/*")
95+
96+
return req, err
97+
}
98+
99+
func (c *Client) Do(req *http.Request) (*Response, error) {
100+
req = c.Config.AddRequestHeader(req)
101+
response, err := c.Client.Do(req)
102+
103+
if err != nil {
104+
return nil, err
105+
}
106+
107+
res := Response(*response)
108+
109+
return &res, err
110+
}
111+
112+
func (c *Client) Get(url string) (resp *Response, err error) {
113+
req, err := c.NewRequest("GET", url, nil)
114+
115+
if err != nil {
116+
return nil, err
117+
}
118+
119+
req = c.Config.AddRequestHeader(req)
120+
response, err := c.Do(req)
121+
122+
if err != nil {
123+
return nil, err
124+
}
125+
126+
res := Response(*response)
127+
128+
return &res, err
129+
}
130+
131+
func (c *Client) Post(url string, bodyType string, body interface{}) (resp *Response, err error) {
132+
var req *http.Request
133+
134+
req, err = c.NewRequest("POST", url, body)
135+
if err != nil {
136+
return nil, err
137+
}
138+
139+
req.Header.Set("Content-Type", bodyType)
140+
141+
req = c.Config.AddRequestHeader(req)
142+
response, err := c.Do(req)
143+
144+
if err != nil {
145+
return response, err
146+
}
147+
148+
res := Response(*response)
149+
150+
return &res, err
151+
}
152+
153+
func (c *Client) PostForm(url string, data url.Values) (resp *Response, err error) {
154+
return c.Post(url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
155+
}
156+
157+
func (c *Client) PostJson(url string, data interface{}) (resp *Response, err error) {
158+
buf := new(bytes.Buffer)
159+
err = json.NewEncoder(buf).Encode(data)
160+
if err != nil {
161+
return nil, err
162+
}
163+
164+
return c.Post(url, "application/json", buf)
165+
}
166+
167+
func (c *Client) Head(url string) (resp *Response, err error) {
168+
req, _ := c.NewRequest("HEAD", url, nil)
169+
if err != nil {
170+
return nil, err
171+
}
172+
173+
response, err := c.Do(req)
174+
if err != nil {
175+
return nil, err
176+
}
177+
178+
res := Response(*response)
179+
return &res, err
180+
}
181+
182+
func (r *Response) BodyJson(data interface{}) error {
183+
if data == nil {
184+
return errors.New("You must pass in an interface{}")
185+
}
186+
187+
body, _ := ioutil.ReadAll(r.Body)
188+
err := json.Unmarshal(body, &data)
189+
190+
return err
191+
}

edgegrid.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@ func (c *Config) createAuthHeader(req *http.Request, timestamp string, nonce str
194194

195195
// AddRequestHeader sets the authorization header to use Akamai Open API
196196
func AddRequestHeader(c Config, req *http.Request) *http.Request {
197+
return c.AddRequestHeader(req)
198+
}
199+
200+
func (c Config) AddRequestHeader(req *http.Request) *http.Request {
197201
if c.Debug {
198202
log.SetLevel(log.DebugLevel)
199203
}

examples/client.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/akamai-open/AkamaiOPEN-edgegrid-golang"
6+
"log"
7+
"math/rand"
8+
"net/url"
9+
"time"
10+
)
11+
12+
func random(min int, max int) int {
13+
rand.Seed(time.Now().UTC().UnixNano())
14+
random := rand.Intn(max-min) + min
15+
16+
return random
17+
}
18+
19+
type LocationsResponse struct {
20+
Locations []string `json:locations`
21+
}
22+
23+
type DigResponse struct {
24+
Dig struct {
25+
Hostname string `json:hostname`
26+
QueryType string `json:queryType`
27+
Result string `json:result`
28+
ErrorString string `json:errorString`
29+
} `json:dig`
30+
}
31+
32+
func main() {
33+
config, err := edgegrid.Init("~/.edgerc", "default")
34+
//config.Debug = true
35+
if err == nil {
36+
client, err := edgegrid.New(nil, config)
37+
if err == nil {
38+
fmt.Println("Requesting locations that support the diagnostic-tools API.")
39+
40+
res, err := client.Get("/diagnostic-tools/v1/locations")
41+
if err != nil {
42+
log.Fatal(err.Error())
43+
}
44+
45+
locationsResponse := LocationsResponse{}
46+
res.BodyJson(&locationsResponse)
47+
48+
if err != nil {
49+
log.Fatal(err.Error())
50+
}
51+
52+
fmt.Printf("There are %d locations that can run dig in the Akamai Network\n", len(locationsResponse.Locations))
53+
54+
if len(locationsResponse.Locations) == 0 {
55+
log.Fatal("No locations found")
56+
}
57+
58+
location := locationsResponse.Locations[random(0, len(locationsResponse.Locations))-1]
59+
60+
fmt.Println("We will make our call from " + location)
61+
62+
fmt.Println("Running dig from " + location)
63+
64+
client.Timeout = 5 * time.Minute
65+
res, err = client.Get("/diagnostic-tools/v1/dig?hostname=developer.akamai.com&location=" + url.QueryEscape(location) + "&queryType=A")
66+
if err != nil {
67+
log.Fatal(err.Error())
68+
}
69+
70+
digResponse := DigResponse{}
71+
res.BodyJson(&digResponse)
72+
fmt.Println(digResponse.Dig.Result)
73+
} else {
74+
log.Fatal(err.Error())
75+
}
76+
}
77+
}

examples/edgegrid.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func main() {
3232

3333
if err == nil {
3434
req, _ := http.NewRequest("GET", fmt.Sprintf("https://%s/diagnostic-tools/v1/locations", config.Host), nil)
35-
req = edgegrid.AddRequestHeader(config, req)
35+
req = config.AddRequestHeader(req)
3636
resp, _ := client.Do(req)
3737
byt, _ := ioutil.ReadAll(resp.Body)
3838
fmt.Println(string(byt))

examples/edgegrid_without_configfile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func main() {
2424
}
2525

2626
req, _ := http.NewRequest("GET", fmt.Sprintf("https://%s/siteshield/v1/maps", config.Host), nil)
27-
req = edgegrid.AddRequestHeader(config, req)
27+
req = config.AddRequestHeader(req)
2828
resp, _ := client.Do(req)
2929
byt, _ := ioutil.ReadAll(resp.Body)
3030
fmt.Println(string(byt))

0 commit comments

Comments
 (0)