Skip to content
108 changes: 108 additions & 0 deletions asset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package client

import (
"encoding/json"
"fmt"
"net/http"
)

const (
assetAPIEndpoint = "service/rest/v1/assets"
)

type AssetResponse struct {
Comment thread
anmoel marked this conversation as resolved.
Outdated
Items []Asset `json:"items,omitempty"`
ContinuationToken string `json:"continuationToken,omitempty"`
}

type Asset struct {
DownloadUrl string `json:"download_url,omitempty"`
Comment thread
anmoel marked this conversation as resolved.
Outdated
Path string `json:"path,omitempty"`
ID string `json:"id,omitempty"`
Repository string `json:"repository,omitempty"`
Format string `json:"format,omitempty"`
}

func jsonUnmarshalAssetResponse(data []byte) (*AssetResponse, error) {
var assetResponse AssetResponse
if err := json.Unmarshal(data, &assetResponse); err != nil {
return nil, fmt.Errorf("could not unmarshal assetResponse: %v", err)
}
return &assetResponse, nil
}

func jsonUnmarshalAsset(data []byte) (*Asset, error) {
var asset Asset
if err := json.Unmarshal(data, &asset); err != nil {
return nil, fmt.Errorf("could not unmarshal Asset: %v", err)
}
return &asset, nil
}

func (c client) AssetRead(id string) (*Asset, error) {
body, resp, err := c.Delete(fmt.Sprintf("%s/%s", assetAPIEndpoint, id))
Comment thread
anmoel marked this conversation as resolved.
Outdated
if err != nil {
return nil, err
}

if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
return nil, fmt.Errorf("could not delete Asset '%s': HTTP: %d, %s", id, resp.StatusCode, string(body))
Comment thread
anmoel marked this conversation as resolved.
Outdated
}

asset, err := jsonUnmarshalAsset(body)
if err != nil {
return nil, err
}

return asset, nil
}

func (c client) AssetDelete(id string) error {
body, resp, err := c.Delete(fmt.Sprintf("%s/%s", assetAPIEndpoint, id))
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("could not delete asset '%s': HTTP: %d, %s", id, resp.StatusCode, string(body))
}
return nil
}

func (c client) AssetList(repository string) ([]Asset, error) {
body, resp, err := c.Get(fmt.Sprintf("%s?repository=%s", assetAPIEndpoint, repository), nil)
if err != nil {
return nil, err
}

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("could not read repository '%s' asset list : HTTP: %d, %s",
repository, resp.StatusCode, string(body))
}

assetResponse, err := jsonUnmarshalAssetResponse(body)
if err != nil {
return nil, err
}

list := assetResponse.Items
for assetResponse.ContinuationToken != "" {
body, resp, err := c.Get(fmt.Sprintf("%s?repository=%s&continuationToken=%s", assetAPIEndpoint, repository, assetResponse.ContinuationToken), nil)
if err != nil {
return nil, err
}

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("could not read repository '%s' asset list : HTTP: %d, %s",
repository, resp.StatusCode, string(body))
}

assetResponse, err := jsonUnmarshalAssetResponse(body)
if err != nil {
return nil, err
}

list = append(list, assetResponse.Items...)
}

return assetResponse.Items, nil
}
1 change: 1 addition & 0 deletions asset_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package client

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please create tests

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add tests for all functions

8 changes: 8 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ const (

// Client represents the Nexus API Client interface
type Client interface {
AssetRead(string) (*Asset, error)
AssetDelete(string) error
AssetList(string) ([]Asset, error)
BlobstoreCreate(Blobstore) error
BlobstoreDelete(string) error
BlobstoreRead(string) (*Blobstore, error)
Expand Down Expand Up @@ -51,6 +54,11 @@ type Client interface {
RepositoryDelete(string) error
RepositoryRead(string) (*Repository, error)
RepositoryUpdate(string, Repository) error
RepositoryList() ([]Repository, error)
ComponentRead(string) (*Component, error)
ComponentUpload(string, Component) error
ComponentDelete(string) error
ComponentList(string) ([]Component, error)
RoleCreate(Role) error
RoleDelete(string) error
RoleRead(string) (*Role, error)
Expand Down
114 changes: 114 additions & 0 deletions component.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package client

import (
"encoding/json"
"fmt"
"net/http"
)

const (
componentAPIEndpoint = "service/rest/v1/components"
)

type ComponentResponse struct {
Comment thread
anmoel marked this conversation as resolved.
Outdated
Items []Component `json:"items,omitempty"`
ContinuationToken string `json:"continuationToken,omitempty"`
}

// Component is the base structure for Nexus Component
type Component struct {
ID string `json:"id,omitempty"`
Repository string `json:"repository,omitempty"`
Format string `json:"format,omitempty"`
Group string `json:"group,omitempty"`
Name string `json:"name,omitempty"`
Version string `json:"version,omitempty"`
}

func jsonUnmarshalComponentResponse(data []byte) (*ComponentResponse, error) {
var componentResponse ComponentResponse
if err := json.Unmarshal(data, &componentResponse); err != nil {
return nil, fmt.Errorf("could not unmarshal componentResponse: %v", err)
}
return &componentResponse, nil
}

func jsonUnmarshalComponent(data []byte) (*Component, error) {
var component Component
if err := json.Unmarshal(data, &component); err != nil {
return nil, fmt.Errorf("could not unmarshal component: %v", err)
}
return &component, nil
}

func (c client) ComponentRead(id string) (*Component, error) {
body, resp, err := c.Delete(fmt.Sprintf("%s/%s", componentAPIEndpoint, id))
Comment thread
anmoel marked this conversation as resolved.
Outdated
if err != nil {
return nil, err
}

if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
return nil, fmt.Errorf("could not delete component '%s': HTTP: %d, %s", id, resp.StatusCode, string(body))
Comment thread
anmoel marked this conversation as resolved.
Outdated
}

component, err := jsonUnmarshalComponent(body)
if err != nil {
return nil, err
}

return component, nil
}

func (c client) ComponentUpload(s string, component Component) error {
panic("implement me")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please implement

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please implement the logic or remove the function

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add tests for all functions

}

func (c client) ComponentDelete(id string) error {
body, resp, err := c.Delete(fmt.Sprintf("%s/%s", componentAPIEndpoint, id))
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("could not delete component '%s': HTTP: %d, %s", id, resp.StatusCode, string(body))
}
return nil
}

func (c client) ComponentList(repository string) ([]Component, error) {
body, resp, err := c.Get(fmt.Sprintf("%s?repository=%s", componentAPIEndpoint, repository), nil)
if err != nil {
return nil, err
}

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("could not read repository '%s' component list : HTTP: %d, %s",
repository, resp.StatusCode, string(body))
}

componentResponse, err := jsonUnmarshalComponentResponse(body)
if err != nil {
return nil, err
}

list := componentResponse.Items
for componentResponse.ContinuationToken != "" {
body, resp, err := c.Get(fmt.Sprintf("%s?repository=%s&continuationToken=%s", componentAPIEndpoint, repository, componentResponse.ContinuationToken), nil)
if err != nil {
return nil, err
}

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("could not read repository '%s' component list : HTTP: %d, %s",
repository, resp.StatusCode, string(body))
}

componentResponse, err := jsonUnmarshalComponentResponse(body)
if err != nil {
return nil, err
}

list = append(list, componentResponse.Items...)
}

return list, nil
}
1 change: 1 addition & 0 deletions component_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package client
Comment thread
shentuzhigang marked this conversation as resolved.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module github.com/datadrivers/go-nexus-client
module github.com/shentuzhigang/nexus-client-go

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove this change


go 1.14
go 1.16

require (
github.com/google/go-querystring v1.0.0
github.com/minio/minio-go/v7 v7.0.11 // indirect
github.com/minio/minio-go/v7 v7.0.11
github.com/stretchr/testify v1.4.0
)
11 changes: 7 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
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=
Expand All @@ -9,15 +8,19 @@ github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/json-iterator/go v1.1.10 h1:Kz6Cvnvv2wGdaG/V8yMvfkmNiXq9Ya2KUv4rouJJr68=
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/klauspost/cpuid v1.2.3/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
github.com/klauspost/cpuid v1.3.1 h1:5JNjFYYQrZeKRJ0734q51WCEEn2huer72Dc7K+R/b6s=
github.com/klauspost/cpuid v1.3.1/go.mod h1:bYW4mA6ZgKPob1/Dlai2LviZJO7KGI3uoWLd42rAQw4=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
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/minio/md5-simd v1.1.0 h1:QPfiOqlZH+Cj9teu0t9b1nTBfPbyTl16Of5MeuShdK4=
github.com/minio/md5-simd v1.1.0/go.mod h1:XpBqgZULrMYD3R+M28PcmP0CkI7PEMzB3U77ZrKZ0Gw=
Expand All @@ -39,9 +42,10 @@ github.com/rs/xid v1.2.1 h1:mhH9Nq+C1fY2l1XIpgxIiUOfNpRBYH1kKcr+qfKgjRc=
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
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.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand All @@ -66,12 +70,11 @@ golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
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/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/ini.v1 v1.57.0 h1:9unxIsFcTt4I55uWluz+UmL95q4kdJ0buvQ1ZIqVQww=
gopkg.in/ini.v1 v1.57.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
30 changes: 20 additions & 10 deletions repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type Repository struct {
Online bool `json:"online"`
RoutingRuleName *string `json:"routingRuleName,omitempty"`
Type string `json:"type"`
Url string `json:"url"`

// Apt Repository data
*RepositoryApt `json:"apt,omitempty"`
Expand Down Expand Up @@ -194,16 +195,7 @@ func (c client) RepositoryCreate(repo Repository) error {
}

func (c client) RepositoryRead(id string) (*Repository, error) {
body, resp, err := c.Get(fmt.Sprintf("%s", repositoryAPIEndpoint), nil)
if err != nil {
return nil, err
}

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("could not read repository '%s': HTTP: %d, %s", id, resp.StatusCode, string(body))
}

repositories, err := jsonUnmarshalRepositories(body)
repositories, err := c.RepositoryList()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -245,3 +237,21 @@ func (c client) RepositoryDelete(id string) error {
}
return nil
}

func (c client) RepositoryList() ([]Repository, error) {
body, resp, err := c.Get(fmt.Sprintf("%s", repositoryAPIEndpoint), nil)
if err != nil {
return nil, err
}

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("could not read repository list: HTTP: %d, %s", resp.StatusCode, string(body))
}

repositories, err := jsonUnmarshalRepositories(body)
if err != nil {
return nil, err
}

return repositories, nil
}