Skip to content

Commit

Permalink
Merge pull request #9 from oslokommune/http
Browse files Browse the repository at this point in the history
Added http communcication module
  • Loading branch information
bsek committed Nov 8, 2023
2 parents 896dcfd + 1dc8c10 commit e4b1eaa
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 0 deletions.
23 changes: 23 additions & 0 deletions httpcomm/decode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package httpComm

import (
"context"
"encoding/json"

"go.opentelemetry.io/otel/trace"
)

func Decode[T any](ctx context.Context, responseBody []byte, tracing bool) (*T, error) {
if tracing {
_, span := tracer.Start(ctx, "decode-json", trace.WithSpanKind(trace.SpanKindInternal), trace.WithAttributes(traceCommonLabels...))
defer span.End()
}

var message T
err := json.Unmarshal([]byte(responseBody), &message)
if err != nil {
return nil, err
}

return &message, nil
}
14 changes: 14 additions & 0 deletions httpcomm/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module github.com/oslokommune/common-lib-go/httpcomm

go 1.21.3

require (
go.opentelemetry.io/otel v1.19.0
go.opentelemetry.io/otel/trace v1.19.0
)

require (
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
go.opentelemetry.io/otel/metric v1.19.0 // indirect
)
21 changes: 21 additions & 0 deletions httpcomm/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ=
github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
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/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
go.opentelemetry.io/otel v1.19.0 h1:MuS/TNf4/j4IXsZuJegVzI1cwut7Qc00344rgH7p8bs=
go.opentelemetry.io/otel v1.19.0/go.mod h1:i0QyjOq3UPoTzff0PJB2N66fb4S0+rSbSB15/oyH9fY=
go.opentelemetry.io/otel/metric v1.19.0 h1:aTzpGtV0ar9wlV4Sna9sdJyII5jTVJEvKETPiOKwvpE=
go.opentelemetry.io/otel/metric v1.19.0/go.mod h1:L5rUsV9kM1IxCj1MmSdS+JQAcVm319EUrDVLrt7jqt8=
go.opentelemetry.io/otel/trace v1.19.0 h1:DFVQmlVbfVeOuBRrwdtaehRrWiL1JoVs9CPIQ1Dzxpg=
go.opentelemetry.io/otel/trace v1.19.0/go.mod h1:mfaSyvGyEJEI0nyV2I4qhNQnbBOUUmYZpYojqMnX2vo=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
75 changes: 75 additions & 0 deletions httpcomm/request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package httpComm

import (
"context"
"fmt"
"io"
"net/http"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)

var (
tracer = otel.Tracer("github.com/oslokommune/httpcomm")
traceCommonLabels = []attribute.KeyValue{
attribute.String("language", "go"),
}
)

func createRequest(ctx context.Context, httpRequest HTTPRequest) (*http.Request, error) {
req, err := http.NewRequestWithContext(ctx, httpRequest.Method, httpRequest.Url, httpRequest.Body)
if err != nil {
return nil, err
}

for key, value := range httpRequest.Headers {
req.Header.Set(key, value)
}

if httpRequest.Token != nil {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", *httpRequest.Token))
}

return req, nil
}

func Call(ctx context.Context, httpClient *http.Client, httpRequest HTTPRequest) (*HTTPResponse, error) {
var span trace.Span
if httpRequest.Tracing {
_, span = tracer.Start(ctx, httpRequest.Method, trace.WithSpanKind(trace.SpanKindInternal), trace.WithAttributes(traceCommonLabels...))
defer span.End()
}

req, err := createRequest(ctx, httpRequest)
if err != nil {
return nil, err
}

resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

statusCode := resp.StatusCode

if httpRequest.Tracing {
span.SetAttributes(attribute.Int("http.status", statusCode))
span.SetAttributes(attribute.String("http.method", "GET"))
span.SetAttributes(attribute.String("http.url", req.URL.String()))
}

body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}

httpResponse := HTTPResponse{
StatusCode: statusCode,
Message: string(body),
}

return &httpResponse, nil
}
19 changes: 19 additions & 0 deletions httpcomm/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package httpComm

import (
"io"
)

type HTTPRequest struct {
Body io.Reader
Token *string
Headers map[string]string
Url string
Method string
Tracing bool
}

type HTTPResponse struct {
Message string
StatusCode int
}

0 comments on commit e4b1eaa

Please sign in to comment.