Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
UltraNemesis authored Mar 15, 2018
1 parent 48ce2b0 commit 60f58c2
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 24 deletions.
11 changes: 5 additions & 6 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ const (
)

const (
wptStatusOk = 200
wptStatusTestRunning = 100
wptStatusTestQueued = 101
wptStatusTestSuccess = 200
wptStatusTestNotFound = 400
wptStatusTestCancelled = 402
)

var (
Expand Down Expand Up @@ -350,11 +354,6 @@ type HistoryItem struct {
Label string `csv:"Label"`
}

type Test struct {
Client *Client
Options *TestOptions
}

type TestOptions struct {
URL string `url:"url"`
Label string `url:"label,omitempty"`
Expand Down
6 changes: 3 additions & 3 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (
"github.com/gocarina/gocsv"
)

func parseData(data []byte, format string, response interface{}) error {
func parseData(data string, format string, response interface{}) error {
var err error = nil

switch format {
case "json":
err = json.Unmarshal(data, response)
err = json.Unmarshal([]byte(data), response)
case "csv":
err = gocsv.UnmarshalBytes(data, response)
err = gocsv.UnmarshalString(data, response)
default:
log.Println("Unknown format : ", format)
}
Expand Down
75 changes: 60 additions & 15 deletions wpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,38 @@ import (
"net/http"
"net/url"
"strconv"
"time"

"github.com/google/go-querystring/query"
)

type Options struct {
const (
DefualtStatusPollingInterval = 5
DefaultTestTimeout = 30
)

var (
StatusPollingInterval time.Duration = DefualtStatusPollingInterval
TestTimeout time.Duration = DefaultTestTimeout
)

type ClientOptions struct {
URL *url.URL
APIKey string
}

type Client struct {
options *Options
options *ClientOptions
httpClient *http.Client
}

func NewClient(options *Options) (*Client, error) {
type Test struct {
client *Client
options *TestOptions
Response *TestResponse
}

func NewClient(options *ClientOptions) (*Client, error) {

if options.URL == nil {
options.URL, _ = url.Parse(defaultURL)
Expand All @@ -37,21 +54,21 @@ func NewClient(options *Options) (*Client, error) {

func (c *Client) NewTest(options *TestOptions) (*Test, error) {
return &Test{
Client: c,
Options: options,
client: c,
options: options,
}, nil
}

func (t *Test) Run() (*TestResponse, error) {
v, _ := query.Values(t.Options)
v, _ := query.Values(t.options)

if len(t.Client.options.APIKey) > 0 {
v.Add("k", t.Client.options.APIKey)
if len(t.client.options.APIKey) > 0 {
v.Add("k", t.client.options.APIKey)
}

v.Add("f", "json")

resp, err := t.Client.query(wptQueryRunTest, v)
resp, err := t.client.query(wptQueryRunTest, v)

var response TestResponse

Expand All @@ -62,9 +79,37 @@ func (t *Test) Run() (*TestResponse, error) {
parseData(resp, v.Get("f"), &response)
}

t.Response = &response

return &response, err
}

func (t *Test) RunSync() {
t.Run()
t.monitor()
}

func (t *Test) monitor() {
for {
select {

default:
time.Sleep(StatusPollingInterval * time.Second)

status, _ := t.client.GetStatus(t.Response.Data.TestId)

log.Println(status.StatusCode)

switch status.StatusCode {
case wptStatusTestSuccess, wptStatusTestNotFound, wptStatusTestCancelled:
log.Println("Exiting")
return
}

}
}
}

func (c *Client) GetLocations() (*WPTLocations, error) {
v := url.Values{}
v.Add("f", "json")
Expand Down Expand Up @@ -143,7 +188,7 @@ func (c *Client) CancelTest(testId string) error {
return err
}

func (c *Client) query(path string, values url.Values) ([]byte, error) {
func (c *Client) query(path string, values url.Values) (string, error) {
url := c.options.URL
url.Path = path
url.RawQuery = values.Encode()
Expand All @@ -153,28 +198,28 @@ func (c *Client) query(path string, values url.Values) ([]byte, error) {
log.Println("Making Request : ", url.String())

if err != nil {
return nil, errCreateRequest
return "", errCreateRequest
}

resp, err := c.httpClient.Do(req)

if err != nil {
return nil, errQueryServer
return "", errQueryServer
}

if resp.StatusCode != http.StatusOK {
return nil, errBadResponse
return "", errBadResponse
}

defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)

if err != nil {
return nil, errReadBody
return "", errReadBody
}

//response = parse(body, values.Get("f"), response)

return body, nil
return string(body), nil
}

0 comments on commit 60f58c2

Please sign in to comment.