Пример #1
0
func (c *clientImpl) do(request *gorequest.SuperAgent) ([]byte, error) {
	res, body, errs := request.EndBytes()
	if errs != nil {
		return nil, fmt.Errorf("failed to create request: GET %q : %v", request.Url, errs)
	}
	defer res.Body.Close()
	if res.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("http error %d GET %q: %q", res.StatusCode, request.Url, string(body))
	}
	return body, nil
}
Пример #2
0
// Does api call with the superagent
func makeCallWithRequest(req *gorequest.SuperAgent, token string) (*string, error) {
	resp, body, errs := req.Param("circle-token", token).Set("accept", "application/json").End()
	if errs != nil {
		var errorMessage string
		for _, err := range errs {
			errorMessage += err.Error() + "\n"
		}
		return nil, errors.New(errorMessage)
	}
	if resp.StatusCode != 200 {
		return nil, errors.New(body)
	}
	return &body, nil
}
Пример #3
0
func (db *Database) GetView(docName string, viewName string, query string) ([]byte, error) {

	log.Debugf("couch : GetView query %s in viewName %s of desDoc %s", query, viewName, docName)
	type ViewResponse struct {
		Error  string `json:"error"`
		Reason string `json:"reason"`
	}

	var body string
	var errs []error
	var prefix string
	var superAgent *gorequest.SuperAgent

	if query == "" {
		prefix = docName + "/_view/" + viewName
		log.Debug("Getting view name " + prefix)
		_, body, errs = db.Req.Get(prefix).End()
	} else {
		values, err := url.ParseQuery(query)
		if err != nil {
			return nil, errors.New("Unable to parse query string: " + query)
		}
		encodedKey := values.Encode()

		prefix = docName + "/_view/" + viewName
		superAgent = db.Req.Get(prefix).Query(encodedKey)
		_, body, errs = superAgent.End()
		log.Debug("Url" + superAgent.Url + encodedKey)
	}

	if len(errs) > 0 {
		return nil, errors.New("Database : Error making request " + fmt.Sprint("%v", errs))
	}
	viewResp := &ViewResponse{}
	err := json.Unmarshal([]byte(body), viewResp)

	if err != nil {
		log.Error(body)
		return nil, err
	}

	if viewResp.Error != "" {
		err = errors.New(viewResp.Error + " " + viewResp.Reason + "\n req " + superAgent.Url)
		return nil, err
	}

	log.Debug("Returning body", body)
	return []byte(body), nil
}
Пример #4
0
// Create a request which is set headers for Parse API
func (s *ParseSession) initRequest(req *gorequest.SuperAgent, useMaster bool) {
	if useMaster {
		req.
			Set(headerAppID, s.client.ApplicationID).
			Set(headerMasterKey, s.client.MasterKey).
			Timeout(s.client.TimeOut)
	} else {
		req.
			Set(headerAppID, s.client.ApplicationID).
			Set(headerAPIKey, s.client.RESTAPIKey).
			Timeout(s.client.TimeOut)
	}

	if s.SessionToken != "" {
		req.Set(headerSessionToken, s.SessionToken)
	}
}
Пример #5
0
func getRequest(job models.Job) *gorequest.SuperAgent {
	var request = gorequest.New()
	var r *gorequest.SuperAgent

	if job.HTTPVerb == gorequest.GET {
		r = request.Get(job.URI)
	}

	if job.HTTPVerb == gorequest.POST {
		r = request.Post(job.URI)
	}
	r.Timeout(time.Second * 20)

	for key, header := range job.Headers {
		r.Set(key, interfaceToString(header))
	}
	return r
}
Пример #6
0
// Execute a parse request
func do(req *gorequest.SuperAgent, data interface{}) error {

	res, body, errs := req.End()
	if errs != nil {
		return fmt.Errorf("%v", errs)
	}
	if res.StatusCode < 200 || res.StatusCode >= 300 {
		// parse as error model
		reserr := new(Error)
		err := json.NewDecoder(strings.NewReader(body)).Decode(reserr)
		if err != nil {
			return err
		}
		return reserr
	}
	if data == nil {
		return nil
	}
	return json.NewDecoder(strings.NewReader(body)).Decode(data)
}
Пример #7
0
func setHeadersFromMock(req *gorequest.SuperAgent, mock Mock) {
	for header, val := range mock.HttpRequest.Headers {
		req.Set(header, val)
	}
}