Skip to content

Commit

Permalink
fix by comments
Browse files Browse the repository at this point in the history
  • Loading branch information
gongweibao committed Jun 2, 2017
1 parent ea31b78 commit 94e35bc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 51 deletions.
5 changes: 3 additions & 2 deletions go/paddlecloud/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"flag"
"fmt"
"net/url"
"os"
"strings"
"text/tabwriter"
Expand Down Expand Up @@ -60,8 +61,8 @@ func (p *GetCommand) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{
}

func workers(jobname string) error {
queryMap := make(map[string]string)
queryMap["jobname"] = jobname
var queryMap url.Values
queryMap.Add("jobname", jobname)
respBody, err := GetCall(config.ActiveConfig.Endpoint+"/api/v1/workers/", queryMap)
if err != nil {
fmt.Fprintf(os.Stderr, "error getting workers: %v\n", err)
Expand Down
10 changes: 6 additions & 4 deletions go/paddlecloud/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"flag"
"fmt"
"net/url"
"os"
"strconv"

Expand Down Expand Up @@ -43,11 +44,12 @@ func (p *LogsCommand) Execute(_ context.Context, f *flag.FlagSet, _ ...interface
f.Usage()
return subcommands.ExitFailure
}
queryMap := make(map[string]string)

queryMap["n"] = strconv.FormatInt(int64(p.n), 10)
queryMap["w"] = p.w
queryMap["jobname"] = f.Arg(0)
var queryMap url.Values
queryMap.Add("n", strconv.FormatInt(int64(p.n), 10))
queryMap.Add("w", p.w)
queryMap.Add("jobname", f.Arg(0))

respBody, err := GetCall(config.ActiveConfig.Endpoint+"/api/v1/logs", queryMap)
if err != nil {
fmt.Fprintf(os.Stderr, "call paddle cloud error %v", err)
Expand Down
60 changes: 15 additions & 45 deletions go/paddlecloud/restclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,7 @@ import (
// HTTPOK is ok status of http api call.
const HTTPOK = "200 OK"

type RestClient struct {
client *http.Client
}

// NewRestClient returns a new RestClient struct.
func NewRestClient() *RestClient {
client := http.Client{Transport: &http.Transport{}}
return &RestClient{client: &client}
}
var httpClient = &http.Client{Transport: &http.Transport{}}

func makeRequest(uri string, method string, body io.Reader,
contentType string, query url.Values,
Expand Down Expand Up @@ -64,8 +56,8 @@ func makeRequestToken(uri string, method string, body io.Reader,

// NOTE: add other request makers if we need other auth methods.

func (p *RestClient) getResponse(req *http.Request) ([]byte, error) {
resp, err := p.client.Do(req)
func getResponse(req *http.Request) ([]byte, error) {
resp, err := httpClient.Do(req)
if err != nil {
return []byte{}, err
}
Expand All @@ -78,34 +70,34 @@ func (p *RestClient) getResponse(req *http.Request) ([]byte, error) {
}

// GetCall make a GET call to targetURL with query.
func (p *RestClient) GetCall(targetURL string, query url.Values) ([]byte, error) {
func GetCall(targetURL string, query url.Values) ([]byte, error) {
req, err := makeRequestToken(targetURL, "GET", nil, "", query)
if err != nil {
return []byte{}, err
}
return p.getResponse(req)
return getResponse(req)
}

// PostCall make a POST call to targetURL with a json body.
func (p *RestClient) PostCall(targetURL string, jsonString []byte) ([]byte, error) {
func PostCall(targetURL string, jsonString []byte) ([]byte, error) {
req, err := makeRequestToken(targetURL, "POST", bytes.NewBuffer(jsonString), "", nil)
if err != nil {
return []byte{}, err
}
return p.getResponse(req)
return getResponse(req)
}

// DeleteCall make a DELETE call to targetURL with a json body.
func (p *RestClient) DeleteCall(targetURL string, jsonString []byte) ([]byte, error) {
func DeleteCall(targetURL string, jsonString []byte) ([]byte, error) {
req, err := makeRequestToken(targetURL, "DELETE", bytes.NewBuffer(jsonString), "", nil)
if err != nil {
return []byte{}, err
}
return p.getResponse(req)
return getResponse(req)
}

// PostFile make a POST call to HTTP server to upload a file.
func (p *RestClient) PostFile(targetURL string, filename string) ([]byte, error) {
func PostFile(targetURL string, filename string) ([]byte, error) {
bodyBuf := &bytes.Buffer{}
bodyWriter := multipart.NewWriter(bodyBuf)

Expand Down Expand Up @@ -136,11 +128,11 @@ func (p *RestClient) PostFile(targetURL string, filename string) ([]byte, error)
if err != nil {
return []byte{}, err
}
return p.getResponse(req)
return getResponse(req)
}

// PostChunkData makes a POST call to HTTP server to upload chunkdata.
func (p *RestClient) PostChunk(targetURL string,
func PostChunk(targetURL string,
chunkName string, reader io.Reader, len int64, boundary string) ([]byte, error) {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
Expand All @@ -166,38 +158,16 @@ func (p *RestClient) PostChunk(targetURL string,
return []byte{}, err
}

return p.getResponse(req)
return getResponse(req)
}

// GetChunkData makes a GET call to HTTP server to download chunk data.
func (p *RestClient) GetChunk(targetURL string,
func GetChunk(targetURL string,
query url.Values) (*http.Response, error) {
req, err := makeRequestToken(targetURL, "GET", nil, "", query)
if err != nil {
return nil, err
}

return p.client.Do(req)
}

var DefaultClient = NewRestClient()

// GetCall makes a GET call to targetURL with k-v params of query.
func GetCall(targetURL string, query map[string]string) ([]byte, error) {
q := url.Values{}
for k, v := range query {
q.Add(k, v)
}

return DefaultClient.GetCall(targetURL, q)
}

// PostCall makes a POST call to targetURL with a json body.
func PostCall(targetURL string, jsonString []byte) ([]byte, error) {
return DefaultClient.PostCall(targetURL, jsonString)
}

// DeleteCall makes a DELETE call to targetURL with a json body.
func DeleteCall(targetURL string, jsonString []byte) ([]byte, error) {
return DefaultClient.DeleteCall(targetURL, jsonString)
return httpClient.Do(req)
}

0 comments on commit 94e35bc

Please sign in to comment.