Skip to content

Commit

Permalink
Fix formRequest and update linter (#83)
Browse files Browse the repository at this point in the history
* Fix formRequest and update linter

The original PR from @itsalltoast would fix on service layer, but in the
future we can face the same issue when we add new endpoints, so decided
to fix it in the "core" with checking if it's a pointer or not.

Now we have tests to cover formRequest endpoints. Both create file and
create file from URL have test.

As an addition, linters are up to date and all offenses are fixed.
original PR: https://github.com/yitsushi/go-misskey/pull/82/files

resolves #81

* update go version in PR checker

* update go.sum

* fix go mod?
  • Loading branch information
Balazs Nadasdi committed Aug 23, 2021
1 parent 79312a4 commit ad3e552
Show file tree
Hide file tree
Showing 46 changed files with 252 additions and 113 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/core-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ^1.15
go-version: ^1.16
id: go

- name: Checkout code
Expand Down Expand Up @@ -41,7 +41,7 @@ jobs:
- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ^1.15
go-version: ^1.16
id: go

- name: Checkout code
Expand All @@ -68,4 +68,4 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.31.0
version: v1.42.0
10 changes: 6 additions & 4 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ linters:
- gofumpt
- goheader
- goimports
- golint
# - golint # Replaced by revive.
- revive
- gomnd
- gomodguard
- goprintffuncname
- gosec
- interfacer
# - interfacer # The repository of the linter has been archived by the owner.
- lll
- maligned
# - maligned # Replaced by govet 'fieldalignment'.
- misspell
- nakedret
- nestif
Expand All @@ -63,7 +64,8 @@ linters:
- nolintlint
- prealloc
- rowserrcheck
- scopelint
# - scopelint # Replaced by exportloopref.
- exportloopref
- sqlclosecheck
- stylecheck
- testpackage
Expand Down
16 changes: 1 addition & 15 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,14 @@ import (

"github.com/sirupsen/logrus"
"github.com/yitsushi/go-misskey/core"
"github.com/yitsushi/go-misskey/services/antennas"
"github.com/yitsushi/go-misskey/services/meta"
"golang.org/x/net/context"
)

// HTTPClient is a simple intreface for http.Client.
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}

// ClientInterface is an interface to describe how a Client looks like.
// Mostly for Mocking. Or later if Misskey gets multiple API versions.
type ClientInterface interface {
Meta() *meta.Service
Antennas() *antennas.Service
}

// Client is the main Misskey client struct.
type Client struct {
BaseURL string
Token string
HTTPClient HTTPClient
HTTPClient core.HTTPClient

logger *logrus.Logger
}
Expand Down
8 changes: 8 additions & 0 deletions core/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package core

import "net/http"

// HTTPClient is a simple intreface for http.Client.
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}
8 changes: 7 additions & 1 deletion core/multipart.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"strings"
)

const numberOfPartsKeyValue = 2

type multipartField struct {
Type string
Name string
Expand All @@ -16,6 +18,10 @@ func parseMultipartFields(r BaseRequest) map[string]multipartField {
fields := map[string]multipartField{}

v := reflect.ValueOf(r)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}

for i := 0; i < v.NumField(); i++ {
tag := v.Type().Field(i).Tag.Get("multipart")
if tag == "" || tag == "-" {
Expand Down Expand Up @@ -45,7 +51,7 @@ func parseTag(tag string) multipartField {
continue
}

parts := strings.SplitN(part, "=", 2)
parts := strings.SplitN(part, "=", numberOfPartsKeyValue)
key, value := parts[0], parts[1]

switch key {
Expand Down
10 changes: 6 additions & 4 deletions core/type.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package core

const unitMultiplier = 1024

// String is a simple string that can be nil.
type String *string

Expand Down Expand Up @@ -37,20 +39,20 @@ func (d DataSize) Bytes() uint64 {

// Kilobytes returns with the calculated kilobytes value as a float.
func (d DataSize) Kilobytes() float64 {
return float64(d.Bytes()) / 1024
return float64(d.Bytes()) / unitMultiplier
}

// Megabytes returns with the calculated megabytes value as a float.
func (d DataSize) Megabytes() float64 {
return d.Kilobytes() / 1024
return d.Kilobytes() / unitMultiplier
}

// Gigabytes returns with the calculated gigabytes value as a float.
func (d DataSize) Gigabytes() float64 {
return d.Megabytes() / 1024
return d.Megabytes() / unitMultiplier
}

// Terabytes returns with the calculated terabytes value as a float.
func (d DataSize) Terabytes() float64 {
return d.Gigabytes() / 1024
return d.Gigabytes() / unitMultiplier
}
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ require (
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/sirupsen/logrus v1.7.0
github.com/stretchr/testify v1.6.1
golang.org/x/net v0.0.0-20201024042810-be3efd7ff127
golang.org/x/sys v0.0.0-20201024132449-ef9fd89ba245 // indirect
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf // indirect
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b // indirect
)
13 changes: 8 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
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/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
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/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM=
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
Expand All @@ -20,18 +17,24 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20201024042810-be3efd7ff127 h1:pZPp9+iYUqwYKLjht0SDBbRCRK/9gAXDy7pz5fRDpjo=
golang.org/x/net v0.0.0-20201024042810-be3efd7ff127/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d h1:LO7XpTYMwTqxjLcGWPijK3vRXg1aWdlNOVOHRq45d7c=
golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f h1:+Nyd8tzPX9R7BWHguqsrbFdRx3WQ/1ib8I44HXV5yTA=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201024132449-ef9fd89ba245 h1:GGQcbpn3KsnwsPvzzr1mDsTriyvGNKi9eo2lG3N8YdM=
golang.org/x/sys v0.0.0-20201024132449-ef9fd89ba245/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf h1:2ucpDCmfkl8Bd/FsLtiD653Wf96cW37s+iGx93zsu4k=
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b h1:QRR6H1YWRnHb4Y/HeNFCTJLFVxaq6wH4YuVdsUOr75U=
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
6 changes: 4 additions & 2 deletions services/admin/announcements/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"github.com/yitsushi/go-misskey/models"
)

const maxLimit = 100

// ListRequest represents an List Announcement request.
type ListRequest struct {
Limit uint `json:"limit"`
Expand All @@ -14,10 +16,10 @@ type ListRequest struct {

// Validate the request.
func (r ListRequest) Validate() error {
if r.Limit < 1 || r.Limit > 100 {
if r.Limit < 1 || r.Limit > maxLimit {
return core.RequestValidationError{
Request: r,
Message: core.NewRangeError(1, 100),
Message: core.NewRangeError(1, maxLimit),
Field: "Limit",
}
}
Expand Down
3 changes: 1 addition & 2 deletions services/antennas/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (
)

// ListRequest is empty, but keep it as it is for consistency.
type ListRequest struct {
}
type ListRequest struct{}

// Validate the request.
func (r ListRequest) Validate() error {
Expand Down
5 changes: 3 additions & 2 deletions services/antennas/notes.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
const (
// NoteListDefaultLimit is the default value for notes list.
NoteListDefaultLimit = 10
noteMaxLimit = 100
)

// NotesRequest represents a request to fetch notes for a given Antenna.
Expand All @@ -20,10 +21,10 @@ type NotesRequest struct {

// Validate the request.
func (r NotesRequest) Validate() error {
if r.Limit < 1 || r.Limit > 100 {
if r.Limit < 1 || r.Limit > noteMaxLimit {
return core.RequestValidationError{
Request: r,
Message: core.NewRangeError(1, 100),
Message: core.NewRangeError(1, noteMaxLimit),
Field: "Limit",
}
}
Expand Down
3 changes: 1 addition & 2 deletions services/clips/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (
)

// ListRequest represents an List request.
type ListRequest struct {
}
type ListRequest struct{}

// Validate the request.
func (r ListRequest) Validate() error {
Expand Down
4 changes: 2 additions & 2 deletions services/drive/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ type FilesRequest struct {

// Validate the request.
func (r FilesRequest) Validate() error {
if r.Limit < 1 || r.Limit > 100 {
if r.Limit < 1 || r.Limit > maxLimit {
return core.RequestValidationError{
Request: r,
Message: core.NewRangeError(1, 100),
Message: core.NewRangeError(1, maxLimit),
Field: "Limit",
}
}
Expand Down
27 changes: 17 additions & 10 deletions services/drive/files/create_from_url.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@ import (
"net/http"
"time"

"github.com/yitsushi/go-misskey/core"
"github.com/yitsushi/go-misskey/models"
)

// RequestTimout is the timeout of a request in seconds.
const RequestTimout = 30

// CreateFromURLOptions has all the values you can play with.
type CreateFromURLOptions struct {
FolderID string
Name string
IsSensitive bool
Force bool
URL string
FolderID string
Name string
IsSensitive bool
Force bool
URL string
DownloadClient core.HTTPClient
}

// CreateFromURL downloads a file and then uploads it to the server.
Expand All @@ -27,24 +32,26 @@ type CreateFromURLOptions struct {
// UploadFromURL. That way, we can get back a File from the request.
// And of course, because something stinks to me around that endpoint.
// Why? Check the comment on UploadFromURL.
func (s *Service) CreateFromURL(options *CreateFromURLOptions) (models.File, error) {
func (s *Service) CreateFromURL(options CreateFromURLOptions) (models.File, error) {
return s.Create(CreateRequest{
FolderID: options.FolderID,
Name: options.Name,
IsSensitive: options.IsSensitive,
Force: options.Force,
Content: downloadFile(options.URL),
Content: downloadFile(options.URL, options.DownloadClient),
})
}

func downloadFile(url string) []byte {
func downloadFile(url string, client core.HTTPClient) []byte {
req, err := http.NewRequestWithContext(context.Background(), "GET", url, nil)
if err != nil {
return []byte{}
}

client := &http.Client{
Timeout: time.Minute,
if client == nil {
client = &http.Client{
Timeout: time.Second * RequestTimout,
}
}

resp, err := client.Do(req)
Expand Down
Loading

0 comments on commit ad3e552

Please sign in to comment.