Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly handle some errors #187

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion api/client/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ func (c *BaseClient) Get(kind string, resource string) ([]byte, int, error) {
log.Printf("GET %s\n", url)

req, err := http.NewRequest("GET", url, nil)
if err != nil {
return []byte(""), 0, err
}

req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Token %s", c.authToken))
Expand Down Expand Up @@ -87,6 +90,9 @@ func (c *BaseClient) List(kind string) ([]byte, int, error) {
log.Printf("GET %s\n", url)

req, err := http.NewRequest("GET", url, nil)
if err != nil {
return []byte(""), 0, err
}

req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Token %s", c.authToken))
Expand Down Expand Up @@ -117,6 +123,9 @@ func (c *BaseClient) ListWithParams(kind string, query url.Values) ([]byte, int,
log.Printf("GET %s\n", url)

req, err := http.NewRequest("GET", url, nil)
if err != nil {
return []byte(""), 0, err
}

req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Token %s", c.authToken))
Expand Down Expand Up @@ -147,6 +156,9 @@ func (c *BaseClient) Delete(kind string, name string) ([]byte, int, error) {
log.Printf("DELETE %s\n", url)

req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
return []byte(""), 0, err
}

req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Token %s", c.authToken))
Expand Down Expand Up @@ -187,6 +199,9 @@ func (c *BaseClient) PostHeaders(kind string, resource []byte, headers map[strin
log.Println("Resource", string(resource))

req, err := http.NewRequest("POST", url, bytes.NewBuffer(resource))
if err != nil {
return []byte(""), 0, err
}

req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Token %s", c.authToken))
Expand Down Expand Up @@ -221,6 +236,9 @@ func (c *BaseClient) Patch(kind string, name string, resource []byte) ([]byte, i
log.Printf("PATCH %s\n", url)

req, err := http.NewRequest("PATCH", url, bytes.NewBuffer(resource))
if err != nil {
return []byte(""), 0, err
}

req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Token %s", c.authToken))
Expand Down Expand Up @@ -259,9 +277,15 @@ func newfileUploadRequest(uri string, args map[string]string, fileArgName, path
return nil, err
}
_, err = io.Copy(part, file)
if err != nil {
return nil, err
}

for key, val := range args {
_ = writer.WriteField(key, val)
err = writer.WriteField(key, val)
if err != nil {
return nil, err
}
}
err = writer.Close()
if err != nil {
Expand Down
Loading