ChangeApplicationPlan changes the plan of an application
(accessToken, appID, plan string)
| 465 | |
| 466 | // ChangeApplicationPlan changes the plan of an application |
| 467 | func (c *Client) ChangeApplicationPlan(accessToken, appID, plan string) (*Application, error) { |
| 468 | payload := ChangePlanRequest{Plan: plan} |
| 469 | body, err := json.Marshal(payload) |
| 470 | if err != nil { |
| 471 | return nil, err |
| 472 | } |
| 473 | |
| 474 | endpoint := fmt.Sprintf( |
| 475 | "%s/1/applications/%s/plan/self-serve", |
| 476 | c.APIURL, |
| 477 | url.PathEscape(appID), |
| 478 | ) |
| 479 | req, err := http.NewRequest(http.MethodPatch, endpoint, bytes.NewReader(body)) |
| 480 | if err != nil { |
| 481 | return nil, err |
| 482 | } |
| 483 | c.setAPIHeaders(req, accessToken) |
| 484 | req.Header.Set("Content-Type", "application/json") |
| 485 | |
| 486 | resp, err := c.client.Do(req) |
| 487 | if err != nil { |
| 488 | return nil, fmt.Errorf("Couldn't change your application's plan: %w", err) |
| 489 | } |
| 490 | defer resp.Body.Close() |
| 491 | |
| 492 | if resp.StatusCode == http.StatusUnauthorized { |
| 493 | return nil, ErrSessionExpired |
| 494 | } |
| 495 | if resp.StatusCode < 200 || resp.StatusCode >= 300 { |
| 496 | return nil, fmt.Errorf("Couldn't change your application's plan: %d", resp.StatusCode) |
| 497 | } |
| 498 | |
| 499 | respBody, _ := io.ReadAll(resp.Body) |
| 500 | var singleResp SingleApplicationResponse |
| 501 | if err := json.Unmarshal(respBody, &singleResp); err == nil && |
| 502 | singleResp.Data.Attributes.ApplicationID != "" { |
| 503 | app := singleResp.Data.toApplication() |
| 504 | return &app, nil |
| 505 | } |
| 506 | return &Application{ID: appID}, nil |
| 507 | } |
| 508 | |
| 509 | // ListRegions returns the allowed hosting regions for application creation. |
| 510 | func (c *Client) ListRegions(accessToken string) ([]Region, error) { |