Skip to content

Commit

Permalink
Usage of constants (#31)
Browse files Browse the repository at this point in the history
* Usage of constants

Signed-off-by: Matthias Wessendorf <mwessend@redhat.com>

* 💄 run make fmt

Signed-off-by: Matthias Wessendorf <mwessend@redhat.com>

Co-authored-by: Amir Mofasser <amimof@users.noreply.github.com>
  • Loading branch information
matzew and amimof authored Feb 2, 2021
1 parent bc3c602 commit 9410002
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions huego.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import (
"strings"
)

const (
applicationJSON = "application/json"
contentType = "Content-Type"
)

// APIResponse holds the response data returned form the bridge after a request has been made.
type APIResponse struct {
Success map[string]interface{} `json:"success,omitempty"`
Expand Down Expand Up @@ -82,7 +87,7 @@ func unmarshal(data []byte, v interface{}) error {

func get(ctx context.Context, url string) ([]byte, error) {

req, err := http.NewRequest("GET", url, nil)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -110,14 +115,14 @@ func put(ctx context.Context, url string, data []byte) ([]byte, error) {

body := strings.NewReader(string(data))

req, err := http.NewRequest("PUT", url, body)
req, err := http.NewRequest(http.MethodPut, url, body)
if err != nil {
return nil, err
}

req = req.WithContext(ctx)

req.Header.Set("Content-Type", "application/json")
req.Header.Set(contentType, applicationJSON)

client := http.Client{}
res, err := client.Do(req)
Expand All @@ -140,14 +145,14 @@ func post(ctx context.Context, url string, data []byte) ([]byte, error) {

body := strings.NewReader(string(data))

req, err := http.NewRequest("POST", url, body)
req, err := http.NewRequest(http.MethodPost, url, body)
if err != nil {
return nil, err
}

req = req.WithContext(ctx)

req.Header.Set("Content-Type", "application/json")
req.Header.Set(contentType, applicationJSON)

client := http.Client{}
res, err := client.Do(req)
Expand All @@ -168,14 +173,14 @@ func post(ctx context.Context, url string, data []byte) ([]byte, error) {

func delete(ctx context.Context, url string) ([]byte, error) {

req, err := http.NewRequest("DELETE", url, nil)
req, err := http.NewRequest(http.MethodDelete, url, nil)
if err != nil {
return nil, err
}

req = req.WithContext(ctx)

req.Header.Set("Content-Type", "application/json")
req.Header.Set(contentType, applicationJSON)

client := http.Client{}
res, err := client.Do(req)
Expand Down Expand Up @@ -204,7 +209,7 @@ func DiscoverAll() ([]Bridge, error) {
// DiscoverAllContext returns a list of Bridge objects.
func DiscoverAllContext(ctx context.Context) ([]Bridge, error) {

req, err := http.NewRequest("GET", "https://discovery.meethue.com", nil)
req, err := http.NewRequest(http.MethodGet, "https://discovery.meethue.com", nil)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 9410002

Please sign in to comment.