-
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 7 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 AssetResponse struct { | ||
| Items []Asset `json:"items,omitempty"` | ||
| ContinuationToken string `json:"continuationToken,omitempty"` | ||
| } | ||
|
|
||
| type Asset struct { | ||
| DownloadUrl string `json:"download_url,omitempty"` | ||
|
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)) | ||
|
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)) | ||
|
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 | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| package client | ||
|
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 create tests
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 |
||
| 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 { | ||
|
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)) | ||
|
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)) | ||
|
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") | ||
|
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)) | ||
| } | ||
|
|
||
| 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 | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| package client | ||
|
shentuzhigang marked this conversation as resolved.
|
||
| 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 | ||
| ) | ||
Uh oh!
There was an error while loading. Please reload this page.