Skip to content

Commit

Permalink
Update pkg to Go 1.18 and implement new API endpoint /user
Browse files Browse the repository at this point in the history
  • Loading branch information
ManuelReschke committed Nov 30, 2022
1 parent ac0c60f commit 8f5e782
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.17
go-version: 1.18

- name: test
run: make test
Expand Down
18 changes: 0 additions & 18 deletions CHANGELOG.md

This file was deleted.

9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,11 @@ func main() {
| [x] POST - /list | CreateList(r *RequestCreateList) (*ResponseCreateList, error) |
| [x] GET - /list/{id} | GetList(r *RequestGetList) (*ResponseGetList, error) |
### User Methods
| PixelDrain Call | Package Func |
|------------------------|---|
| [x] POST - /user/files | GetUserFiles(r *RequestGetUserFiles) (*ResponseGetUserFiles, error) |
| [x] GET - /user/lists | GetUserLists(r *RequestGetUserLists) (*ResponseGetUserLists, error) |
| PixelDrain Call | Package Func |
|------------------|---|
| [x] POST - /user | GetUser(r *RequestGetUser) (*ResponseGetUser, error) |
| [x] POST - /user/files | GetUserFiles(r *RequestGetUserFiles) (*ResponseGetUserFiles, error) |
| [x] GET - /user/lists | GetUserLists(r *RequestGetUserLists) (*ResponseGetUserLists, error) |

## Package CLI commands

Expand Down
12 changes: 6 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
module github.com/ManuelReschke/go-pd

go 1.17
go 1.18

require (
github.com/imroc/req v0.3.2
github.com/joho/godotenv v1.4.0
github.com/stretchr/testify v1.7.0
github.com/spf13/cobra v1.6.1
github.com/stretchr/testify v1.8.1
)

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/cobra v1.4.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
16 changes: 16 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
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/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/imroc/req v0.3.2 h1:M/JkeU6RPmX+WYvT2vaaOL0K+q8ufL5LxwvJc4xeB4o=
github.com/imroc/req v0.3.2/go.mod h1:F+NZ+2EFSo6EFXdeIbpfE9hcC233id70kf0byW97Caw=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
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/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q=
github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g=
github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA=
github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
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.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
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=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
34 changes: 34 additions & 0 deletions pkg/pd/mock_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,40 @@ func MockFileUploadServer() *httptest.Server {
_, _ = w.Write([]byte(str))
}

// ##########################################
// GET /user
if r.URL.EscapedPath() == "/user" {
_ = r.ParseForm()

w.WriteHeader(http.StatusOK)
str := `{
"username":"TestTest",
"email":"test@test.de",
"subscription":{
"id":"",
"name":"Free",
"type":"",
"file_size_limit":20000000000,
"file_expiry_days":60,
"storage_space":-1,
"price_per_tb_storage":0,
"price_per_tb_bandwidth":0,
"monthly_transfer_cap":0,
"file_viewer_branding":false
},
"storage_space_used":18834,
"is_admin":false,
"balance_micro_eur":0,
"hotlinking_enabled":true,
"monthly_transfer_cap":0,
"monthly_transfer_used":0,
"file_viewer_branding":null,
"file_embed_domains":"",
"skip_file_viewer":false
}`
_, _ = w.Write([]byte(str))
}

// ##########################################
// GET /user/lists
if r.URL.EscapedPath() == "/user/lists" {
Expand Down
36 changes: 36 additions & 0 deletions pkg/pd/pd.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,42 @@ func (pd *PixelDrainClient) GetList(r *RequestGetList) (*ResponseGetList, error)
return rspStruct, nil
}

// GetUser GET /api/user
func (pd *PixelDrainClient) GetUser(r *RequestGetUser) (*ResponseGetUser, error) {
if r.URL == "" {
r.URL = APIURL + "/user"
}

// pixeldrain want an empty username and the APIKey as password
if r.Auth.IsAuthAvailable() {
addBasicAuthHeader(pd.Client.Header, "", r.Auth.APIKey)
}

rsp, err := pd.Client.Request.Get(r.URL, pd.Client.Header)
if pd.Debug {
log.Println(rsp.Dump())
}
if err != nil {
return nil, err
}

rspStruct := &ResponseGetUser{}
err = rsp.ToJSON(rspStruct)
if err != nil {
return nil, err
}

status := false
if rsp.Response().StatusCode == http.StatusOK {
status = true
}

rspStruct.Success = status
rspStruct.StatusCode = rsp.Response().StatusCode

return rspStruct, nil
}

// GetUserFiles GET /api/user/files
func (pd *PixelDrainClient) GetUserFiles(r *RequestGetUserFiles) (*ResponseGetUserFiles, error) {
if r.URL == "" {
Expand Down
46 changes: 46 additions & 0 deletions pkg/pd/pd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,52 @@ func TestPD_GetList_Integration(t *testing.T) {
assert.Equal(t, 37621, rsp.Files[0].Size)
}

// TestPD_GetUser is a unit test for the GET "/user" method
func TestPD_GetUser(t *testing.T) {
server := pd.MockFileUploadServer()
defer server.Close()
testURL := server.URL + "/user"

req := &pd.RequestGetUser{
URL: testURL,
}

req.Auth = setAuthFromEnv()

c := pd.New(nil, nil)
rsp, err := c.GetUser(req)
if err != nil {
t.Error(err)
}

assert.Equal(t, 200, rsp.StatusCode)
assert.Equal(t, true, rsp.Success)
assert.Equal(t, "TestTest", rsp.Username)
assert.Equal(t, "Free", rsp.Subscription.Name)
}

// TestPD_GetUser_Integration run a real integration test against the service
func TestPD_GetUser_Integration(t *testing.T) {
if testing.Short() {
t.Skip(SkipIntegrationTest)
}

req := &pd.RequestGetUser{}

req.Auth = setAuthFromEnv()

c := pd.New(nil, nil)
rsp, err := c.GetUser(req)
if err != nil {
t.Error(err)
}

assert.Equal(t, 200, rsp.StatusCode)
assert.Equal(t, true, rsp.Success)
assert.Equal(t, "ManuelReschke", rsp.Username)
assert.Equal(t, "Free", rsp.Subscription.Name)
}

// TestPD_GetUserFiles is a unit test for the GET "/user/files" method
func TestPD_GetUserFiles(t *testing.T) {
server := pd.MockFileUploadServer()
Expand Down
6 changes: 6 additions & 0 deletions pkg/pd/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ type RequestGetList struct {
URL string
}

// RequestGetUser ...
type RequestGetUser struct {
Auth Auth
URL string
}

// RequestGetUserFiles ...
type RequestGetUserFiles struct {
Auth Auth
Expand Down
29 changes: 29 additions & 0 deletions pkg/pd/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,35 @@ type ResponseGetList struct {
ResponseDefault
}

type ResponseGetUser struct {
Username string `json:"username"`
Email string `json:"email"`
Subscription GetUserSubscription `json:"subscription"`
StorageSpaceUsed int `json:"storage_space_used"`
IsAdmin bool `json:"is_admin"`
BalanceMicroEur int `json:"balance_micro_eur"`
HotlinkingEnabled bool `json:"hotlinking_enabled"`
MonthlyTransferCap int `json:"monthly_transfer_cap"`
MonthlyTransferUsed int `json:"monthly_transfer_used"`
FileViewerBranding interface{} `json:"file_viewer_branding"`
FileEmbedDomains string `json:"file_embed_domains"`
SkipFileViewer bool `json:"skip_file_viewer"`
ResponseDefault
}

type GetUserSubscription struct {
ID string `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
FileSizeLimit int64 `json:"file_size_limit"`
FileExpiryDays int `json:"file_expiry_days"`
StorageSpace int `json:"storage_space"`
PricePerTbStorage int `json:"price_per_tb_storage"`
PricePerTbBandwidth int `json:"price_per_tb_bandwidth"`
MonthlyTransferCap int `json:"monthly_transfer_cap"`
FileViewerBranding bool `json:"file_viewer_branding"`
}

type FileGetUser struct {
ID string `json:"id"`
Name string `json:"name"`
Expand Down

0 comments on commit 8f5e782

Please sign in to comment.