// genericAPIRequest performs HTTP POST to an IAM API endpoint.
491454
func (cl *Client) genericAPIRequest(c context.Context, base *url.URL, resource, action string, body, resp interface{}) error {
	query, err := url.Parse(fmt.Sprintf("v1/%s:%s?alt=json", resource, action))
	if err != nil {
		return err
	}
	endpoint := base.ResolveReference(query)

	// Serialize the body.
	var reader io.Reader
	if body != nil {
		blob, err := json.Marshal(body)
		if err != nil {
			return err
		}
		reader = bytes.NewReader(blob)
	}

	// Issue the request
	req, err := http.NewRequest("POST", endpoint.String(), reader)
	if err != nil {
		return err
	}
	if reader != nil {
		req.Header.Set("Content-Type", "application/json")
	}

	// Send and handle errors. This is roughly how google-api-go-client calls
	// methods. CheckResponse returns *googleapi.Error.
	logging.Debugf(c, "POST %s", endpoint)
	res, err := ctxhttp.Do(c, cl.Client, req)
	if err != nil {
		return err
	}
	defer googleapi.CloseBody(res)
	if err := googleapi.CheckResponse(res); err != nil {
		logging.WithError(err).Errorf(c, "POST %s failed", endpoint)
		return err
	}
	return json.NewDecoder(res.Body).Decode(resp)
}
1115034
func IsTooManyRequests(err error) bool {
	if ReasonForError(err) == metav1.StatusReasonTooManyRequests {
		return true
	}
	switch t := err.(type) {
	case APIStatus:
		return t.Status().Code == http.StatusTooManyRequests
	}
	return false
}
65556
func (bot *BotAPI) makeMessageRequest(endpoint string, params url.Values) (Message, error) {
	resp, err := bot.MakeRequest(endpoint, params)
	if err != nil {
		return Message{}, err
	}

	var message Message
	json.Unmarshal(resp.Result, &message)

	bot.debugLog(endpoint, params, message)

	return message, nil
}
333223
func httpParamsToArgs(rpcFunc *RPCFunc, r *http.Request) ([]reflect.Value, error) {
	values := make([]reflect.Value, len(rpcFunc.args))

	for i, name := range rpcFunc.argNames {
		argType := rpcFunc.args[i]

		values[i] = reflect.Zero(argType) // set default for that type

		arg := GetParam(r, name)
		// log.Notice("param to arg", "argType", argType, "name", name, "arg", arg)

		if "" == arg {
			continue
		}

		v, err, ok := nonJSONToArg(argType, arg)
		if err != nil {
			return nil, err
		}
		if ok {
			values[i] = v
			continue
		}

		values[i], err = jsonStringToArg(argType, arg)
		if err != nil {
			return nil, err
		}
	}

	return values, nil
}
377615
func (m *MockLambdaAPI) InvokeAsyncRequest(arg0 *lambda.InvokeAsyncInput) (*request.Request, *lambda.InvokeAsyncOutput) {
	ret := m.ctrl.Call(m, "InvokeAsyncRequest", arg0)
	ret0, _ := ret[0].(*request.Request)
	ret1, _ := ret[1].(*lambda.InvokeAsyncOutput)
	return ret0, ret1
}
437262
func NewInterfaceAPI(client *Client) *InterfaceAPI {
	return &InterfaceAPI{
		&baseAPI{
			client: client,
			FuncGetResourceURL: func() string {
				return "interface"
			},
		},
	}
}
230336
func RequestObjectMapper() MiddlewareFunc {
	return func(b *Bubble) error {
		argIdx := -1
		var argT reflect.Type
		for idx, arg := range b.Arguments {
			if arg.IsValid() {
				// already injected
				continue
			}
			if b.ArgumentTypes[idx].Kind() != reflect.Ptr || b.ArgumentTypes[idx].Elem().Kind() != reflect.Struct {
				// only support for struct
				continue
			}
			argT = b.ArgumentTypes[idx]
			argIdx = idx
			break
		}

		if argT == nil {
			return b.Next()
		}

		reqV := reflect.New(argT.Elem())
		req := reqV.Interface()

		// NOTE value will be overwritten by below process
		// url path extract
		if v := b.Context.Value(PathParameterKey); v != nil {
			params, ok := v.(map[string]string)
			if !ok {
				return ErrInvalidPathParameterType
			}
			for key, value := range params {
				found, _ := valueStringMapper(reqV, key, value)
				if !found {
					return ErrPathParameterFieldMissing
				}
			}
		}

		// url get parameter
		for key, ss := range b.R.URL.Query() {
			_, err := valueStringSliceMapper(reqV, key, ss)
			if err != nil {
				return err
			}
		}

		// request body as JSON
		{
			// where is the spec???
			ct := strings.Split(b.R.Header.Get("Content-Type"), ";")
			// TODO check charset
			if ct[0] == "application/json" {
				var body []byte
				var err error
				if b.R.Body != nil {
					// this case occured in unit test
					defer b.R.Body.Close()
					body, err = ioutil.ReadAll(b.R.Body)
				}
				if err != nil {
					return err
				}

				if len(body) == 2 {
					// dirty hack. {} map to []interface or [] map to normal struct.
				} else if len(body) != 0 {
					err := json.Unmarshal(body, req)
					if err != nil {
						return newBadRequestf(err.Error())
					}
				}

			} else if ct[0] == "application/x-www-form-urlencoded" {
				err := b.R.ParseForm()
				if err != nil {
					return err
				}

				for key, ss := range b.R.Form {
					_, err := valueStringSliceMapper(reqV, key, ss)
					if err != nil {
						return err
					}
				}
			}
		}
		// NOTE need request body as a=b&c=d style parsing?

		b.Arguments[argIdx] = reqV

		return b.Next()
	}
}
1204413
func (bow *Browser) httpRequest(req *http.Request) error {
	if bow.client == nil {
		bow.client = bow.buildClient()
	}
	bow.preSend()
	resp, err := bow.client.Do(req)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	var reader io.Reader
	switch resp.Header.Get("Content-Encoding") {
	case "gzip":
		reader, err = gzip.NewReader(resp.Body)
		if err != nil {
			return err
		}
	case "deflate":
		reader = flate.NewReader(resp.Body)

	default:
		reader = resp.Body
	}

	bow.body, err = ioutil.ReadAll(reader)
	if err != nil {
		return err
	}

	buff := bytes.NewBuffer(bow.body)
	dom, err := goquery.NewDocumentFromReader(buff)
	if err != nil {
		return err
	}

	bow.history.Push(bow.state)
	bow.state = jar.NewHistoryState(req, resp, dom)
	bow.postSend()

	return nil
}
1248085
func (cl *Client) iamAPIRequest(c context.Context, resource, action string, body, resp interface{}) error {
	if cl.BasePath != "" {
		// We are in "testing"
		base, err := url.Parse(cl.BasePath)
		if err != nil {
			return err
		}
		return cl.genericAPIRequest(c, base, resource, action, body, resp)
	}
	return cl.genericAPIRequest(c, DefaultIamBaseURL, resource, action, body, resp)
}
439512
func simpleRequest(method, url_ string) (*http.Request, error) {
	req, err := http.NewRequest(method, url_, nil)
	if err != nil {
		return nil, err
	}
	req.Header.Set("x-goog-api-version", "2")
	return req, err
}
1072612
func (fhc *FakeHttpClient) DoRawHttpRequest(path string, requestType string, requestBody *bytes.Buffer) ([]byte, int, error) {
	fhc.DoRawHttpRequestPath = path
	fhc.DoRawHttpRequestRequestType = requestType
	fhc.DoRawHttpRequestRequestBody = requestBody

	return fhc.processResponse()
}
31222
func Generic(n int, edge FilterFunc) *Virtual {
	switch {
	case n < 0:
		return nil
	case n == 0:
		return null
	case n == 1:
		return singleton()
	case edge == nil:
		return Kn(n)
	}
	return generic0(n, edge)
}
578671
func (cli *OpsGenieClient) OpsGenieAPIUrl() string {
	if cli.opsGenieAPIURL == "" {
		cli.opsGenieAPIURL = endpointURL
	}
	return cli.opsGenieAPIURL
}
630603
func (p Protos) makeRequest(req *http.Request) ([]byte, error) {
	req.Header.Set("Appid", p.AppID)

	resp, err := p.HTTPclient.Do(req)
	if err != nil {
		return []byte{}, err
	}
	defer resp.Body.Close()

	payload, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return []byte{}, err
	}

	if resp.StatusCode != 200 {
		httperr := httpErr{}
		err := json.Unmarshal(payload, &httperr)
		if err != nil {
			return []byte{}, fmt.Errorf("Failed to decode error message from Protos: %s", err.Error())
		}
		return []byte{}, errors.New(httperr.Error)
	}

	return payload, nil
}
727871
func validateRequest(r *http.Request) (int, error) {
	if r.Method == http.MethodPut || r.Method == http.MethodDelete {
		return http.StatusMethodNotAllowed, errors.New("method not allowed")
	}
	if r.ContentLength > maxRequestContentLength {
		err := fmt.Errorf("content length too large (%d>%d)", r.ContentLength, maxRequestContentLength)
		return http.StatusRequestEntityTooLarge, err
	}
	// Allow OPTIONS (regardless of content-type)
	if r.Method == http.MethodOptions {
		return 0, nil
	}
	// Check content-type
	if mt, _, err := mime.ParseMediaType(r.Header.Get("content-type")); err == nil {
		for _, accepted := range acceptedContentTypes {
			if accepted == mt {
				return 0, nil
			}
		}
	}
	// Invalid content-type
	err := fmt.Errorf("invalid content type, only %s is supported", contentType)
	return http.StatusUnsupportedMediaType, err
}
225312
func (c *Client) DoAPIRequestWithAuth(remote string, req *http.Request) (*http.Response, error) {
	operation := getReqOperation(req)
	apiEndpoint := c.Endpoints.Endpoint(operation, remote)
	access := c.Endpoints.AccessFor(apiEndpoint.Url)
	return c.DoWithAuth(remote, access, req)
}
716140
func (ea *Config) MakeAPIRequestRaw(method, fullURL string) (*[]byte, error) {
	if ea.httpClient == nil {
		ea.httpClient = &http.Client{}
	}

	if ea.BaseURI == "" {
		ea.BaseURI = defaultBase
	}

	newReq, err := http.NewRequest(method, fullURL, nil)
	if err != nil {
		return nil, err
	}
	newReq.Header.Add("api-version", "1.0")
	newReq.Header.Add("authorization", fmt.Sprintf("bearer %s", ea.APIKey))

	resp, err := ea.httpClient.Do(newReq)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	if resp.StatusCode != 200 {
		return nil, fmt.Errorf("non-200 reponse code received (%d)", resp.StatusCode)
	}

	out, err := ioutil.ReadAll(resp.Body)
	return &out, err
}