Skip to content

Commit

Permalink
refactor: pointed from codebeat.
Browse files Browse the repository at this point in the history
  • Loading branch information
tamada committed May 15, 2023
1 parent a701ad7 commit 77acba6
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 32 deletions.
57 changes: 40 additions & 17 deletions bitly.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,30 +55,42 @@ func (bitly *Bitly) buildUrl(endpoint string) string {
return fmt.Sprintf("%s/%s", bitly.url, endpoint)
}

func handleGroup(config *Config, bitly *Bitly) (string, error) {
if bitly.group != "" {
return bitly.group, nil
}
gs, err := bitly.Groups(config)
if err != nil {
return "", err
}
if len(gs) == 0 {
return "", fmt.Errorf("no active groups")
}
return gs[0].Guid, nil
}

func (bitly *Bitly) List(config *Config) ([]*ShortenUrl, error) {
if bitly.group == "" {
gs, err := bitly.Groups(config)
if err != nil {
return nil, err
}
if len(gs) == 0 {
return nil, fmt.Errorf("no active groups")
}
bitly.group = gs[0].Guid
group, err := handleGroup(config, bitly)
if err != nil {
return nil, err
}
request, err := http.NewRequest("GET", bitly.buildUrl(fmt.Sprintf("/groups/%s/bitlinks?size=20", bitly.group)), nil)
request, err := http.NewRequest("GET", bitly.buildUrl(fmt.Sprintf("/groups/%s/bitlinks?size=20", group)), nil)
if err != nil {
return nil, err
}
data, err := sendRequest(request, config)
if err != nil {
return nil, err
}
return handleListResponse(data, group)
}

func handleListResponse(data []byte, group string) ([]*ShortenUrl, error) {
result := struct {
Links []*ShortenUrl `json:"links"`
}{}
err = json.Unmarshal(data, &result)
return removeDeletedLinks(result.Links, bitly.group), err
err := json.Unmarshal(data, &result)
return removeDeletedLinks(result.Links, group), err
}

func removeDeletedLinks(links []*ShortenUrl, group string) []*ShortenUrl {
Expand All @@ -102,9 +114,13 @@ func (bitly *Bitly) Shorten(config *Config, url string) (*ShortenUrl, error) {
if err != nil {
return nil, err
}
return handleShortenResponse(data)
}

func handleShortenResponse(data []byte) (*ShortenUrl, error) {
result := &ShortenUrl{}
fmt.Println("result:", string(data))
err = json.Unmarshal(data, result)
err := json.Unmarshal(data, result)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -138,15 +154,22 @@ func (bitly *Bitly) QRCode(config *Config, shortenURL string) ([]byte, error) {
}

func sendRequest(request *http.Request, config *Config) ([]byte, error) {
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", config.Token))
request.Header.Add("Content-Type", "application/json")
client := &http.Client{}
response, err := client.Do(request)
response, err := sendRequestImpl(request, config)
if err != nil {
return []byte{}, err
}
defer response.Body.Close()
return handleResponse(response)
}

func sendRequestImpl(request *http.Request, config *Config) (*http.Response, error) {
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", config.Token))
request.Header.Add("Content-Type", "application/json")
client := &http.Client{}
return client.Do(request)
}

func handleResponse(response *http.Response) ([]byte, error) {
if response.StatusCode/100 != 2 {
data, _ := io.ReadAll(response.Body)
fmt.Println("response body:", string(data))
Expand Down
36 changes: 21 additions & 15 deletions cmd/urleap/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,23 @@ type flags struct {
versionFlag bool
}

type runOpts struct {
token string
qrcode string
config string
group string
}

/*
This struct holds the values of the options.
*/
type options struct {
token string
qrcode string
config string
group string
runOpt *runOpts
flagSet *flags
}

func newOptions() *options {
return &options{flagSet: &flags{}}
return &options{runOpt: &runOpts{}, flagSet: &flags{}}
}

func (opts *options) mode(args []string) urleap.Mode {
Expand All @@ -81,6 +85,8 @@ func (opts *options) mode(args []string) urleap.Mode {
return urleap.List
case opts.flagSet.deleteFlag:
return urleap.Delete
case opts.runOpt.qrcode != "":
return urleap.QRCode
default:
return urleap.Shorten
}
Expand All @@ -93,10 +99,10 @@ func buildOptions(args []string) (*options, *flag.FlagSet) {
opts := newOptions()
flags := flag.NewFlagSet(args[0], flag.ContinueOnError)
flags.Usage = func() { fmt.Println(helpMessage(args)) }
flags.StringVarP(&opts.token, "token", "t", "", "specify the token for the service. This option is mandatory.")
flags.StringVarP(&opts.qrcode, "qrcode", "q", "", "include QR-code of the URL in the output.")
flags.StringVarP(&opts.config, "config", "c", "", "specify the configuration file.")
flags.StringVarP(&opts.group, "group", "g", "", "specify the group name for the service. Default is \"urleap\"")
flags.StringVarP(&opts.runOpt.token, "token", "t", "", "specify the token for the service. This option is mandatory.")
flags.StringVarP(&opts.runOpt.qrcode, "qrcode", "q", "", "include QR-code of the URL in the output.")
flags.StringVarP(&opts.runOpt.config, "config", "c", "", "specify the configuration file.")
flags.StringVarP(&opts.runOpt.group, "group", "g", "", "specify the group name for the service. Default is \"urleap\"")
flags.BoolVarP(&opts.flagSet.listGroupFlag, "list-group", "L", false, "list the groups. This is hidden option.")
flags.BoolVarP(&opts.flagSet.deleteFlag, "delete", "d", false, "delete the specified shorten URL.")
flags.BoolVarP(&opts.flagSet.helpFlag, "help", "h", false, "print this mesasge and exit.")
Expand All @@ -118,13 +124,13 @@ func parseOptions(args []string) (*options, []string, *UrleapError) {
fmt.Println(versionString(args))
return nil, nil, &UrleapError{statusCode: 0, message: ""}
}
if opts.token == "" {
if opts.runOpt.token == "" {
return nil, nil, &UrleapError{statusCode: 3, message: "no token was given"}
}
return opts, flags.Args(), nil
}

func shortenEach(bitly *urleap.Bitly, opts *options, config *urleap.Config, url string) error {
func shortenEach(bitly *urleap.Bitly, config *urleap.Config, url string) error {
result, err := bitly.Shorten(config, url)
if err != nil {
return err
Expand Down Expand Up @@ -160,9 +166,9 @@ func listGroups(bitly *urleap.Bitly, config *urleap.Config) error {
}

func perform(opts *options, args []string) *UrleapError {
bitly := urleap.NewBitly(opts.group)
config := urleap.NewConfig(opts.config, opts.mode(args))
config.Token = opts.token
bitly := urleap.NewBitly(opts.runOpt.group)
config := urleap.NewConfig(opts.runOpt.config, opts.mode(args))
config.Token = opts.runOpt.token
switch config.RunMode {
case urleap.List:
err := listUrls(bitly, config)
Expand All @@ -179,7 +185,7 @@ func perform(opts *options, args []string) *UrleapError {
}
case urleap.Shorten:
for _, url := range args {
err := shortenEach(bitly, opts, config, url)
err := shortenEach(bitly, config, url)
if err != nil {
return makeError(err, 4)
}
Expand Down

0 comments on commit 77acba6

Please sign in to comment.