// parse request and return limit
165649
func (p *Paginator) ParseLimit(req *restful.Request) int {
	limit := p.LimitDefault
	if p := req.QueryParameter(p.LimitName); p != "" {
		if val, err := strconv.Atoi(p); err == nil {
			limit = val
		}
	}
	if limit < 0 {
		limit = p.LimitDefault
	}
	if limit > p.LimitMax {
		limit = p.LimitMax
	}
	return limit
}
1266473
func (c *Client) LimitedRequest(path string, results int) (string, int, error) {
	res, reqErr := c.Request("GET", path+"?limit="+strconv.Itoa(results), nil)

	if reqErr != nil && !IsErrAPIMismatch(reqErr) {
		return "", -1, reqErr
	}

	defer res.Body.Close()

	body, err := ioutil.ReadAll(res.Body)

	if err != nil {
		return "", -1, err
	}

	r := make(map[string]interface{})
	if err = json.Unmarshal([]byte(body), &r); err != nil {
		return "", -1, err
	}

	out, err := json.Marshal(r["results"].([]interface{}))

	if err != nil {
		return "", -1, err
	}

	return string(out), int(r["count"].(float64)), reqErr
}
862143
func (s *Salesforce) queryLimits() (*http.Response, error) {
	endpoint := fmt.Sprintf("%s://%s/services/data/v%s/limits", s.ServerURL.Scheme, s.ServerURL.Host, s.Version)
	req, err := http.NewRequest(http.MethodGet, endpoint, nil)
	if err != nil {
		return nil, err
	}
	req.Header.Add("Accept", "encoding/json")
	req.Header.Add("Authorization", "Bearer "+s.SessionID)
	return s.client.Do(req)
}
988743
func queryLimit(query url.Values, name string, def int) (int, error) {
	str := query.Get(name)
	if str == "" {
		return def, nil
	}
	limit, err := strconv.Atoi(str)
	if err != nil {
		return 0, trace.BadParameter("failed to parse %v as limit: %v", name, str)
	}
	return limit, nil
}
707291
func (opts *ContainersOpts) parse() (url.Values, Headers) {
	v := url.Values{}
	var h Headers
	if opts != nil {
		if opts.Limit > 0 {
			v.Set("limit", strconv.Itoa(opts.Limit))
		}
		if opts.Prefix != "" {
			v.Set("prefix", opts.Prefix)
		}
		if opts.Marker != "" {
			v.Set("marker", opts.Marker)
		}
		if opts.EndMarker != "" {
			v.Set("end_marker", opts.EndMarker)
		}
		h = opts.Headers
	}
	return v, h
}
862652
func (rl *RateLimiter) Limit() bool {
	// Calculate the number of ns that have passed since our last call
	now := unixNano()
	passed := now - atomic.SwapUint64(&rl.lastCheck, now)

	// Add them to our allowance
	rate := atomic.LoadUint64(&rl.rate)
	current := atomic.AddUint64(&rl.allowance, passed*rate)

	// Ensure our allowance is not over maximum
	if max := atomic.LoadUint64(&rl.max); current > max {
		atomic.AddUint64(&rl.allowance, max-current)
		current = max
	}

	// If our allowance is less than one unit, rate-limit!
	if current < rl.unit {
		return true
	}

	// Not limited, subtract a unit
	atomic.AddUint64(&rl.allowance, -rl.unit)
	return false
}
1251068
func (c *Client) Parse(url string) (*Result, error) {
	header := make(map[string]string)
	bytes, err := c.session.Get(url, header)
	if err != nil {
		return nil, err
	}

	rssType, err := c.detector.detect(bytes)
	if err != nil {
		return nil, err
	}

	parser, err := getParser(rssType)
	if err != nil {
		return nil, err
	}

	mappableFeed, err := parser.Parse(bytes)
	if err != nil {
		return nil, err
	}

	feedBytes, err := mappableFeed.ToJSON()
	if err != nil {
		return nil, err
	}

	feed := Feed{}
	err = feed.Map(feedBytes)
	if err != nil {
		return nil, err
	}

	ret := makeResult(rssType, feed, mappableFeed)
	return &ret, nil
}
1309848
func ParseRequest(req *http.Request) Group {
	if req == nil {
		return nil
	}

	return ParseHeader(req.Header)
}
718859
func (s *SelectQuery) Limit(limit int64) *SelectQuery {
	s.limit = limit
	return s
}
278074
func readResponse(l *Limits, re *http.Response, err error) (*http.Response, error) {
	if re == nil || re.Body == nil {
		return nil, err
	}
	// Closing a response body releases connection to the pool in transport
	body := re.Body
	defer body.Close()
	newBody, err := netutils.NewBodyBufferWithOptions(body, netutils.BodyBufferOptions{
		MemBufferBytes: l.MaxMemBodyBytes,
		MaxSizeBytes:   l.MaxBodyBytes,
	})
	if err != nil {
		return nil, err
	}
	re.Body = newBody
	return re, nil
}
1215667
func (c Client) LimitedRequest(path string, results int) (string, int, error) {
	body, err := c.BasicRequest("GET", path+"?page_size="+strconv.Itoa(results), nil)

	if err != nil {
		return "", -1, err
	}

	res := make(map[string]interface{})
	if err = json.Unmarshal([]byte(body), &res); err != nil {
		return "", -1, err
	}

	out, err := json.Marshal(res["results"].([]interface{}))

	if err != nil {
		return "", -1, err
	}

	return string(out), int(res["count"].(float64)), nil
}
1003306
func parseValidity(r *http.Request) int64 {
	validityString := r.FormValue("validity")
	var validity int64
	if validityString == "" {
		validity = -1
	} else {
		var err error
		validity, err = strconv.ParseInt(validityString, 10, 64)
		if err != nil {
			log.Debugf("Failed to parse validity argument (%v) as int64", validityString)
			validity = -1
		}
	}
	return validity
}
786641
func (s *HTTPServer) parse(resp http.ResponseWriter, req *http.Request, dc *string, b *structs.QueryOptions) bool {
	return s.parseInternal(resp, req, dc, b, true)
}
1054043
func LimitByRequest(limiter *config.Limiter, r engine.Request) *echo.HTTPError {
	sliceKeys := BuildKeys(limiter, r)

	// Loop sliceKeys and check if one of them has error.
	for _, keys := range sliceKeys {
		httpError := LimitByKeys(limiter, keys)
		if httpError != nil {
			return httpError
		}
	}

	return nil
}
1083116
func ParseRequest(r *http.Request) ([]*Request, bool, *Error) {

	var rerr *Error

	if !strings.HasPrefix(r.Header.Get(contentTypeKey), contentTypeValue) {
		return nil, false, ErrInvalidRequest()
	}

	buf := bytes.NewBuffer(make([]byte, 0, r.ContentLength))
	if _, err := buf.ReadFrom(r.Body); err != nil {
		return nil, false, ErrInvalidRequest()
	}
	defer func(r *http.Request) {
		err := r.Body.Close()
		if err != nil {
			rerr = ErrInternal()
		}
	}(r)

	if buf.Len() == 0 {
		return nil, false, ErrInvalidRequest()
	}

	f, _, err := buf.ReadRune()
	if err != nil {
		return nil, false, ErrInvalidRequest()
	}
	if err := buf.UnreadRune(); err != nil {
		return nil, false, ErrInvalidRequest()
	}

	var rs []*Request
	if f != batchRequestKey {
		var req *Request
		if err := fastjson.NewDecoder(buf).Decode(&req); err != nil {
			return nil, false, ErrParse()
		}
		return append(rs, req), false, nil
	}

	if err := fastjson.NewDecoder(buf).Decode(&rs); err != nil {
		return nil, false, ErrParse()
	}

	return rs, true, rerr
}
350512
func (c *Connection) Limit(limit int) *Query {
	return Q(c).Limit(limit)
}
51931
func (opts *ObjectsOpts) parse() (url.Values, Headers) {
	v := url.Values{}
	var h Headers
	if opts != nil {
		if opts.Limit > 0 {
			v.Set("limit", strconv.Itoa(opts.Limit))
		}
		if opts.Marker != "" {
			v.Set("marker", opts.Marker)
		}
		if opts.EndMarker != "" {
			v.Set("end_marker", opts.EndMarker)
		}
		if opts.Prefix != "" {
			v.Set("prefix", opts.Prefix)
		}
		if opts.Path != "" {
			v.Set("path", opts.Path)
		}
		if opts.Delimiter != 0 {
			v.Set("delimiter", string(opts.Delimiter))
		}
		h = opts.Headers
	}
	return v, h
}