67 lines
1.6 KiB
Go
67 lines
1.6 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
)
|
|
|
|
type Project struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
Description string `json:"description,omitempty"`
|
|
Status string `json:"status"`
|
|
CreatedAt string `json:"created_at"`
|
|
UpdatedAt string `json:"updated_at"`
|
|
}
|
|
|
|
func (c *Client) ListProjects(ctx context.Context) ([]Project, error) {
|
|
var response struct {
|
|
Projects []Project `json:"projects"`
|
|
}
|
|
if err := c.getJSON(ctx, "/api/v1/projects", &response); err != nil {
|
|
return nil, err
|
|
}
|
|
return response.Projects, nil
|
|
}
|
|
|
|
func (c *Client) GetProject(ctx context.Context, project string) (Project, error) {
|
|
project = strings.TrimSpace(project)
|
|
if project == "" {
|
|
return Project{}, errors.New("项目标识不能为空")
|
|
}
|
|
var result Project
|
|
path := "/api/v1/projects/" + url.PathEscape(project)
|
|
if err := c.getJSON(ctx, path, &result); err != nil {
|
|
return Project{}, err
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (c *Client) CreateProject(ctx context.Context, project Project) (Project, error) {
|
|
var result Project
|
|
if err := c.postJSON(ctx, "/api/v1/projects", map[string]string{
|
|
"id": project.ID,
|
|
"name": project.Name,
|
|
"description": project.Description,
|
|
}, &result); err != nil {
|
|
return Project{}, err
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (c *Client) ProjectHealth(ctx context.Context) error {
|
|
request, err := c.newRequest(ctx, http.MethodGet, "/healthz", nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
response, err := c.httpClient.Do(request)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer response.Body.Close()
|
|
return checkResponse(response)
|
|
}
|