Skip to content
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

Added 1:1 of search endpoints #18

Merged
merged 3 commits into from
Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@ This project tries to follow [SemVer 2.0.0](https://semver.org/).
When composing new changes to this list, try to follow convention.

The WIP release shall be updated just before adding the Git tag.
From (WIP) to (YYYY-MM-DD), ex: (2021-02-09) for 9th of Febuary, 2021
From (WIP) to (YYYY-MM-DD), ex: (2021-02-09) for 9th of February, 2021

A good source on conventions can be found here:
https://changelog.md/
-->

## v1.4.0 (WIP)

- Added methods for search endpoints: (#18)

- `Client.SearchProject(Project) []Project`: `POST /api/projects/search`
- `Client.SearchProvider(Provider) []Provider`: `POST /api/providers/search`
- `Client.SearchToken(Token) []Token`: `POST /api/tokens/search`

## v1.3.1 (2021-08-30)

- Added documentation to all exported methods. (#11)
Expand Down
26 changes: 26 additions & 0 deletions pkg/wharfapi/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,32 @@ func (c Client) GetProjectByID(projectID uint) (Project, error) {
return newProject, nil
}

// SearchProject tries to match the given project on all non-zero fields by
// invoking the HTTP request:
// POST /api/projects/search
func (c Client) SearchProject(project Project) ([]Project, error) {
body, err := json.Marshal(project)
if err != nil {
return nil, err
}

url := fmt.Sprintf("%s/api/projects/search", c.APIURL)
ioBody, err := doRequest("SEARCH | PROJECT |", http.MethodPost, url, body, c.AuthHeader)
if err != nil {
return nil, err
}

defer (*ioBody).Close()

var foundProjects []Project
err = json.NewDecoder(*ioBody).Decode(&foundProjects)
if err != nil {
return nil, err
}

return foundProjects, nil
}

// PutProject tries to match an existing project by ID or name+group and updates
// it, or adds a new a project if none matched, by invoking the HTTP request:
// PUT /api/project
Expand Down
30 changes: 29 additions & 1 deletion pkg/wharfapi/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (c Client) GetProviderByID(providerID uint) (Provider, error) {

// GetProvider tries to find a provider based on its name, URL, etc. by invoking
// the HTTP request:
// GET /api/providers/search
// POST /api/providers/search
func (c Client) GetProvider(providerName string, urlStr string, uploadURLStr string, tokenID uint) (Provider, error) {
newProvider := Provider{}

Expand Down Expand Up @@ -77,6 +77,34 @@ func (c Client) GetProvider(providerName string, urlStr string, uploadURLStr str
return providers[0], nil
}

// SearchProvider tries to match the given provider on the provider name, URL,
// upload URL, and token ID, by invoking the HTTP request:
// POST /api/providers/search
//
// The token ID is not queried if the argument's tokenID field is set to zero.
func (c Client) SearchProvider(provider Provider) ([]Provider, error) {
body, err := json.Marshal(provider)
if err != nil {
return nil, err
}

url := fmt.Sprintf("%s/api/projects/search", c.APIURL)
ioBody, err := doRequest("SEARCH | PROVIDER |", http.MethodPost, url, body, c.AuthHeader)
if err != nil {
return nil, err
}

defer (*ioBody).Close()

var foundProviders []Provider
err = json.NewDecoder(*ioBody).Decode(&foundProviders)
if err != nil {
return nil, err
}

return foundProviders, nil
}

// PutProvider tries to match an existing provider by ID or combination of name,
// URL, etc. and updates it, or adds a new a provider if none matched,
// by invoking the HTTP request:
Expand Down
28 changes: 27 additions & 1 deletion pkg/wharfapi/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (c Client) GetTokenByID(tokenID uint) (Token, error) {

// GetToken tries to search for a token using the username+token pair by
// invoking the HTTP request:
// GET /api/token/search
// POST /api/token/search
func (c Client) GetToken(token string, userName string) (Token, error) {
newToken := Token{}

Expand Down Expand Up @@ -73,6 +73,32 @@ func (c Client) GetToken(token string, userName string) (Token, error) {
return tokens[0], nil
}

// SearchToken tries to match the given token on the token field and username
// field, by invoking the HTTP request:
// POST /api/tokens/search
func (c Client) SearchToken(token Token) ([]Token, error) {
body, err := json.Marshal(token)
if err != nil {
return nil, err
}

url := fmt.Sprintf("%s/api/projects/search", c.APIURL)
ioBody, err := doRequest("SEARCH | TOKEN |", http.MethodPost, url, body, c.AuthHeader)
if err != nil {
return nil, err
}

defer (*ioBody).Close()

var foundTokens []Token
err = json.NewDecoder(*ioBody).Decode(&foundTokens)
if err != nil {
return nil, err
}

return foundTokens, nil
}

// PutToken tries to match an existing token by ID or username+token and updates
// it, or adds a new a token if none matched, by invoking the HTTP request:
// PUT /api/token
Expand Down