Last active
August 29, 2015 13:57
-
-
Save theophoric/9380822 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"encoding/hex" | |
"fmt" | |
"io" | |
"io/ioutil" | |
"net/http" | |
"github.com/ugorji/go/codec" | |
) | |
var ( | |
mh codec.MsgpackHandle | |
rootToken string | |
accountToken string | |
secret = "super-secret-key" | |
slug = "test" | |
mac = "00212effff004b0d" | |
) | |
func decodeHexBody(bodyReader io.ReadCloser) (args map[string][]interface{}, err error) { | |
defer bodyReader.Close() | |
// args = []interface{}{} | |
body, err := ioutil.ReadAll(bodyReader) | |
if err != nil { | |
return | |
} | |
fmt.Println("\nBODY:", body) | |
decodedBody, err := hex.DecodeString(string(body)) | |
if err != nil { | |
fmt.Println("\nERROR AT HEX DECODESTRING: ", err) | |
} | |
fmt.Println("\nDecoded Body:", decodedBody) | |
dec := codec.NewDecoder(bytes.NewBuffer(decodedBody), &mh) | |
err = dec.Decode(&args) | |
if err != nil { | |
fmt.Println("\nERROR AT MSGPACK DECODE: ", err) | |
} | |
return | |
} | |
func encodeHexBody(body interface{}) (responseBytes []byte, err error) { | |
resWriter := new(bytes.Buffer) | |
enc := codec.NewEncoder(resWriter, &mh) | |
enc.Encode(&body) | |
responseBytes = make([]byte, hex.EncodedLen(len(resWriter.Bytes()))) | |
hex.Encode(responseBytes, resWriter.Bytes()) | |
fmt.Println("\nresponseBytes:\t", responseBytes) | |
return | |
} | |
func main() { | |
mh.RawToString = true | |
var ( | |
// args0 map[string][]interface{} | |
// args1 map[string][]interface{} | |
// args2 map[string][]interface{} | |
args3 map[string][]interface{} | |
) | |
var err error | |
// args0, err = rootLoginReq() | |
// if err != nil { | |
// fmt.Println("\nERROR:\t", err) | |
// return | |
// } | |
// fmt.Println("\n\nargs=", args0) | |
// fmt.Println("\nErr0:", args0["Err"]) | |
// fmt.Println("\nOutputs0:", args0["Outputs"]) | |
// args1, err = rootCreateAccountReq() | |
// if err != nil { | |
// fmt.Println("\nERROR:\t", err) | |
// return | |
// } | |
// fmt.Println("\n\nargs=", args1) | |
// fmt.Println("\nErr1:", args1["Err"]) | |
// fmt.Println("\nOutputs1:", args1["Outputs"]) | |
// rootToken, _ = args0["Outputs"][0].(string) | |
// fmt.Print("\nrootToken:", rootToken) | |
// args2, err = accountsCreateDeviceReq() | |
// if err != nil { | |
// fmt.Println("\nERROR:\t", err) | |
// return | |
// } | |
// fmt.Println("\nErr2:", args2["Err"]) | |
// fmt.Println("\nOutputs2:", args2["Outputs"]) | |
args3, err = accountsDevicesReq() | |
if err != nil { | |
fmt.Println("\nERROR:\t", err) | |
return | |
} | |
fmt.Println("\nErr3:", args3["Err"]) | |
fmt.Println("\nOutputs3:", args3["Outputs"]) | |
} | |
func makeReq(req *http.Request) (res *http.Response, err error) { | |
client := &http.Client{} | |
return client.Do(req) | |
} | |
func rootLoginReq() (map[string][]interface{}, error) { | |
args := []interface{}{} | |
args = append(args, secret) | |
var ( | |
// | |
// h = &mh | |
) | |
// enc := codec.NewEncoder(w, h) | |
// enc.Encode(&args) | |
b, err := encodeHexBody(args) | |
// req, err := http.NewRequest("POST", "http://127.0.0.1:8095/w-root-v0/root/login", w) | |
req, err := http.NewRequest("POST", "http://root.a.helium.co:8095/w-root-v0/root/login", bytes.NewReader(b)) | |
if err != nil { | |
return nil, err | |
} | |
res, err := makeReq(req) | |
if err != nil { | |
return nil, err | |
} | |
return decodeHexBody(res.Body) | |
} | |
func rootCreateAccountReq() (map[string][]interface{}, error) { | |
args := []interface{}{} | |
var ( | |
name string = "Test Account" | |
email string = "element@helium.co" | |
) | |
args = append(args, name, slug, email, secret) | |
b, err := encodeHexBody(args) | |
req, err := http.NewRequest("POST", "http://root.a.helium.co:8095/w-root-v0/root/createaccount", bytes.NewReader(b)) | |
if err != nil { | |
return nil, err | |
} | |
req.Header.Add("x-helium-token", rootToken) | |
res, err := makeReq(req) | |
if err != nil { | |
return nil, err | |
} | |
return decodeHexBody(res.Body) | |
} | |
func accountsCreateDeviceReq() (map[string][]interface{}, error) { | |
args := []interface{}{} | |
var ( | |
name string = "theo's nike fuelband" | |
) | |
args = append(args, name, mac) | |
b, err := encodeHexBody(args) | |
url := fmt.Sprintf("http://%s.a.helium.co:8095/w-root-v0/accounts/createdevice", slug) | |
req, err := http.NewRequest("POST", url, bytes.NewReader(b)) | |
if err != nil { | |
return nil, err | |
} | |
req.Header.Add("x-helium-token", accountToken) | |
res, err := makeReq(req) | |
if err != nil { | |
return nil, err | |
} | |
return decodeHexBody(res.Body) | |
} | |
func accountsDevicesReq() (map[string][]interface{}, error) { | |
args := []interface{}{} | |
b, err := encodeHexBody(args) | |
url := fmt.Sprintf("http://%s.d.helium.co:8095/w-root-v0/accounts/apis", "helium") | |
req, err := http.NewRequest("GET", url, bytes.NewReader(b)) | |
if err != nil { | |
return nil, err | |
} | |
req.Header.Add("x-helium-token", accountToken) | |
res, err := makeReq(req) | |
if err != nil { | |
return nil, err | |
} | |
return decodeHexBody(res.Body) | |
} | |
func devicesPongReq() (map[string][]interface{}, error) { | |
args := []interface{}{} | |
b, err := encodeHexBody(args) | |
url := fmt.Sprintf("http://%s.a.helium.co:8095/w-root-v0/devices/pong", mac) | |
req, err := http.NewRequest("POST", url, bytes.NewReader(b)) | |
if err != nil { | |
return nil, err | |
} | |
req.Header.Add("x-helium-token", accountToken) | |
res, err := makeReq(req) | |
if err != nil { | |
return nil, err | |
} | |
return decodeHexBody(res.Body) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment