Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add discourse service #217

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
linting/style fixes
  • Loading branch information
piksel committed Aug 16, 2021
commit 41c09183925d701806d19c888c94912ddfb26860
14 changes: 7 additions & 7 deletions pkg/common/webclient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var _ = Describe("WebClient", func() {
It("should return an error", func() {
server.AppendHandlers(ghttp.RespondWith(http.StatusOK, "invalid json"))
res := &mockResponse{}
err := webclient.GetJson(server.URL(), &res)
err := webclient.GetJSON(server.URL(), &res)
Expect(server.ReceivedRequests()).Should(HaveLen(1))
Expect(err).To(MatchError("invalid character 'i' looking for beginning of value"))
Expect(res.Status).To(BeEmpty())
Expand All @@ -32,7 +32,7 @@ var _ = Describe("WebClient", func() {
It("should return an error", func() {
server.AppendHandlers(ghttp.RespondWith(http.StatusOK, nil))
res := &mockResponse{}
err := webclient.GetJson(server.URL(), &res)
err := webclient.GetJSON(server.URL(), &res)
Expect(server.ReceivedRequests()).Should(HaveLen(1))
Expect(err).To(MatchError("unexpected end of JSON input"))
Expect(res.Status).To(BeEmpty())
Expand All @@ -42,7 +42,7 @@ var _ = Describe("WebClient", func() {
It("should deserialize GET response", func() {
server.AppendHandlers(ghttp.RespondWithJSONEncoded(http.StatusOK, mockResponse{Status: "OK"}))
res := &mockResponse{}
err := webclient.GetJson(server.URL(), &res)
err := webclient.GetJSON(server.URL(), &res)
Expect(server.ReceivedRequests()).Should(HaveLen(1))
Expect(err).ToNot(HaveOccurred())
Expect(res.Status).To(Equal("OK"))
Expand Down Expand Up @@ -100,22 +100,22 @@ var _ = Describe("WebClient", func() {
ghttp.RespondWithJSONEncoded(http.StatusOK, &mockResponse{Status: "That's Numberwang!"})),
)

err := webclient.PostJson(server.URL(), &req, &res)
err := webclient.PostJSON(server.URL(), &req, &res)
Expect(server.ReceivedRequests()).Should(HaveLen(1))
Expect(err).ToNot(HaveOccurred())
Expect(res.Status).To(Equal("That's Numberwang!"))
})

It("should return error on error status responses", func() {
server.AppendHandlers(ghttp.RespondWith(404, "Not found!"))
err := webclient.PostJson(server.URL(), &mockRequest{}, &mockResponse{})
err := webclient.PostJSON(server.URL(), &mockRequest{}, &mockResponse{})
Expect(server.ReceivedRequests()).Should(HaveLen(1))
Expect(err).To(MatchError("got HTTP 404 Not Found"))
})

It("should return error on invalid request", func() {
server.AppendHandlers(ghttp.VerifyRequest("POST", "/"))
err := webclient.PostJson(server.URL(), func() {}, &mockResponse{})
err := webclient.PostJSON(server.URL(), func() {}, &mockResponse{})
Expect(server.ReceivedRequests()).Should(HaveLen(0))
Expect(err).To(MatchError("error creating payload: json: unsupported type: func()"))
})
Expand All @@ -127,7 +127,7 @@ var _ = Describe("WebClient", func() {
ghttp.RespondWithJSONEncoded(http.StatusOK, res)),
)

err := webclient.PostJson(server.URL(), nil, &[]bool{})
err := webclient.PostJSON(server.URL(), nil, &[]bool{})
Expect(server.ReceivedRequests()).Should(HaveLen(1))
Expect(err).To(MatchError("json: cannot unmarshal object into Go value of type []bool"))
Expect(webclient.ErrorBody(err)).To(MatchJSON(`{"Status":"cool skirt"}`))
Expand Down
22 changes: 11 additions & 11 deletions pkg/common/webclient/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@ import (
"net/http"
)

// JsonContentType is the default mime type for JSON
const JsonContentType = "application/json"
// JSONContentType is the default mime type for JSON
const JSONContentType = "application/json"

// DefaultJsonClient is the singleton instance of WebClient using http.DefaultClient
var DefaultJsonClient = NewJSONClient()
// DefaultJSONClient is the singleton instance of WebClient using http.DefaultClient
var DefaultJSONClient = NewJSONClient()

// GetJson fetches url using GET and unmarshals into the passed response using DefaultJsonClient
func GetJson(url string, response interface{}) error {
return DefaultJsonClient.Get(url, response)
// GetJSON fetches url using GET and unmarshals into the passed response using DefaultJSONClient
func GetJSON(url string, response interface{}) error {
return DefaultJSONClient.Get(url, response)
}

// PostJson sends request as JSON and unmarshals the response JSON into the supplied struct using DefaultJsonClient
func PostJson(url string, request interface{}, response interface{}) error {
return DefaultJsonClient.Post(url, request, response)
// PostJSON sends request as JSON and unmarshals the response JSON into the supplied struct using DefaultJSONClient
func PostJSON(url string, request interface{}, response interface{}) error {
return DefaultJSONClient.Post(url, request, response)
}

// NewJSONClient returns a WebClient using the default http.Client and JSON serialization
func NewJSONClient() WebClient {
var c client
c = client{
headers: http.Header{
"Content-Type": []string{JsonContentType},
"Content-Type": []string{JSONContentType},
},
parse: json.Unmarshal,
write: func(v interface{}) ([]byte, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/common/webclient/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@
},
},
headers: http.Header{
"Content-Type": []string{JsonContentType},
"Content-Type": []string{JSONContentType},
},
parse: json.Unmarshal,
write: func(v interface{}) ([]byte, error) {
return json.MarshalIndent(v, "", s.client.indent)
},

Check warning on line 56 in pkg/common/webclient/service.go

View check run for this annotation

Codecov / codecov/patch

pkg/common/webclient/service.go#L55-L56

Added lines #L55 - L56 were not covered by tests
}
}

Expand All @@ -63,7 +63,7 @@
if s.certPool == nil {
certPool, err := x509.SystemCertPool()
if err != nil {
certPool = x509.NewCertPool()

Check warning on line 66 in pkg/common/webclient/service.go

View check run for this annotation

Codecov / codecov/patch

pkg/common/webclient/service.go#L66

Added line #L66 was not covered by tests
}
s.certPool = certPool
if tp, ok := s.client.httpClient.Transport.(*http.Transport); ok {
Expand Down
2 changes: 1 addition & 1 deletion pkg/services/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ var _ = Describe("services", func() {
service, err := serviceRouter.Locate(configURL)
Expect(err).NotTo(HaveOccurred())

if httpService, isHttpService := service.(types.HTTPService); isHttpService {
if httpService, isHTTPService := service.(types.HTTPService); isHTTPService {
httpmock.ActivateNonDefault(httpService.HTTPClient())
}

Expand Down
3 changes: 1 addition & 2 deletions pkg/services/telegram/telegram_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
errResponse := &ErrorResponse{}
if c.WebClient.ErrorResponse(err, errResponse) {
return errResponse
} else {
return err
}
return err

Check warning on line 68 in pkg/services/telegram/telegram_client.go

View check run for this annotation

Codecov / codecov/patch

pkg/services/telegram/telegram_client.go#L68

Added line #L68 was not covered by tests
}
Loading