MCPcopy Create free account
hub / github.com/algolia/cli / CreateApplication

Method CreateApplication

api/dashboard/client.go:305–352  ·  view source on GitHub ↗

CreateApplication creates a new application for the authenticated user.

(accessToken, region, name string)

Source from the content-addressed store, hash-verified

303
304// CreateApplication creates a new application for the authenticated user.
305func (c *Client) CreateApplication(accessToken, region, name string) (*Application, error) {
306 payload := CreateApplicationRequest{RegionCode: region, Name: name}
307 body, err := json.Marshal(payload)
308 if err != nil {
309 return nil, err
310 }
311
312 req, err := http.NewRequest(http.MethodPost, c.APIURL+"/1/applications", bytes.NewReader(body))
313 if err != nil {
314 return nil, err
315 }
316 c.setAPIHeaders(req, accessToken)
317 req.Header.Set("Content-Type", "application/json")
318
319 resp, err := c.client.Do(req)
320 if err != nil {
321 return nil, fmt.Errorf("create application request failed: %w", err)
322 }
323 defer resp.Body.Close()
324
325 if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
326 respBody, _ := io.ReadAll(resp.Body)
327 respStr := string(respBody)
328
329 if strings.Contains(strings.ToLower(respStr), "cluster") &&
330 strings.Contains(strings.ToLower(respStr), "not available") ||
331 strings.Contains(strings.ToLower(respStr), "no cluster") {
332 return nil, &ErrClusterUnavailable{
333 Region: region,
334 Message: fmt.Sprintf("no cluster available in region %q", region),
335 }
336 }
337
338 return nil, fmt.Errorf(
339 "create application failed with status %d: %s",
340 resp.StatusCode,
341 respStr,
342 )
343 }
344
345 var singleResp SingleApplicationResponse
346 if err := json.NewDecoder(resp.Body).Decode(&singleResp); err != nil {
347 return nil, fmt.Errorf("failed to parse application response: %w", err)
348 }
349
350 app := singleResp.Data.toApplication()
351 return &app, nil
352}
353
354// UpdateApplication renames an existing application.
355func (c *Client) UpdateApplication(accessToken, appID, name string) (*Application, error) {

Callers 2

Calls 5

setAPIHeadersMethod · 0.95
toApplicationMethod · 0.80
ErrorfMethod · 0.65
CloseMethod · 0.65
SetMethod · 0.45

Tested by 1