Skip to content

Commit

Permalink
refactor client
Browse files Browse the repository at this point in the history
  • Loading branch information
Chikage0o0 committed Sep 6, 2022
1 parent 60d5700 commit 22997e1
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 47 deletions.
50 changes: 3 additions & 47 deletions http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,61 +7,17 @@ import (
"io"
"log"
"net/http"
"net/url"
"os"
"path"
"regexp"
"runtime/debug"
"strconv"
)

type ErrorModel interface {
String() string
}

type Client struct {
UserAgent string
HTTPClient *http.Client
}

// TODO: global lookup map host -> ratelimit hits left and sleep wait strategy

var Instance *Client = nil

func init() {
Instance = &Client{
UserAgent: "mrpack-install",
HTTPClient: &http.Client{},
}
info, ok := debug.ReadBuildInfo()
if ok && info.Main.Path != "" {
Instance.UserAgent = info.Main.Path + "/" + info.Main.Version
}
}

func (client *Client) SetProxy(CustomProxy string) error {
proxy := func(_ *http.Request) (*url.URL, error) {
return url.Parse(CustomProxy)
}

httpTransport := &http.Transport{
Proxy: proxy,
}

client.HTTPClient = &http.Client{
Transport: httpTransport,
}

httpUrl := "https://api.modrinth.com/"
response, err := client.HTTPClient.Get(httpUrl)
if err != nil {
return err
}
if response.StatusCode != http.StatusOK {
return err
}
return nil
}
var Instance = NewHTTPClient()

func (client *Client) GetJson(url string, respModel interface{}, errModel ErrorModel) error {
request, err := http.NewRequest(http.MethodGet, url, nil)
Expand All @@ -74,7 +30,7 @@ func (client *Client) GetJson(url string, respModel interface{}, errModel ErrorM

request.Close = true

response, err := client.HTTPClient.Do(request)
response, err := client.Do(request)
if err != nil {
return err
}
Expand Down Expand Up @@ -111,7 +67,7 @@ func (client *Client) DownloadFile(url string, downloadDir string, fileName stri
request.Header.Set("User-Agent", client.UserAgent)
request.Close = true

response, err := client.HTTPClient.Do(request)
response, err := client.Do(request)
if err != nil {
return "", err
}
Expand Down
115 changes: 115 additions & 0 deletions http/http_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package http

import (
"crypto/tls"
"net/http"
"net/http/cookiejar"
"net/url"
"runtime/debug"
"time"
)

type Client struct {
http.Client
insecureSkipVerify bool
UserAgent string
transport *http.Transport
}

// TODO: global lookup map host -> ratelimit hits left and sleep wait strategy

func NewHTTPClient() *Client {
client := &Client{
Client: http.Client{},
UserAgent: "mrpack-install",
}
client.Client.Jar, _ = cookiejar.New(nil)
info, ok := debug.ReadBuildInfo()
if ok && info.Main.Path != "" {
client.UserAgent = info.Main.Path + "/" + info.Main.Version
}
return client
}

func (client *Client) lazyInit() {
if client.transport == nil {
client.transport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: false,
},
TLSHandshakeTimeout: 20 * time.Second,
DisableKeepAlives: false,
DisableCompression: false, // gzip
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
ResponseHeaderTimeout: 25 * time.Second,
ExpectContinueTimeout: 10 * time.Second,
}
client.Client.Transport = client.transport
}
}

func (client *Client) SetUserAgent(ua string) {
client.UserAgent = ua
}

func (client *Client) SetCookiejar(jar http.CookieJar) {
client.Client.Jar = jar
}

func (client *Client) ResetCookiejar() {
client.Jar, _ = cookiejar.New(nil)
}

func (client *Client) SetProxy(CustomProxy string) error {
client.lazyInit()
proxy, err := url.Parse(CustomProxy)
if err != nil {
return err
}

client.transport.Proxy = http.ProxyURL(proxy)

// Test proxy
httpUrl := "https://api.modrinth.com/"
response, err := client.Get(httpUrl)
if err != nil {
return err
}
if response.StatusCode != http.StatusOK {
return err
}
return nil
}

func (client *Client) SetInsecureSkipVerify(b bool) {
client.lazyInit()
client.transport.TLSClientConfig = &tls.Config{
InsecureSkipVerify: b,
}
}

func (client *Client) SetKeepAlive(b bool) {
client.lazyInit()
client.transport.DisableKeepAlives = !b
}

func (client *Client) SetGzip(b bool) {
client.lazyInit()
client.transport.DisableCompression = !b
}

func (client *Client) SetResponseHeaderTimeout(t time.Duration) {
client.lazyInit()
client.transport.ResponseHeaderTimeout = t
}

func (client *Client) SetTLSHandshakeTimeout(t time.Duration) {
client.lazyInit()
client.transport.TLSHandshakeTimeout = t
}

func (client *Client) SetTimeout(t time.Duration) {
client.Client.Timeout = t
}

0 comments on commit 22997e1

Please sign in to comment.