Skip to content

Commit

Permalink
Linting.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcdee committed May 22, 2024
1 parent 229f7d8 commit b248324
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 45 deletions.
48 changes: 21 additions & 27 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@
# This file is not a configuration example,
# it contains the exhaustive configuration with explanations of the options.

issues:
# Which files to exclude: they will be analyzed, but issues from them won't be reported.
# There is no need to include all autogenerated files,
# we confidently recognize autogenerated files.
# If it's not, please let us know.
# "/" will be replaced by current OS file path separator to properly work on Windows.
# Default: []
exclude-files:
- ".*_ssz\\.go$"

# Options for analysis running.
run:
# The default concurrency value is the number of available CPU.
Expand Down Expand Up @@ -39,16 +49,6 @@ run:
# Default: true
# skip-dirs-use-default: false

# Which files to skip: they will be analyzed, but issues from them won't be reported.
# Default value is empty list,
# but there is no need to include all autogenerated files,
# we confidently recognize autogenerated files.
# If it's not please let us know.
# "/" will be replaced by current OS file path separator to properly work on Windows.
skip-files:
- ".*_ssz\\.go$"
- ".*_encoding\\.go$"

# If set we pass it to "go list -mod={option}". From "go help modules":
# If invoked with -mod=readonly, the go command is disallowed from the implicit
# automatic updating of go.mod described above. Instead, it fails when any changes
Expand All @@ -69,7 +69,7 @@ run:
# Define the Go version limit.
# Mainly related to generics support since go1.18.
# Default: use Go version from the go.mod file, fallback on the env var `GOVERSION`, fallback on 1.18
# go: '1.20'
# go: '1.19'


# output configuration options
Expand Down Expand Up @@ -109,6 +109,10 @@ linters-settings:
lll:
line-length: 132

nlreturn:
# Allow two-line blocks without requiring a newline
block-size: 3

stylecheck:
checks: [ "all", "-ST1000" ]

Expand All @@ -129,48 +133,38 @@ linters:
- contextcheck
- cyclop
- deadcode
- decorder
- depguard
- dupl
- dupword
- errchkjson
- errorlint
- err113
- execinquery
- exhaustive
- exhaustivestruct
- exhaustruct
- forbidigo
- forcetypeassert
- funlen
- gci
- gochecknoglobals
- gochecknoinits
- gocognit
- goconst
- goerr113
- gofumpt
- goheader
- golint
- gomnd
- ifshort
- interfacer
- ireturn
- inamedparam
- lll
- maintidx
- maligned
- mnd
- musttag
- nestif
- nilnil
- nlreturn
- nolintlint
- nosnakecase
- perfsprint
- promlinter
- rowserrcheck
- scopelint
- sqlclosecheck
- structcheck
- thelper
- varcheck
- varnamelen
- wastedassign
- whitespace
- wrapcheck
- wsl
7 changes: 7 additions & 0 deletions http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func (s *Service) get(ctx context.Context, endpoint string) (ContentType, io.Rea
return ContentTypeUnknown, nil, errors.Wrap(err, "failed to create GET request")
}

s.addExtraHeaders(req)
// Prefer SSZ if available.
req.Header.Set("Accept", "application/octet-stream;q=1,application/json;q=0.9")
span.AddEvent("Sending request")
Expand Down Expand Up @@ -120,3 +121,9 @@ func contentTypeFromResp(resp *http.Response) (ContentType, error) {
}
return ParseFromMediaType(respContentType[0])
}

func (s *Service) addExtraHeaders(req *http.Request) {
for k, v := range s.extraHeaders {
req.Header.Add(k, v)
}
}
23 changes: 16 additions & 7 deletions http/parameters.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2022 Attestant Limited.
// Copyright © 2022, 2023 Attestant Limited.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand All @@ -22,10 +22,11 @@ import (
)

type parameters struct {
logLevel zerolog.Level
monitor metrics.Service
address string
timeout time.Duration
logLevel zerolog.Level
monitor metrics.Service
address string
timeout time.Duration
extraHeaders map[string]string
}

// Parameter is the interface for service parameters.
Expand Down Expand Up @@ -67,11 +68,19 @@ func WithTimeout(timeout time.Duration) Parameter {
})
}

// WithExtraHeaders sets additional headers to be sent with each HTTP request.
func WithExtraHeaders(headers map[string]string) Parameter {
return parameterFunc(func(p *parameters) {
p.extraHeaders = headers
})
}

// parseAndCheckParameters parses and checks parameters to ensure that mandatory parameters are present and correct.
func parseAndCheckParameters(params ...Parameter) (*parameters, error) {
parameters := parameters{
logLevel: zerolog.GlobalLevel(),
timeout: 2 * time.Second,
logLevel: zerolog.GlobalLevel(),
timeout: 2 * time.Second,
extraHeaders: make(map[string]string),
}
for _, p := range params {
if params != nil {
Expand Down
24 changes: 13 additions & 11 deletions http/service.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2022 Attestant Limited.
// Copyright © 2022, 2023 Attestant Limited.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down Expand Up @@ -32,11 +32,12 @@ import (

// Service is an Ethereum 2 client service.
type Service struct {
base *url.URL
address string
client *http.Client
timeout time.Duration
pubkey *phase0.BLSPubKey
base *url.URL
address string
client *http.Client
timeout time.Duration
pubkey *phase0.BLSPubKey
extraHeaders map[string]string
}

// log is a service-wide logger.
Expand Down Expand Up @@ -100,11 +101,12 @@ func New(ctx context.Context, params ...Parameter) (builderclient.Service, error
base.User = nil
}
s := &Service{
base: base,
address: base.String(),
client: client,
timeout: parameters.timeout,
pubkey: pubkey,
base: base,
address: base.String(),
client: client,
timeout: parameters.timeout,
pubkey: pubkey,
extraHeaders: parameters.extraHeaders,
}

// Close the service on context done.
Expand Down

0 comments on commit b248324

Please sign in to comment.