From b16d7e65f7287bce00bb3aad706b8bcfec48ec29 Mon Sep 17 00:00:00 2001 From: "kayos@tcp.direct" Date: Mon, 13 Dec 2021 01:02:29 -0800 Subject: [PATCH] Refactor --- actions.go | 98 +++++--------------------------------------------- api.go | 28 ++++++++++++++- requests.go | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++ util.go | 33 ----------------- 4 files changed, 136 insertions(+), 123 deletions(-) create mode 100644 requests.go delete mode 100644 util.go diff --git a/actions.go b/actions.go index 3221ff5..d17e5cf 100644 --- a/actions.go +++ b/actions.go @@ -6,35 +6,10 @@ import ( "fmt" "net" "net/http" - "net/url" "strconv" - "strings" - "time" ) -const ( - APIBaseURL = "https://proxybonanza.com/api/v1/" - packages = "userpackages.json" - authips = "authips.json" -) - -// Package contains what we know about a particular proxybonanza package. -type Package struct { - ID int - AuthIPs []AuthIP - AllTimeStats PackageStatistics - HourlyStats map[time.Time]PackageStatistics -} - -// APIClient is a client for ProxyBonanza.com. -type APIClient struct { - Key string - KnownPackages map[int]PackageDetails - - c *http.Client -} - -// NewApiClient instantiates a proxybonanza.com API client with the given key. +// NewApiClient instantiates a proxybonanza.com API client with the given key using golang's default http client. func NewApiClient(key string) *APIClient { return &APIClient{ Key: key, @@ -43,71 +18,16 @@ func NewApiClient(key string) *APIClient { } } -func (api *APIClient) newRequest(method, u string) (r *http.Request) { - r, _ = http.NewRequest(method, u, nil) - r.Header.Add("accept", "application/json") - r.Header.Add("Authorization", api.Key) - - return +// NewCustomClient insantiates a proxybonanza API client with the given key and the given http.Client. +func NewCustomClient(key string, client *http.Client) *APIClient { + return &APIClient{ + Key: key, + KnownPackages: make(map[int]PackageDetails), + c: client, + } } -func (api *APIClient) getReq(endpoint string) ([]byte, error) { - res, err := api.c.Do(api.newRequest("GET", APIBaseURL+endpoint)) - body, err := processBody(res) - if err != nil { - return nil, err - } - return body, nil -} -func (api *APIClient) postReq(endpoint string, post map[string]string) ([]byte, error) { - params := url.Values{} - for k, v := range post { - params.Set(k, v) - } - enc := params.Encode() - req, err := http.NewRequest("POST", APIBaseURL+endpoint, strings.NewReader(enc)) - req.Header.Add("accept", "application/json") - req.Header.Add("Authorization", api.Key) - if err != nil { - return nil, err - } - - req.Header.Add("Content-Type", "application/x-www-form-urlencoded") - - res, err := api.c.Do(req) - if err != nil { - return nil, err - } - body, err := processBody(res) - if err != nil { - return nil, err - } - return body, nil -} - -func (api *APIClient) deleteReq(endpoint string) ([]byte, error) { - req, err := http.NewRequest("DELETE", APIBaseURL+endpoint, nil) - - if err != nil { - return nil, err - } - - req.Header.Add("accept", "application/json") - req.Header.Add("Authorization", api.Key) - - res, err := api.c.Do(req) - if err != nil { - return nil, err - } - - body, err := processBody(res) - if err != nil { - return nil, err - } - - return body, nil -} // GetProxyPackages gets current proxy packages from your account. func (api *APIClient) GetProxyPackages() ([]UserPackage, error) { @@ -235,7 +155,7 @@ func (api *APIClient) AddCurrentIPtoAllPackages() (success int) { return } - myip := GetMyIP() + myip := getMyIP() for _, p := range packs { _, err := api.AddAuthIP(myip, p.ID) if err == nil { diff --git a/api.go b/api.go index 98c3c52..ab0be8d 100644 --- a/api.go +++ b/api.go @@ -1,6 +1,32 @@ package proxygonanza -import "time" +import ( + "net/http" + "time" +) + +const ( + APIBaseURL = "https://proxybonanza.com/api/v1/" + packages = "userpackages.json" + authips = "authips.json" +) + +// Package contains what we know about a particular proxybonanza package. +type Package struct { + ID int + AuthIPs []AuthIP + AllTimeStats PackageStatistics + HourlyStats map[time.Time]PackageStatistics +} + +// APIClient is a client for ProxyBonanza.com. +type APIClient struct { + Key string + KnownPackages map[int]PackageDetails + + c *http.Client +} + // PackageResponse represents an API response from proxybonanza.com containing proxy package information. type PackageResponse struct { diff --git a/requests.go b/requests.go new file mode 100644 index 0000000..d699ba2 --- /dev/null +++ b/requests.go @@ -0,0 +1,100 @@ +package proxygonanza + +import ( + "fmt" + "io/ioutil" + "net" + "net/http" + "net/url" + "strings" +) + +func (api *APIClient) newRequest(method, u string) (r *http.Request) { + r, _ = http.NewRequest(method, u, nil) + r.Header.Add("accept", "application/json") + r.Header.Add("Authorization", api.Key) + + return +} + +func (api *APIClient) getReq(endpoint string) ([]byte, error) { + res, err := api.c.Do(api.newRequest("GET", APIBaseURL+endpoint)) + body, err := processBody(res) + if err != nil { + return nil, err + } + return body, nil +} + +func (api *APIClient) postReq(endpoint string, post map[string]string) ([]byte, error) { + params := url.Values{} + for k, v := range post { + params.Set(k, v) + } + enc := params.Encode() + req, err := http.NewRequest("POST", APIBaseURL+endpoint, strings.NewReader(enc)) + req.Header.Add("accept", "application/json") + req.Header.Add("Authorization", api.Key) + if err != nil { + return nil, err + } + + req.Header.Add("Content-Type", "application/x-www-form-urlencoded") + + res, err := api.c.Do(req) + if err != nil { + return nil, err + } + body, err := processBody(res) + if err != nil { + return nil, err + } + return body, nil +} + +func (api *APIClient) deleteReq(endpoint string) ([]byte, error) { + req, err := http.NewRequest("DELETE", APIBaseURL+endpoint, nil) + + if err != nil { + return nil, err + } + + req.Header.Add("accept", "application/json") + req.Header.Add("Authorization", api.Key) + + res, err := api.c.Do(req) + if err != nil { + return nil, err + } + + body, err := processBody(res) + if err != nil { + return nil, err + } + + return body, nil +} + +func processBody(res *http.Response) ([]byte, error) { + defer res.Body.Close() + + body, err := ioutil.ReadAll(res.Body) + if err != nil { + return nil, err + } + return body, nil +} + +func getMyIP() net.IP { + res, err := http.DefaultClient.Get("https://wtfismyip.com/text") + if err != nil { + fmt.Println(err) + return net.IP{} + } + body, err := ioutil.ReadAll(res.Body) + if err != nil { + fmt.Println(err) + return net.IP{} + } + return net.ParseIP(strings.TrimSpace(string(body))) +} diff --git a/util.go b/util.go deleted file mode 100644 index 3c141cb..0000000 --- a/util.go +++ /dev/null @@ -1,33 +0,0 @@ -package proxygonanza - -import ( - "fmt" - "io/ioutil" - "net" - "net/http" - "strings" -) - -func processBody(res *http.Response) ([]byte, error) { - defer res.Body.Close() - - body, err := ioutil.ReadAll(res.Body) - if err != nil { - return nil, err - } - return body, nil -} - -func GetMyIP() net.IP { - res, err := http.DefaultClient.Get("https://wtfismyip.com/text") - if err != nil { - fmt.Println(err) - return net.IP{} - } - body, err := ioutil.ReadAll(res.Body) - if err != nil { - fmt.Println(err) - return net.IP{} - } - return net.ParseIP(strings.TrimSpace(string(body))) -}