MCPcopy
hub / github.com/spacecloud-io/space-cloud / Get

Function Get

space-cli/cmd/utils/http.go:12–54  ·  view source on GitHub ↗

Get gets spec object

(method, url string, params map[string]string, vPtr interface{})

Source from the content-addressed store, hash-verified

10
11// Get gets spec object
12func Get(method, url string, params map[string]string, vPtr interface{}) error {
13 account, token, err := LoginWithSelectedAccount()
14 if err != nil {
15 return LogError("Couldn't get account details or login token", err)
16 }
17 url = fmt.Sprintf("%s%s", account.ServerURL, url)
18
19 req, err := http.NewRequest(method, url, nil)
20 if err != nil {
21 return err
22 }
23 if token != "" {
24 req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
25 }
26 q := req.URL.Query()
27 for k, v := range params {
28 q.Add(k, v)
29 }
30 req.URL.RawQuery = q.Encode()
31 resp, err := http.DefaultClient.Do(req)
32 if err != nil {
33 return err
34 }
35
36 defer CloseTheCloser(resp.Body)
37
38 data, _ := ioutil.ReadAll(resp.Body)
39
40 if resp.StatusCode != 200 {
41 respBody := map[string]interface{}{}
42 if err := json.Unmarshal(data, &respBody); err != nil {
43 return err
44 }
45 _ = LogError(fmt.Sprintf("error while getting service got http status code %s - %s", resp.Status, respBody["error"]), nil)
46 return fmt.Errorf("received invalid status code (%d)", resp.StatusCode)
47 }
48
49 if err := json.Unmarshal(data, vPtr); err != nil {
50 return err
51 }
52
53 return nil
54}
55
56// CloseTheCloser closes the closer
57func CloseTheCloser(c io.Closer) {

Callers 1

GetProjectsFromSCFunction · 0.85

Calls 6

LoginWithSelectedAccountFunction · 0.85
LogErrorFunction · 0.85
AddMethod · 0.80
CloseTheCloserFunction · 0.70
DoMethod · 0.65
UnmarshalMethod · 0.45

Tested by

no test coverage detected