File: restclient.go

package info (click to toggle)
singularity-container 4.0.3%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 21,672 kB
  • sloc: asm: 3,857; sh: 2,125; ansic: 1,677; awk: 414; makefile: 110; python: 99
file content (105 lines) | stat: -rw-r--r-- 3,394 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright (c) 2018-2023, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.

package client

import (
	"bytes"
	"context"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"net/url"

	jsonresp "github.com/sylabs/json-resp"
)

func (c *Client) apiGet(ctx context.Context, path string) (objJSON []byte, err error) {
	c.Logger.Logf("apiGet calling %s", path)
	return c.doGETRequest(ctx, path)
}

func (c *Client) apiCreate(ctx context.Context, url string, o interface{}) (objJSON []byte, err error) {
	c.Logger.Logf("apiCreate calling %s", url)
	return c.doPOSTRequest(ctx, url, o)
}

func (c *Client) apiUpdate(ctx context.Context, url string, o interface{}) (objJSON []byte, err error) {
	c.Logger.Logf("apiUpdate calling %s", url)
	return c.doPUTRequest(ctx, url, o)
}

func (c *Client) doGETRequest(ctx context.Context, path string) (objJSON []byte, err error) {
	return c.commonRequestHandler(ctx, "GET", path, nil, []int{http.StatusOK})
}

func (c *Client) doPUTRequest(ctx context.Context, path string, o interface{}) (objJSON []byte, err error) {
	return c.commonRequestHandler(ctx, "PUT", path, o, []int{http.StatusOK, http.StatusNoContent})
}

func (c *Client) doPOSTRequest(ctx context.Context, path string, o interface{}) (objJSON []byte, err error) {
	return c.commonRequestHandler(ctx, "POST", path, o, []int{http.StatusOK, http.StatusCreated})
}

func (c *Client) doDeleteRequest(ctx context.Context, path string) (objJSON []byte, err error) {
	return c.commonRequestHandler(ctx, "DELETE", path, nil, []int{http.StatusOK})
}

func (c *Client) commonRequestHandler(ctx context.Context, method string, path string, o interface{}, acceptedStatusCodes []int) (objJSON []byte, err error) {
	var payload io.Reader

	// only PUT and POST methods
	if method != "GET" && method != "DELETE" {
		s, err := json.Marshal(o)
		if err != nil {
			return []byte{}, fmt.Errorf("error encoding object to JSON:\n\t%w", err)
		}
		payload = bytes.NewBuffer(s)
	}

	// split url containing query into component pieces (path and raw query)
	u, err := url.Parse(path)
	if err != nil {
		return []byte{}, fmt.Errorf("error parsing url:\n\t%w", err)
	}

	req, err := c.newRequest(ctx, method, u.Path, u.RawQuery, payload)
	if err != nil {
		return []byte{}, fmt.Errorf("error creating %s request:\n\t%w", method, err)
	}

	res, err := c.HTTPClient.Do(req)
	if err != nil {
		return []byte{}, fmt.Errorf("error making request to server:\n\t%w", err)
	}
	defer res.Body.Close()

	// check http status code
	if res.StatusCode == http.StatusNotFound {
		return []byte{}, ErrNotFound
	}
	if !isValidStatusCode(res.StatusCode, acceptedStatusCodes) {
		err := jsonresp.ReadError(res.Body)
		if err != nil {
			return []byte{}, fmt.Errorf("request did not succeed: %w", err)
		}
		return []byte{}, fmt.Errorf("%w: request did not succeed: http status code: %d", errHTTP, res.StatusCode)
	}
	objJSON, err = io.ReadAll(res.Body)
	if err != nil {
		return []byte{}, fmt.Errorf("error reading response from server:\n\t%w", err)
	}
	return objJSON, nil
}

func isValidStatusCode(statusCode int, acceptedStatusCodes []int) bool {
	for _, value := range acceptedStatusCodes {
		if value == statusCode {
			return true
		}
	}
	return false
}