-
Notifications
You must be signed in to change notification settings - Fork 47
Add asset and component api #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 10 commits
874afc4
eea26c4
12444fd
3469a91
41bc0fc
5e94fb9
b1efc85
42122ce
3f09e5f
0c072f8
6764baa
8dbc4a1
77d48d0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 AssetList struct { | ||
| Items []Asset `json:"items,omitempty"` | ||
| ContinuationToken string `json:"continuationToken,omitempty"` | ||
| } | ||
|
|
||
| type Asset struct { | ||
| DownloadUrl string `json:"downloadUrl,omitempty"` | ||
| Path string `json:"path,omitempty"` | ||
| ID string `json:"id,omitempty"` | ||
| Repository string `json:"repository,omitempty"` | ||
| Format string `json:"format,omitempty"` | ||
| } | ||
|
|
||
| func jsonUnmarshalAssetList(data []byte) (*AssetList, error) { | ||
| var assetList AssetList | ||
| if err := json.Unmarshal(data, &assetList); err != nil { | ||
| return nil, fmt.Errorf("could not unmarshal assetList: %v", err) | ||
| } | ||
| return &assetList, 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.Get(fmt.Sprintf("%s/%s", assetAPIEndpoint, id), nil) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent { | ||
| return nil, fmt.Errorf("could not read Asset '%s': HTTP: %d, %s", id, resp.StatusCode, string(body)) | ||
| } | ||
|
|
||
| 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)) | ||
| } | ||
|
|
||
| assetList, err := jsonUnmarshalAssetList(body) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| list := assetList.Items | ||
| for assetList.ContinuationToken != "" { | ||
| body, resp, err := c.Get(fmt.Sprintf("%s?repository=%s&continuationToken=%s", assetAPIEndpoint, repository, assetList.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 := jsonUnmarshalAssetList(body) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| list = append(list, assetResponse.Items...) | ||
| } | ||
|
|
||
| return assetList.Items, nil | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package client | ||
|
|
||
| import ( | ||
| "github.com/stretchr/testify/assert" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestAssetRead(t *testing.T) { | ||
| client := getTestClient() | ||
|
|
||
| id := "" | ||
|
|
||
| asset, err := client.AssetRead(id) | ||
| assert.Nil(t, err) | ||
| assert.NotNil(t, asset) | ||
|
|
||
| if asset != nil { | ||
| assert.Equal(t, id, asset.ID) | ||
| assert.Equal(t, "maven-central", asset.Repository) | ||
| } | ||
| } | ||
| 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 ComponentList struct { | ||
| 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 jsonUnmarshalComponentList(data []byte) (*ComponentList, error) { | ||
| var componentList ComponentList | ||
| if err := json.Unmarshal(data, &componentList); err != nil { | ||
| return nil, fmt.Errorf("could not unmarshal componentList: %v", err) | ||
| } | ||
| return &componentList, 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.Get(fmt.Sprintf("%s/%s", componentAPIEndpoint, id), nil) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent { | ||
| return nil, fmt.Errorf("could not read component '%s': HTTP: %d, %s", id, resp.StatusCode, string(body)) | ||
| } | ||
|
|
||
| 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") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please implement
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please implement the logic or remove the function
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) | ||
| } | ||
|
|
||
| componentList, err := jsonUnmarshalComponentList(body) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| list := componentList.Items | ||
| for componentList.ContinuationToken != "" { | ||
| body, resp, err := c.Get(fmt.Sprintf("%s?repository=%s&continuationToken=%s", componentAPIEndpoint, repository, componentList.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 := jsonUnmarshalComponentList(body) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| list = append(list, componentResponse.Items...) | ||
| } | ||
|
|
||
| return list, nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package client | ||
|
shentuzhigang marked this conversation as resolved.
|
||
|
|
||
| import ( | ||
| "github.com/stretchr/testify/assert" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestComponentReadRead(t *testing.T) { | ||
| client := getTestClient() | ||
|
|
||
| id := "" | ||
|
|
||
| component, err := client.ComponentRead(id) | ||
| assert.Nil(t, err) | ||
| assert.NotNil(t, component) | ||
|
|
||
| if component != nil { | ||
| assert.Equal(t, id, component.ID) | ||
| assert.Equal(t, "maven-central", component.Repository) | ||
| } | ||
| } | ||
| 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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| ) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please create tests
There was a problem hiding this comment.
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