mirror of
https://github.com/crunchy-labs/crunchy-cli.git
synced 2026-01-21 12:12:00 -06:00
Add custom useragent for cli request
This commit is contained in:
parent
3617955bc5
commit
598e460e6c
3 changed files with 47 additions and 16 deletions
|
|
@ -2,6 +2,7 @@ package cmd
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/ByteDream/crunchyroll-go"
|
||||
"github.com/spf13/cobra"
|
||||
"net/http"
|
||||
|
|
@ -10,6 +11,8 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
var Version = "development"
|
||||
|
||||
var (
|
||||
client *http.Client
|
||||
crunchy *crunchyroll.Crunchyroll
|
||||
|
|
@ -17,7 +20,10 @@ var (
|
|||
|
||||
quietFlag bool
|
||||
verboseFlag bool
|
||||
proxyFlag string
|
||||
|
||||
proxyFlag string
|
||||
|
||||
useragentFlag string
|
||||
)
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
|
|
@ -36,7 +42,7 @@ var rootCmd = &cobra.Command{
|
|||
|
||||
out.DebugLog.Printf("Executing `%s` command with %d arg(s)\n", cmd.Name(), len(args))
|
||||
|
||||
client, err = createOrDefaultClient(proxyFlag)
|
||||
client, err = createOrDefaultClient(proxyFlag, useragentFlag)
|
||||
return
|
||||
},
|
||||
}
|
||||
|
|
@ -44,7 +50,10 @@ var rootCmd = &cobra.Command{
|
|||
func init() {
|
||||
rootCmd.PersistentFlags().BoolVarP(&quietFlag, "quiet", "q", false, "Disable all output")
|
||||
rootCmd.PersistentFlags().BoolVarP(&verboseFlag, "verbose", "v", false, "Adds debug messages to the normal output")
|
||||
|
||||
rootCmd.PersistentFlags().StringVarP(&proxyFlag, "proxy", "p", "", "Proxy to use")
|
||||
|
||||
rootCmd.PersistentFlags().StringVar(&useragentFlag, "useragent", fmt.Sprintf("crunchyroll-go/%s", Version), "Useragent to do all request with")
|
||||
}
|
||||
|
||||
func Execute() {
|
||||
|
|
|
|||
|
|
@ -71,7 +71,23 @@ func allLocalesAsStrings() (locales []string) {
|
|||
return
|
||||
}
|
||||
|
||||
func createOrDefaultClient(proxy string) (*http.Client, error) {
|
||||
type headerRoundTripper struct {
|
||||
http.RoundTripper
|
||||
header map[string]string
|
||||
}
|
||||
|
||||
func (rht headerRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
|
||||
resp, err := rht.RoundTripper.RoundTrip(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for k, v := range rht.header {
|
||||
resp.Header.Set(k, v)
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func createOrDefaultClient(proxy, useragent string) (*http.Client, error) {
|
||||
if proxy == "" {
|
||||
return http.DefaultClient, nil
|
||||
} else {
|
||||
|
|
@ -80,12 +96,21 @@ func createOrDefaultClient(proxy string) (*http.Client, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var rt http.RoundTripper = &http.Transport{
|
||||
DisableCompression: true,
|
||||
Proxy: http.ProxyURL(proxyURL),
|
||||
}
|
||||
if useragent != "" {
|
||||
rt = headerRoundTripper{
|
||||
RoundTripper: rt,
|
||||
header: map[string]string{"User-Agent": useragent},
|
||||
}
|
||||
}
|
||||
|
||||
client := &http.Client{
|
||||
Transport: &http.Transport{
|
||||
DisableCompression: true,
|
||||
Proxy: http.ProxyURL(proxyURL),
|
||||
},
|
||||
Timeout: 30 * time.Second,
|
||||
Transport: rt,
|
||||
Timeout: 30 * time.Second,
|
||||
}
|
||||
return client, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue