-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
62 lines (53 loc) · 1.07 KB
/
client.go
File metadata and controls
62 lines (53 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package gosMAP
import (
"fmt"
"net/http"
"encoding/json"
"io/ioutil"
"github.com/bradfitz/gomemcache/memcache"
)
type Connection struct{
Url string
APIkey string
Mc *memcache.Client
}
type rootPage struct{
Contents []string
}
func validateConnection(conn Connection)(error){
// is url valid
response, err := http.Get(conn.Url)
if err != nil {
return err
}
// Is the data in a json form we expect?
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
}
var m rootPage
err = json.Unmarshal(contents, &m)
if err != nil {
return err
}
if len(m.Contents)>0 && m.Contents[0] == "add" {
return nil
}else{
return fmt.Errorf("Not Valid sMAP Archiver")
}
}
func Connect(url string, key string)(Connection, error){
if url[len(url)-1] != '/'{
url = url + "/"
}
conn := Connection{
Url:url,
APIkey:key,
}
err := validateConnection(conn)
return conn, err
}
func (conn *Connection) ConnectMemcache(server string){
conn.Mc = memcache.New(server)
}