mirror of
https://github.com/coder/coder.git
synced 2025-07-10 23:53:15 +00:00
* feat: Add project models * Add project query functions * Add organization parameter query * Add project URL parameter parse * Add project create and list endpoints * Add test for organization provided * Remove unimplemented routes * Decrease conn timeout * Add test for UnbiasedModulo32 * Fix expected value * Add single user endpoint * Add query for project versions * Fix linting errors * Add comments * Add test for invalid archive * Check unauthenticated endpoints * Add check if no change happened * Ensure context close ends listener * Fix parallel test run * Test empty * Fix organization param comment
87 lines
3.0 KiB
Go
87 lines
3.0 KiB
Go
package codersdk
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/coder/coder/coderd"
|
|
)
|
|
|
|
// Projects lists projects inside an organization.
|
|
// If organization is an empty string, all projects will be returned
|
|
// for the authenticated user.
|
|
func (c *Client) Projects(ctx context.Context, organization string) ([]coderd.Project, error) {
|
|
route := "/api/v2/projects"
|
|
if organization != "" {
|
|
route = fmt.Sprintf("/api/v2/projects/%s", organization)
|
|
}
|
|
res, err := c.request(ctx, http.MethodGet, route, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer res.Body.Close()
|
|
if res.StatusCode != http.StatusOK {
|
|
return nil, readBodyAsError(res)
|
|
}
|
|
var projects []coderd.Project
|
|
return projects, json.NewDecoder(res.Body).Decode(&projects)
|
|
}
|
|
|
|
// Project returns a single project.
|
|
func (c *Client) Project(ctx context.Context, organization, project string) (coderd.Project, error) {
|
|
res, err := c.request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/projects/%s/%s", organization, project), nil)
|
|
if err != nil {
|
|
return coderd.Project{}, nil
|
|
}
|
|
defer res.Body.Close()
|
|
if res.StatusCode != http.StatusOK {
|
|
return coderd.Project{}, readBodyAsError(res)
|
|
}
|
|
var resp coderd.Project
|
|
return resp, json.NewDecoder(res.Body).Decode(&resp)
|
|
}
|
|
|
|
// CreateProject creates a new project inside an organization.
|
|
func (c *Client) CreateProject(ctx context.Context, organization string, request coderd.CreateProjectRequest) (coderd.Project, error) {
|
|
res, err := c.request(ctx, http.MethodPost, fmt.Sprintf("/api/v2/projects/%s", organization), request)
|
|
if err != nil {
|
|
return coderd.Project{}, err
|
|
}
|
|
defer res.Body.Close()
|
|
if res.StatusCode != http.StatusCreated {
|
|
return coderd.Project{}, readBodyAsError(res)
|
|
}
|
|
var project coderd.Project
|
|
return project, json.NewDecoder(res.Body).Decode(&project)
|
|
}
|
|
|
|
// ProjectVersions lists history for a project.
|
|
func (c *Client) ProjectVersions(ctx context.Context, organization, project string) ([]coderd.ProjectVersion, error) {
|
|
res, err := c.request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/projects/%s/%s/versions", organization, project), nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer res.Body.Close()
|
|
if res.StatusCode != http.StatusOK {
|
|
return nil, readBodyAsError(res)
|
|
}
|
|
var projectVersions []coderd.ProjectVersion
|
|
return projectVersions, json.NewDecoder(res.Body).Decode(&projectVersions)
|
|
}
|
|
|
|
// CreateProjectVersion inserts a new version for the project.
|
|
func (c *Client) CreateProjectVersion(ctx context.Context, organization, project string, request coderd.CreateProjectVersionRequest) (coderd.ProjectVersion, error) {
|
|
res, err := c.request(ctx, http.MethodPost, fmt.Sprintf("/api/v2/projects/%s/%s/versions", organization, project), request)
|
|
if err != nil {
|
|
return coderd.ProjectVersion{}, err
|
|
}
|
|
defer res.Body.Close()
|
|
if res.StatusCode != http.StatusCreated {
|
|
return coderd.ProjectVersion{}, readBodyAsError(res)
|
|
}
|
|
var projectVersion coderd.ProjectVersion
|
|
return projectVersion, json.NewDecoder(res.Body).Decode(&projectVersion)
|
|
}
|