diff --git a/Makefile b/Makefile index 18361d4..72203ca 100644 --- a/Makefile +++ b/Makefile @@ -12,11 +12,11 @@ define log endef .PHONY: all -all: lint fmt vet test test-coverage +all: fmt vet test test-coverage .PHONY: usage usage: - @echo "make [all|fmt|vet|lint|test|test-coverage]" + @echo "make [all|fmt|vet|test|test-coverage]" .PHONY: fmt fmt: @@ -28,18 +28,13 @@ vet: @$(call log,"Running vet") @go vet -all $(LIST_PKG) -.PHONY: lint -lint: - @$(call log,"Running linter") - @golint -set_exit_status $(LIST_PKG) - .PHONY: test -test: fmt vet lint +test: fmt vet @$(call log,"Running tests") @go test -v -race -short -timeout $(TEST_TIMEOUT)s $(ARGS) $(LIST_PKG) .PHONY: test-coverage -test-coverage: fmt vet lint +test-coverage: fmt vet @$(call log,"Running tests with coverage") @go test -v -race -short -timeout $(TEST_TIMEOUT)s $(ARGS) -coverprofile=coverage.out $(LIST_PKG) @go tool cover -func=coverage.out diff --git a/examples/client-on-request-completed/main.go b/examples/client-on-request-completed/main.go index 5f6a231..e91f016 100644 --- a/examples/client-on-request-completed/main.go +++ b/examples/client-on-request-completed/main.go @@ -3,8 +3,8 @@ package main import ( "context" "fmt" + "github.com/enetx/http" "log" - "net/http" "github.com/vartanbeno/go-reddit/v2/reddit" ) diff --git a/go.mod b/go.mod index abf47d4..66672e5 100644 --- a/go.mod +++ b/go.mod @@ -1,10 +1,34 @@ module github.com/vartanbeno/go-reddit/v2 -go 1.15 +go 1.24.0 require ( + github.com/enetx/surf v1.0.173 github.com/google/go-querystring v1.0.0 - github.com/stretchr/testify v1.5.1 - golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e + github.com/stretchr/testify v1.11.1 + golang.org/x/net v0.49.0 golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d ) + +require ( + github.com/andybalholm/brotli v1.2.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/enetx/g v1.0.202 // indirect + github.com/enetx/http v1.0.24 // indirect + github.com/enetx/http2 v1.0.24 // indirect + github.com/enetx/http3 v1.0.6 // indirect + github.com/enetx/iter v0.0.0-20250912135656-f1583323588f // indirect + github.com/enetx/utls v0.0.0-20260115181616-c525a7d559c8 // indirect + github.com/golang/protobuf v1.2.0 // indirect + github.com/klauspost/compress v1.18.3 // indirect + github.com/kr/text v0.2.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/quic-go/qpack v0.6.0 // indirect + github.com/quic-go/quic-go v0.59.0 // indirect + github.com/wzshiming/socks5 v0.7.0 // indirect + golang.org/x/crypto v0.47.0 // indirect + golang.org/x/sys v0.40.0 // indirect + golang.org/x/text v0.33.0 // indirect + google.golang.org/appengine v1.4.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum index e52bd76..df78e0c 100644 --- a/go.sum +++ b/go.sum @@ -1,26 +1,69 @@ cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= -github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/andybalholm/brotli v1.2.0 h1:ukwgCxwYrmACq68yiUqwIWnGY0cTPox/M94sVwToPjQ= +github.com/andybalholm/brotli v1.2.0/go.mod h1:rzTDkvFWvIrjDXZHkuS16NPggd91W3kUSvPlQ1pLaKY= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/enetx/g v1.0.202 h1:E32/PWp542wHh7c5VYPGohK0Nu6CpiRm34vr0P1F6dU= +github.com/enetx/g v1.0.202/go.mod h1:6/HQeRy+tIJVGY+oRPQVJ/vOSruAi0aldFggurT6jBY= +github.com/enetx/http v1.0.24 h1:sMeJ0rN00HlUYLIhUTHx36KumAlkaZu7vsQe5+GMTNM= +github.com/enetx/http v1.0.24/go.mod h1:1f4mytfF/SfjATEJnynpwGS6aa1ALjb8DtmYgFVblY0= +github.com/enetx/http2 v1.0.24 h1:MpNGZxD8wz2zg2bR4RSyB/4YJukZ91wIsQE2GLwQjCI= +github.com/enetx/http2 v1.0.24/go.mod h1:t54ex5HIS8V1+2j6cvEOv6umlrHsbUPFKQ54nYB58Nk= +github.com/enetx/http3 v1.0.6 h1:zOmCIOHqImGz0U3QTjvxyWIcPq7d4iTQ3sKZYejax8g= +github.com/enetx/http3 v1.0.6/go.mod h1:sqpVGZ9F1/wCiW6sjBUS2errKAh3SUYn6VlWE7LL6KM= +github.com/enetx/iter v0.0.0-20250912135656-f1583323588f h1:GUW+4AWfECIEJ9oAxgEAVGCpaozMCjRiUYnuR6Q0bCQ= +github.com/enetx/iter v0.0.0-20250912135656-f1583323588f/go.mod h1:oMZN8hGLUpi7QBlMEUqailocNy0NFAO/7Lu+Nwh9HMM= +github.com/enetx/surf v1.0.173 h1:36U3d5bGzncQJE8iqNAahIt6SlUYBYPJ90uigf7hqCw= +github.com/enetx/surf v1.0.173/go.mod h1:7kQlsgHH2OT69AB/9w9/9Nppx3wijRFOAPnH4IhwMBM= +github.com/enetx/utls v0.0.0-20260115181616-c525a7d559c8 h1:jN2LdG4CG7cXOMAkQVDencj67Gdt7FFwDP1UaPcYyV8= +github.com/enetx/utls v0.0.0-20260115181616-c525a7d559c8/go.mod h1:jsHaW4RX6DteSbAHT/pW7iJxv7YXLf2NTr3k9PsmXoc= github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk= github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= +github.com/klauspost/compress v1.18.3 h1:9PJRvfbmTabkOX8moIpXPbMMbYN60bWImDDU7L+/6zw= +github.com/klauspost/compress v1.18.3/go.mod h1:R0h/fSBs8DE4ENlcrlib3PsXS61voFxhIs2DeRhCvJ4= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= -github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +github.com/quic-go/qpack v0.6.0 h1:g7W+BMYynC1LbYLSqRt8PBg5Tgwxn214ZZR34VIOjz8= +github.com/quic-go/qpack v0.6.0/go.mod h1:lUpLKChi8njB4ty2bFLX2x4gzDqXwUpaO1DP9qMDZII= +github.com/quic-go/quic-go v0.59.0 h1:OLJkp1Mlm/aS7dpKgTc6cnpynnD2Xg7C1pwL6vy/SAw= +github.com/quic-go/quic-go v0.59.0/go.mod h1:upnsH4Ju1YkqpLXC305eW3yDZ4NfnNbmQRCMWS58IKU= +github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= +github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= +github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= +github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= +github.com/wzshiming/socks5 v0.7.0 h1:euJ+U48WrvVngi+opC8vAnpZ5sK12y1C2hPvb1f48Rg= +github.com/wzshiming/socks5 v0.7.0/go.mod h1:BvCAqlzocQN5xwLjBZDBbvWlrx8sCYSSbHEOf2wZgT0= +github.com/xyproto/randomstring v1.0.5 h1:YtlWPoRdgMu3NZtP45drfy1GKoojuR7hmRcnhZqKjWU= +github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E= +go.uber.org/mock v0.5.2 h1:LbtPTcP8A5k9WPXj54PPPbjcI4Y6lhyOZXn+VS7wNko= +go.uber.org/mock v0.5.2/go.mod h1:wLlUxC2vVTPTaE3UD51E0BGOAElKrILxhVSDYQLld5o= +golang.org/x/crypto v0.47.0 h1:V6e3FRj+n4dbpw86FJ8Fv7XVOql7TEwpHapKoMJ/GO8= +golang.org/x/crypto v0.47.0/go.mod h1:ff3Y9VzzKbwSSEzWqJsJVBnWmRwRSHt/6Op5n9bQc4A= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e h1:bRhVy7zSSasaqNksaRZiA5EEI+Ei4I1nO5Jh72wfHlg= golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.49.0 h1:eeHFmOGUTtaaPSGNmjBKpbng9MulQsJURQUAfUwY++o= +golang.org/x/net v0.49.0/go.mod h1:/ysNB2EvaqvesRkuLAyjI1ycPZlQHM3q01F02UY/MV8= golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d h1:TzXSXBo42m9gQenoE3b9BGiEpg5IG2JkU5FkPIawgtw= golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= -golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 h1:YUO/7uOKsKeq9UokNS62b8FYywz3ker1l1vDZRCRefw= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.19.0 h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4= +golang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI= +golang.org/x/sys v0.40.0 h1:DBZZqJ2Rkml6QMQsZywtnjnnGvHza6BTfYFWY9kjEWQ= +golang.org/x/sys v0.40.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.33.0 h1:B3njUFyqtHDUI5jMn1YIr5B0IE2U0qck04r6d4KPAxE= +golang.org/x/text v0.33.0/go.mod h1:LuMebE6+rBincTi9+xWTY8TztLzKHc/9C1uBCG27+q8= google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= -gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/reddit/account.go b/reddit/account.go index a4ed04d..f2b641d 100644 --- a/reddit/account.go +++ b/reddit/account.go @@ -2,7 +2,7 @@ package reddit import ( "context" - "net/http" + "github.com/enetx/http" "net/url" ) diff --git a/reddit/account_test.go b/reddit/account_test.go index a555210..b522af4 100644 --- a/reddit/account_test.go +++ b/reddit/account_test.go @@ -3,7 +3,7 @@ package reddit import ( "encoding/json" "fmt" - "net/http" + "github.com/enetx/http" "net/url" "testing" "time" diff --git a/reddit/collection.go b/reddit/collection.go index d05087e..7244fbd 100644 --- a/reddit/collection.go +++ b/reddit/collection.go @@ -3,7 +3,7 @@ package reddit import ( "context" "errors" - "net/http" + "github.com/enetx/http" "net/url" "strings" diff --git a/reddit/collection_test.go b/reddit/collection_test.go index f75acc6..ce8966f 100644 --- a/reddit/collection_test.go +++ b/reddit/collection_test.go @@ -2,7 +2,7 @@ package reddit import ( "fmt" - "net/http" + "github.com/enetx/http" "net/url" "testing" "time" diff --git a/reddit/comment.go b/reddit/comment.go index ca6389d..d47215a 100644 --- a/reddit/comment.go +++ b/reddit/comment.go @@ -3,7 +3,7 @@ package reddit import ( "context" "errors" - "net/http" + "github.com/enetx/http" "net/url" "strings" ) diff --git a/reddit/comment_test.go b/reddit/comment_test.go index e902550..286150f 100644 --- a/reddit/comment_test.go +++ b/reddit/comment_test.go @@ -2,7 +2,7 @@ package reddit import ( "fmt" - "net/http" + "github.com/enetx/http" "net/url" "testing" "time" diff --git a/reddit/emoji.go b/reddit/emoji.go index 7c72690..f2f511f 100644 --- a/reddit/emoji.go +++ b/reddit/emoji.go @@ -6,16 +6,15 @@ import ( "encoding/json" "errors" "fmt" + "github.com/enetx/http" "io" "mime/multipart" - "net/http" "net/url" "os" "strconv" "strings" "github.com/google/go-querystring/query" - "golang.org/x/net/context/ctxhttp" ) // EmojiService handles communication with the emoji @@ -242,14 +241,18 @@ func (s *EmojiService) Upload(ctx context.Context, subreddit string, createReque return nil, err } - httpResponse, err := ctxhttp.Post(ctx, nil, uploadURL, writer.FormDataContentType(), body) + req, err := http.NewRequest(http.MethodPost, uploadURL, body) + if err != nil { + return nil, err + } + resp, err = s.client.Do(ctx, req, nil) if err != nil { return nil, err } - err = CheckResponse(httpResponse) + err = CheckResponse(resp.Response) if err != nil { - return newResponse(httpResponse), err + return newResponse(resp.Response), err } return s.upload(ctx, subreddit, createRequest, fields["key"]) diff --git a/reddit/emoji_test.go b/reddit/emoji_test.go index 1edb9fe..3dc815a 100644 --- a/reddit/emoji_test.go +++ b/reddit/emoji_test.go @@ -3,9 +3,9 @@ package reddit import ( "bytes" "fmt" + "github.com/enetx/http" "io" "io/ioutil" - "net/http" "net/url" "os" "testing" diff --git a/reddit/errors.go b/reddit/errors.go index 3b20418..bcd67cc 100644 --- a/reddit/errors.go +++ b/reddit/errors.go @@ -3,7 +3,7 @@ package reddit import ( "encoding/json" "fmt" - "net/http" + "github.com/enetx/http" "strings" "time" ) diff --git a/reddit/flair.go b/reddit/flair.go index 0ecc8e8..f484315 100644 --- a/reddit/flair.go +++ b/reddit/flair.go @@ -6,7 +6,7 @@ import ( "encoding/csv" "errors" "fmt" - "net/http" + "github.com/enetx/http" "net/url" "github.com/google/go-querystring/query" diff --git a/reddit/flair_test.go b/reddit/flair_test.go index dd92533..d83c780 100644 --- a/reddit/flair_test.go +++ b/reddit/flair_test.go @@ -3,7 +3,7 @@ package reddit import ( "encoding/json" "fmt" - "net/http" + "github.com/enetx/http" "net/url" "testing" diff --git a/reddit/gold.go b/reddit/gold.go index 6b63b44..868257f 100644 --- a/reddit/gold.go +++ b/reddit/gold.go @@ -4,7 +4,7 @@ import ( "context" "errors" "fmt" - "net/http" + "github.com/enetx/http" "net/url" "strconv" ) diff --git a/reddit/gold_test.go b/reddit/gold_test.go index 11de810..e266153 100644 --- a/reddit/gold_test.go +++ b/reddit/gold_test.go @@ -1,7 +1,7 @@ package reddit import ( - "net/http" + "github.com/enetx/http" "net/url" "testing" diff --git a/reddit/listings_test.go b/reddit/listings_test.go index 3cac649..c724da2 100644 --- a/reddit/listings_test.go +++ b/reddit/listings_test.go @@ -2,7 +2,7 @@ package reddit import ( "fmt" - "net/http" + "github.com/enetx/http" "net/url" "testing" "time" diff --git a/reddit/live-thread.go b/reddit/live-thread.go index 5dac208..0fbcfed 100644 --- a/reddit/live-thread.go +++ b/reddit/live-thread.go @@ -5,8 +5,8 @@ import ( "encoding/json" "errors" "fmt" + "github.com/enetx/http" "io" - "net/http" "net/url" "reflect" "strings" diff --git a/reddit/live-thread_test.go b/reddit/live-thread_test.go index dc52715..9b50b32 100644 --- a/reddit/live-thread_test.go +++ b/reddit/live-thread_test.go @@ -2,7 +2,7 @@ package reddit import ( "fmt" - "net/http" + "github.com/enetx/http" "net/url" "testing" "time" diff --git a/reddit/message.go b/reddit/message.go index c4fc86c..0442fa3 100644 --- a/reddit/message.go +++ b/reddit/message.go @@ -4,7 +4,7 @@ import ( "context" "encoding/json" "errors" - "net/http" + "github.com/enetx/http" "net/url" "strings" diff --git a/reddit/message_test.go b/reddit/message_test.go index b624653..d457d5a 100644 --- a/reddit/message_test.go +++ b/reddit/message_test.go @@ -2,7 +2,7 @@ package reddit import ( "fmt" - "net/http" + "github.com/enetx/http" "net/url" "testing" "time" diff --git a/reddit/moderation.go b/reddit/moderation.go index a1af7d2..a93cabd 100644 --- a/reddit/moderation.go +++ b/reddit/moderation.go @@ -3,7 +3,7 @@ package reddit import ( "context" "fmt" - "net/http" + "github.com/enetx/http" "net/url" "reflect" diff --git a/reddit/moderation_test.go b/reddit/moderation_test.go index 0966be8..1aebd98 100644 --- a/reddit/moderation_test.go +++ b/reddit/moderation_test.go @@ -2,7 +2,7 @@ package reddit import ( "fmt" - "net/http" + "github.com/enetx/http" "net/url" "testing" "time" diff --git a/reddit/multi.go b/reddit/multi.go index 6e7c3a8..a30d0ab 100644 --- a/reddit/multi.go +++ b/reddit/multi.go @@ -5,7 +5,7 @@ import ( "encoding/json" "errors" "fmt" - "net/http" + "github.com/enetx/http" "net/url" "github.com/google/go-querystring/query" diff --git a/reddit/multi_test.go b/reddit/multi_test.go index 72423b6..b3973f1 100644 --- a/reddit/multi_test.go +++ b/reddit/multi_test.go @@ -3,7 +3,7 @@ package reddit import ( "encoding/json" "fmt" - "net/http" + "github.com/enetx/http" "net/url" "testing" "time" diff --git a/reddit/post-and-comment.go b/reddit/post-and-comment.go index 209604b..7ae1dac 100644 --- a/reddit/post-and-comment.go +++ b/reddit/post-and-comment.go @@ -2,7 +2,7 @@ package reddit import ( "context" - "net/http" + "github.com/enetx/http" "net/url" "strconv" ) diff --git a/reddit/post.go b/reddit/post.go index ef55224..c0754e5 100644 --- a/reddit/post.go +++ b/reddit/post.go @@ -4,7 +4,7 @@ import ( "context" "errors" "fmt" - "net/http" + "github.com/enetx/http" "net/url" "strings" diff --git a/reddit/post_test.go b/reddit/post_test.go index 78d26b3..fdf0adb 100644 --- a/reddit/post_test.go +++ b/reddit/post_test.go @@ -2,7 +2,7 @@ package reddit import ( "fmt" - "net/http" + "github.com/enetx/http" "net/url" "testing" "time" diff --git a/reddit/reddit-oauth.go b/reddit/reddit-oauth.go index 13030d6..0339abc 100644 --- a/reddit/reddit-oauth.go +++ b/reddit/reddit-oauth.go @@ -33,9 +33,10 @@ to https://www.reddit.com/api/v1/access_token with the following form values: package reddit +/* import ( "context" - "net/http" + "github.com/enetx/http" "golang.org/x/oauth2" ) @@ -75,3 +76,4 @@ func oauthTransport(client *Client) http.RoundTripper { Base: client.client.Transport, } } +*/ diff --git a/reddit/reddit-options.go b/reddit/reddit-options.go index 25d397b..bf8d833 100644 --- a/reddit/reddit-options.go +++ b/reddit/reddit-options.go @@ -1,8 +1,6 @@ package reddit import ( - "errors" - "net/http" "net/url" "os" ) @@ -10,17 +8,6 @@ import ( // Opt is used to further configure a client upon initialization. type Opt func(*Client) error -// WithHTTPClient sets the HTTP client which will be used to make requests. -func WithHTTPClient(httpClient *http.Client) Opt { - return func(c *Client) error { - if httpClient == nil { - return errors.New("*http.Client: cannot be nil") - } - c.client = httpClient - return nil - } -} - // WithUserAgent sets the User-Agent header for requests made with the client. // Reddit recommends the following format for the user agent: // :: (by /u/) diff --git a/reddit/reddit-options_test.go b/reddit/reddit-options_test.go index 3db5ce8..c5ec0b5 100644 --- a/reddit/reddit-options_test.go +++ b/reddit/reddit-options_test.go @@ -2,7 +2,6 @@ package reddit import ( "fmt" - "net/http" "net/url" "os" "testing" @@ -10,14 +9,6 @@ import ( "github.com/stretchr/testify/require" ) -func TestWithHTTPClient(t *testing.T) { - _, err := NewClient(Credentials{}, WithHTTPClient(nil)) - require.EqualError(t, err, "*http.Client: cannot be nil") - - _, err = NewClient(Credentials{}, WithHTTPClient(&http.Client{})) - require.NoError(t, err) -} - func TestWithUserAgent(t *testing.T) { c, err := NewClient(Credentials{}, WithUserAgent("test")) require.NoError(t, err) diff --git a/reddit/reddit-user-agent.go b/reddit/reddit-user-agent.go index 0e69b67..f56fccd 100644 --- a/reddit/reddit-user-agent.go +++ b/reddit/reddit-user-agent.go @@ -1,6 +1,6 @@ package reddit -import "net/http" +import "github.com/enetx/http" // cloneRequest returns a clone of the provided *http.Request. // The clone is a shallow copy of the struct and its Header map, diff --git a/reddit/reddit.go b/reddit/reddit.go index bafda0c..be41853 100644 --- a/reddit/reddit.go +++ b/reddit/reddit.go @@ -7,7 +7,6 @@ import ( "fmt" "io" "io/ioutil" - "net/http" "net/url" "reflect" "strconv" @@ -15,6 +14,9 @@ import ( "sync" "time" + "github.com/enetx/g" + "github.com/enetx/http" + "github.com/enetx/surf" "github.com/google/go-querystring/query" "golang.org/x/oauth2" ) @@ -60,7 +62,7 @@ type Credentials struct { // Client manages communication with the Reddit API. type Client struct { // HTTP client used to communicate with the Reddit API. - client *http.Client + client *surf.Client BaseURL *url.URL TokenURL *url.URL @@ -110,7 +112,15 @@ func newClient() *Client { baseURL, _ := url.Parse(defaultBaseURL) tokenURL, _ := url.Parse(defaultTokenURL) - client := &Client{client: &http.Client{}, BaseURL: baseURL, TokenURL: tokenURL} + surf_client := surf.NewClient(). + Builder(). + Impersonate(). + RandomOS(). + Firefox(). + Build(). + Unwrap() + + client := &Client{client: surf_client, BaseURL: baseURL, TokenURL: tokenURL} client.Account = &AccountService{client: client} client.Collection = &CollectionService{client: client} @@ -153,18 +163,20 @@ func NewClient(credentials Credentials, opts ...Opt) (*Client, error) { } } - userAgentTransport := &userAgentTransport{ - userAgent: client.UserAgent(), - Base: client.client.Transport, - } - client.client.Transport = userAgentTransport + /* + userAgentTransport := &userAgentTransport{ + userAgent: client.UserAgent(), + Base: client.client.Transport, + } + client.client.Transport = userAgentTransport - if client.client.CheckRedirect == nil { - client.client.CheckRedirect = client.redirect - } + if client.client.CheckRedirect == nil { + client.client.CheckRedirect = client.redirect + } - oauthTransport := oauthTransport(client) - client.client.Transport = oauthTransport + oauthTransport := oauthTransport(client) + client.client.Transport = oauthTransport + */ return client, nil } @@ -182,15 +194,17 @@ func NewReadonlyClient(opts ...Opt) (*Client, error) { } } - if client.client == nil { - client.client = &http.Client{} - } + /* + if client.client == nil { + client.client = &http.Client{} + } - userAgentTransport := &userAgentTransport{ - userAgent: client.UserAgent(), - Base: client.client.Transport, - } - client.client.Transport = userAgentTransport + userAgentTransport := &userAgentTransport{ + userAgent: client.UserAgent(), + Base: client.client.Transport, + } + client.client.Transport = userAgentTransport + */ return client, nil } @@ -334,6 +348,33 @@ func parseRate(r *http.Response) Rate { return rate } +func (c *Client) request(ctx context.Context, req *http.Request) (*http.Response, error) { + switch req.Method { + case http.MethodGet: + resp, err := c.client. + Get(g.String(req.URL.String())). + WithContext(ctx). + Do(). + Result() + if err != nil { + return nil, err + } + return resp.GetResponse(), nil + case http.MethodPost: + resp, err := c.client. + Post(g.String(req.URL.String()), nil). + WithContext(ctx). + Do(). + Result() + if err != nil { + return nil, err + } + return resp.GetResponse(), nil + default: + return nil, fmt.Errorf("fork does not support %s http method right now", req.Method) + } +} + // Do sends an API request and returns the API response. The API response is JSON decoded and stored in the value // pointed to by v, or returned as an error if an API error has occurred. If v implements the io.Writer interface, // the raw response will be written to v, without attempting to decode it. @@ -345,7 +386,7 @@ func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*Res }, err } - resp, err := DoRequestWithClient(ctx, c.client, req) + resp, err := c.request(ctx, req) if err != nil { return nil, err } diff --git a/reddit/reddit_test.go b/reddit/reddit_test.go index f119004..65205ed 100644 --- a/reddit/reddit_test.go +++ b/reddit/reddit_test.go @@ -4,9 +4,9 @@ import ( "context" "errors" "fmt" + "github.com/enetx/http" + "github.com/enetx/http/httptest" "io/ioutil" - "net/http" - "net/http/httptest" "os" "reflect" "testing" diff --git a/reddit/stream.go b/reddit/stream.go index 14f5b85..09c4d32 100644 --- a/reddit/stream.go +++ b/reddit/stream.go @@ -16,6 +16,7 @@ type StreamService struct { // - a channel into which new posts will be sent // - a channel into which any errors will be sent // - a function that the client can call once to stop the streaming and close the channels +// // Because of the 100 post limit imposed by Reddit when fetching posts, some high-traffic // streams might drop submissions between API requests, such as when streaming r/all. func (s *StreamService) Posts(subreddit string, opts ...StreamOpt) (<-chan *Post, <-chan error, func()) { diff --git a/reddit/stream_test.go b/reddit/stream_test.go index 9ae48c6..50d731d 100644 --- a/reddit/stream_test.go +++ b/reddit/stream_test.go @@ -2,7 +2,7 @@ package reddit import ( "fmt" - "net/http" + "github.com/enetx/http" "testing" "time" diff --git a/reddit/subreddit.go b/reddit/subreddit.go index dc94838..8ac212d 100644 --- a/reddit/subreddit.go +++ b/reddit/subreddit.go @@ -6,9 +6,9 @@ import ( "encoding/json" "errors" "fmt" + "github.com/enetx/http" "io" "mime/multipart" - "net/http" "net/url" "os" "path/filepath" diff --git a/reddit/subreddit_test.go b/reddit/subreddit_test.go index 9bf9049..978567a 100644 --- a/reddit/subreddit_test.go +++ b/reddit/subreddit_test.go @@ -3,9 +3,9 @@ package reddit import ( "bytes" "fmt" + "github.com/enetx/http" "io" "io/ioutil" - "net/http" "net/url" "os" "strings" diff --git a/reddit/user.go b/reddit/user.go index f1839f5..77d8d3c 100644 --- a/reddit/user.go +++ b/reddit/user.go @@ -3,7 +3,7 @@ package reddit import ( "context" "fmt" - "net/http" + "github.com/enetx/http" "net/url" ) diff --git a/reddit/user_test.go b/reddit/user_test.go index 81a2fd0..0607d5d 100644 --- a/reddit/user_test.go +++ b/reddit/user_test.go @@ -3,7 +3,7 @@ package reddit import ( "encoding/json" "fmt" - "net/http" + "github.com/enetx/http" "net/url" "testing" "time" diff --git a/reddit/widget.go b/reddit/widget.go index 34436d6..dcc115a 100644 --- a/reddit/widget.go +++ b/reddit/widget.go @@ -5,7 +5,7 @@ import ( "encoding/json" "errors" "fmt" - "net/http" + "github.com/enetx/http" ) // WidgetService handles communication with the widget diff --git a/reddit/widget_test.go b/reddit/widget_test.go index 40692b6..7ee798e 100644 --- a/reddit/widget_test.go +++ b/reddit/widget_test.go @@ -3,7 +3,7 @@ package reddit import ( "encoding/json" "fmt" - "net/http" + "github.com/enetx/http" "net/url" "testing" diff --git a/reddit/wiki.go b/reddit/wiki.go index ef7acda..0390cac 100644 --- a/reddit/wiki.go +++ b/reddit/wiki.go @@ -5,7 +5,7 @@ import ( "encoding/json" "errors" "fmt" - "net/http" + "github.com/enetx/http" "net/url" "strings" diff --git a/reddit/wiki_test.go b/reddit/wiki_test.go index cc9b961..6eb0b3d 100644 --- a/reddit/wiki_test.go +++ b/reddit/wiki_test.go @@ -2,7 +2,7 @@ package reddit import ( "fmt" - "net/http" + "github.com/enetx/http" "net/url" "testing" "time"