func()

in vidispine/VSRequestor.go [81:124]


func (r *VSRequestor) Do(method string, subpath string, accept string, bodyContentType string, body io.Reader) (io.ReadCloser, error) {
	urlToCall, url_err := r.url_to_call(subpath)
	if url_err != nil {
		return nil, url_err
	}

	log.Printf("Performing %s to %s", method, urlToCall.String())

	req, _ := http.NewRequest(method, urlToCall.String(), body)
	req.Header.Add("Authorization", r.auth)
	req.Header.Add("Accept", accept)
	if bodyContentType != "" {
		req.Header.Add("Content-Type", bodyContentType)
	}
	resp, err := r.client.Do(req)

	if err != nil {
		return nil, err
	}

	if resp.StatusCode == 200 {
		return resp.Body, nil
	}

	bodyContent, readErr := ioutil.ReadAll(resp.Body)
	if readErr != nil {
		return nil, errors.New(fmt.Sprintf("Server returned a %d but could not read the result because of %s", resp.StatusCode, readErr))
	}

	switch resp.StatusCode {
	case 400:
		fallthrough
	case 500:
		return nil, errors.New(string(bodyContent))
	case 503:
		fallthrough
	case 504:
		log.Print("Server is not responding, retrying after a few seconds...")
		time.Sleep(5 * time.Second)
		return r.Do(method, subpath, accept, bodyContentType, body)
	default:
		return nil, errors.New(fmt.Sprintf("Unexpected error code %d, server said %s", resp.StatusCode, string(bodyContent)))
	}
}