Skip to content

Commit

Permalink
Misc. fixes.
Browse files Browse the repository at this point in the history
Addressing #2, #3, #4.
  • Loading branch information
lleadbet committed Dec 20, 2020
1 parent 00bf9df commit d9d0510
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 21 deletions.
6 changes: 5 additions & 1 deletion cmd/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,12 @@ func init() {

apiCmd.PersistentFlags().StringArrayVarP(&queryParameters, "query-params", "q", nil, "Available multiple times. Passes in query parameters to endpoints using the format of `key=value`.")
apiCmd.PersistentFlags().StringVarP(&body, "body", "b", "", "Passes a body to the request. Alteratively supports CURL-like references to files using the format of `@data,json`.")
// default here is false to enable -p commands to toggle off without explicitly defining -p=false as -p false will not work. The below commands invert the bool to pass the true default

// default here is false to enable -p commands to toggle off without explicitly defining -p=false as -p false will not work. The below commands invert the bool to pass the true default. Deprecated, so marking as hidden in favor of the unformatted flag.
apiCmd.PersistentFlags().BoolVarP(&prettyPrint, "pretty-print", "p", false, "Whether to pretty-print API requests. Default is true.")
apiCmd.PersistentFlags().MarkHidden("pretty-print")

apiCmd.PersistentFlags().BoolVarP(&prettyPrint, "unformatted", "u", false, "Whether to have API requests come back unformatted/non-prettyprinted. Default is false.")
}

func cmdRun(cmd *cobra.Command, args []string) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func configureCmdRun(cmd *cobra.Command, args []string) {
clientSecretPrompt := promptui.Prompt{
Label: "Client Secret",
Validate: func(s string) error {
if len(s) != 30 {
if len(s) == 30 || len(s) == 31 {
return errors.New("Invalid length for Client Secret")
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions cmd/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func triggerCmdRun(cmd *cobra.Command, args []string) {
}

for i := 0; i < count; i++ {
res, err := trigger.Fire(trigger.TriggerParamaters{
res, err := trigger.Fire(trigger.TriggerParameters{
Event: args[0],
Transport: transport,
ForwardAddress: forwardAddress,
Expand All @@ -108,7 +108,7 @@ func triggerCmdRun(cmd *cobra.Command, args []string) {
}

func retriggerCmdRun(cmd *cobra.Command, args []string) {
res, err := trigger.RefireEvent(eventID, trigger.TriggerParamaters{
res, err := trigger.RefireEvent(eventID, trigger.TriggerParameters{
ForwardAddress: forwardAddress,
Secret: secret,
})
Expand Down
2 changes: 1 addition & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Allows the user to make GET calls to endpoints on Helix. Requires a logged in to
| Flag | Shorthand | Description | Example | Required? (Y/N) |
|------------------|-----------|---------------------------------------------------------------------------------------------------------------|----------------------|-----------------|
| `--query-param` | `-q` | Query parameters for the endpoint in `key=value` format. Multiple can be entered to give multiple parameters. | `get -q login=ninja` | N |
| `--pretty-print` | `-p` | Whether to pretty-print API requests. Default is `true`. | `get -p` | N |
| `--unformatted` | `-u` | Whether to return unformatted responses. Default is `false`. | `get -u` | N |

**Examples**

Expand Down
19 changes: 8 additions & 11 deletions internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@ type clientInformation struct {
}

// NewRequest is used to request data from the Twitch API using a HTTP GET request- this function is a wrapper for the apiRequest function that handles the network call
func NewRequest(method string, path string, queryParamaters []string, body []byte, prettyPrint bool) {
func NewRequest(method string, path string, queryParameters []string, body []byte, prettyPrint bool) {
client, err := getClientInformation()

if err != nil {
fmt.Println("Error fetching client information", err.Error())
}

paramaters := url.Values{}
Parameters := url.Values{}

if queryParamaters != nil {
if queryParameters != nil {
path += "?"
for _, param := range queryParamaters {
for _, param := range queryParameters {
value := strings.Split(param, "=")
paramaters.Add(value[0], value[1])
Parameters.Add(value[0], value[1])
}
path += paramaters.Encode()
path += Parameters.Encode()
}
resp, err := apiRequest(strings.ToUpper(method), baseURL+path, body, apiRequestParameters{
ClientID: client.ClientID,
Expand Down Expand Up @@ -80,9 +80,6 @@ func ValidOptions(method string) []string {
}
}

// for _, endpoint := range names {
// names = append(names, strings.Split(endpoint, "/")...)
// }
sort.Strings(names)

return names
Expand All @@ -106,7 +103,7 @@ func getClientInformation() (clientInformation, error) {
refreshToken := viper.GetString("refreshToken")

if refreshToken == "" {
log.Fatal("Please run github.com/twitchdev/twitch-cli token")
log.Fatal("Please run twitch token")
}

clientSecret := viper.GetString("clientSecret")
Expand All @@ -119,7 +116,7 @@ func getClientInformation() (clientInformation, error) {
})

if err != nil {
log.Fatal("Unable to refresh token, please rerun github.com/twitchdev/twitch-cli token", err.Error())
log.Fatal("Unable to refresh token, please rerun twitch token", err.Error())
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/events/retrigger_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/twitchdev/twitch-cli/internal/util"
)

func RefireEvent(id string, p TriggerParamaters) (string, error) {
func RefireEvent(id string, p TriggerParameters) (string, error) {
res, err := util.GetEventByID(id)
if err != nil {
return "", err
Expand Down
8 changes: 4 additions & 4 deletions internal/events/trigger_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"github.com/twitchdev/twitch-cli/internal/util"
)

// TriggerParamaters defines the parameters used to emit an event.
type TriggerParamaters struct {
// TriggerParameters defines the parameters used to emit an event.
type TriggerParameters struct {
Event string
Transport string
IsAnonymous bool
Expand All @@ -33,8 +33,8 @@ type TriggerResponse struct {
Timestamp string
}

// Fire emits an event using the TriggerParamaters defined above.
func Fire(p TriggerParamaters) (string, error) {
// Fire emits an event using the TriggerParameters defined above.
func Fire(p TriggerParameters) (string, error) {
if len(triggerTypeMap[p.Transport]) == 0 {
return "", errors.New("Invalid transport")
}
Expand Down
12 changes: 12 additions & 0 deletions internal/request/request_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package request

import (
"testing"
)

func TestNewRequest(t *testing.T) {
_, err := NewRequest("GET", "https://api.twitch.tv/helix/users", nil)
if err != nil {
t.Errorf("Received error %v for valid request", err)
}
}
19 changes: 19 additions & 0 deletions internal/util/path_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package util

import (
"strings"
"testing"
)

func TestGetApplicationDir(t *testing.T) {
dir, err := GetApplicationDir()
if err != nil {
t.Errorf("GetApplicationDir() failed with error %v", err)
}

if strings.HasSuffix(dir, ".twitch-cli") != true {
t.Errorf("GetApplicationDir() expected to end with %v, got %v", ".twitch-cli", dir)
}
}
29 changes: 29 additions & 0 deletions internal/util/random_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package util

import (
"regexp"
"testing"
)

func TestRandomUserId(t *testing.T) {
userID := RandomUserID()

if len(userID) == 0 {
t.Errorf("RandomUserID() returned string with a length of 0")
}
}

func TestRandomGUID(t *testing.T) {
guid := RandomGUID()
if len(guid) == 0 {
t.Errorf("RandomGUID() returned string with a length of 0")
}

r, _ := regexp.Compile("^[{]?[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}[}]?$")

if r.MatchString(guid) != true {
t.Errorf("RandomGUID() returned a string with value %v, which does not meet the GUID pattern", guid)
}
}
19 changes: 19 additions & 0 deletions internal/util/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package util

import (
"testing"
)

func TestVersion(t *testing.T) {
var testString = "test_version"

SetVersion(testString)

v := GetVersion()

if v != testString {
t.Errorf("Version failed, set version to %v, received %v", testString, v)
}
}

0 comments on commit d9d0510

Please sign in to comment.