|
| 1 | +package render |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +type RenderClient struct { |
| 11 | + httpClient http.Client |
| 12 | + token string |
| 13 | +} |
| 14 | + |
| 15 | +type Deploy struct { |
| 16 | + ID string `json:"id"` |
| 17 | + Commit string `json:"commit"` |
| 18 | + Status string `json:"status"` |
| 19 | + CreatedAt time.Time `json:"createdAt"` |
| 20 | + UpdatedAt time.Time `json:"updatedAt"` |
| 21 | + FinishedAt time.Time `json:"finishedAt"` |
| 22 | +} |
| 23 | + |
| 24 | +type ServiceDetailsEnv struct { |
| 25 | + BuildCommand string `json:"buildCommand"` |
| 26 | + StartCommand string `json:"startCommand"` |
| 27 | + DockerCommand string `json:"dockerCommand"` |
| 28 | + DockerContext string `json:"dockerContext"` |
| 29 | + DockerfilePath string `json:"dockerfilePath"` |
| 30 | +} |
| 31 | + |
| 32 | +type ServiceDetails struct { |
| 33 | + Disk map[string]interface{} `json:"disk"` |
| 34 | + Env string `json:"env"` |
| 35 | + EnvSpecificDetails ServiceDetailsEnv `json:"envSpecificDetails"` |
| 36 | + HealthCheckPath string `json:"healthCheckPath"` |
| 37 | + NumInstances int `json:"numInstances"` |
| 38 | + OpenPorts map[string]interface{} `json:"openPorts"` |
| 39 | + PublishPath string `json:"publishPath"` |
| 40 | + ParentServer map[string]interface{} `json:"parentServer"` |
| 41 | + Plan string `json:"plan"` |
| 42 | + PullRequestPreviewsEnabled bool `json:"pullRequestPreviewsEnabled"` |
| 43 | + Region string `json:"region"` |
| 44 | + URL string `json:"url"` |
| 45 | + BuildCommand string `json:"buildCommand"` |
| 46 | +} |
| 47 | + |
| 48 | +type Service struct { |
| 49 | + ID string `json:"id"` |
| 50 | + Type string `json:"type"` |
| 51 | + Repo string `json:"repo"` |
| 52 | + Name string `json:"name"` |
| 53 | + AutoDeploy bool `json:"autoDeploy"` |
| 54 | + Branch string `json:"branch"` |
| 55 | + CreatedAt time.Time `json:"createdAt"` |
| 56 | + UpdatedAt time.Time `json:"updatedAt"` |
| 57 | + NotifyOnFail string `json:"notifyOnFail"` |
| 58 | + OwnerID string `json:"ownerId"` |
| 59 | + Slug string `json:"slug"` |
| 60 | + Suspenders []string `json:"suspenders"` |
| 61 | + Schedule string `json:"schedule"` |
| 62 | + LastSuccessfulRunAt time.Time `json:"lastSuccessfulRunAt"` |
| 63 | + ServiceDetails ServiceDetails `json:"serviceDetails"` |
| 64 | + Deploys []Deploy `json:"deploys"` |
| 65 | +} |
| 66 | + |
| 67 | +type ResponseItem struct { |
| 68 | + Cursor string `json:"cursor"` |
| 69 | + Service Service `json:"service,omitempty"` |
| 70 | + Deploy Deploy `json:"deploy,omitempty"` |
| 71 | +} |
| 72 | +type Response []ResponseItem |
| 73 | + |
| 74 | +func New(token string) (*RenderClient, error) { |
| 75 | + renderClient := new(RenderClient) |
| 76 | + renderClient.httpClient = http.Client{Timeout: 10 * time.Second} |
| 77 | + renderClient.token = token |
| 78 | + |
| 79 | + |
| 80 | + return renderClient, nil |
| 81 | +} |
| 82 | + |
| 83 | +func (r *RenderClient) ListServices() ([]Service, error) { |
| 84 | + req, err := http.NewRequest( |
| 85 | + "GET", |
| 86 | + "https://api.render.com/v1/services", |
| 87 | + nil, |
| 88 | + ) |
| 89 | + if err != nil { |
| 90 | + return []Service{}, err |
| 91 | + } |
| 92 | + |
| 93 | + req.Header.Add( |
| 94 | + "Authorization", |
| 95 | + fmt.Sprintf("Bearer %s", r.token), |
| 96 | + ) |
| 97 | + resp, err := r.httpClient.Do(req) |
| 98 | + if err != nil { |
| 99 | + return []Service{}, err |
| 100 | + } |
| 101 | + |
| 102 | + defer resp.Body.Close() |
| 103 | + var response Response |
| 104 | + json.NewDecoder(resp.Body).Decode(&response) |
| 105 | + |
| 106 | + var services []Service |
| 107 | + for _, responseItem := range response { |
| 108 | + deploys, _ := r.ListDeploys(responseItem.Service.ID) |
| 109 | + |
| 110 | + responseItem.Service.Deploys = deploys |
| 111 | + services = append(services, responseItem.Service) |
| 112 | + } |
| 113 | + return services, nil |
| 114 | +} |
| 115 | + |
| 116 | +func (r *RenderClient) ListDeploys(serviceID string) ([]Deploy, error) { |
| 117 | + req, err := http.NewRequest( |
| 118 | + "GET", |
| 119 | + fmt.Sprintf("https://api.render.com/v1/services/%s/deploys", serviceID), |
| 120 | + nil, |
| 121 | + ) |
| 122 | + if err != nil { |
| 123 | + return []Deploy{}, err |
| 124 | + } |
| 125 | + |
| 126 | + req.Header.Add( |
| 127 | + "Authorization", |
| 128 | + fmt.Sprintf("Bearer %s", r.token), |
| 129 | + ) |
| 130 | + resp, err := r.httpClient.Do(req) |
| 131 | + if err != nil { |
| 132 | + return []Deploy{}, err |
| 133 | + } |
| 134 | + |
| 135 | + defer resp.Body.Close() |
| 136 | + var response Response |
| 137 | + json.NewDecoder(resp.Body).Decode(&response) |
| 138 | + |
| 139 | + var deploys []Deploy |
| 140 | + for _, responseItem := range response { |
| 141 | + deploys = append(deploys, responseItem.Deploy) |
| 142 | + } |
| 143 | + return deploys, nil |
| 144 | +} |
| 145 | + |
0 commit comments