Skip to content

Commit

Permalink
Split client into multiple files and add more tests (woodpecker-ci#3647)
Browse files Browse the repository at this point in the history
All the client functions were in a single file, which was already very
long, and the test file gets even longer as more tests are added. I
split it into separate files representing the API path and started
adding some tests.
  • Loading branch information
xoxys authored and 6543 committed Sep 5, 2024
1 parent 9c1c7e5 commit 8477dea
Show file tree
Hide file tree
Showing 16 changed files with 1,499 additions and 582 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*.so
*.dylib
vendor/
__debug_bin
__debug_bin*

# Test binary, built with `go test -c`
*.test
Expand Down
50 changes: 50 additions & 0 deletions woodpecker-go/woodpecker/agent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package woodpecker

import "fmt"

const (
pathAgents = "%s/api/agents"
pathAgent = "%s/api/agents/%d"
pathAgentTasks = "%s/api/agents/%d/tasks"
)

// AgentCreate creates a new agent.
func (c *client) AgentCreate(in *Agent) (*Agent, error) {
out := new(Agent)
uri := fmt.Sprintf(pathAgents, c.addr)
return out, c.post(uri, in, out)
}

// AgentList returns a list of all registered agents.
func (c *client) AgentList() ([]*Agent, error) {
out := make([]*Agent, 0, 5)
uri := fmt.Sprintf(pathAgents, c.addr)
return out, c.get(uri, &out)
}

// Agent returns an agent by id.
func (c *client) Agent(agentID int64) (*Agent, error) {
out := new(Agent)
uri := fmt.Sprintf(pathAgent, c.addr, agentID)
return out, c.get(uri, out)
}

// AgentUpdate updates the agent with the provided Agent struct.
func (c *client) AgentUpdate(in *Agent) (*Agent, error) {
out := new(Agent)
uri := fmt.Sprintf(pathAgent, c.addr, in.ID)
return out, c.patch(uri, in, out)
}

// AgentDelete deletes the agent with the given id.
func (c *client) AgentDelete(agentID int64) error {
uri := fmt.Sprintf(pathAgent, c.addr, agentID)
return c.delete(uri)
}

// AgentTasksList returns a list of all tasks for the agent with the given id.
func (c *client) AgentTasksList(agentID int64) ([]*Task, error) {
out := make([]*Task, 0, 5)
uri := fmt.Sprintf(pathAgentTasks, c.addr, agentID)
return out, c.get(uri, &out)
}
Loading

0 comments on commit 8477dea

Please sign in to comment.