Signs requests before they are sent

@param Event $event Event emitted
322408
public function onRequestBeforeSend(Event $event) {
        if ($this->token != null) {
            $event['request']->setHeader('x-cos-security-token', $this->token);
        }
/*
        if(!$this->credentials instanceof NullCredentials) {
            $this->signature->signRequest($event['request'], $this->credentials);
        }
*/
    }
1360305
public function onRequestSuccess(Event $event)
    {
        /** @var Response $response */
        $response = $event['response'];

        if (!$response->hasHeader('X-RateLimit-Limit')) {
            $this->rateLimitEnabled = false;

            return;
        }

        $this->rateLimitMax = (string) $response->getHeader('X-RateLimit-Limit');

        if ($response->hasHeader('X-RateLimit-Remaining')) {
            $this->rateLimitRemaining = (string) $response->getHeader('X-RateLimit-Remaining');
        }

        if ($response->hasHeader('X-RateLimit-Reset')) {
            $this->rateLimitReset = (string) $response->getHeader('X-RateLimit-Reset');
        }

        // Prevent division by zero
        if ($this->rateLimitMax == 0) {
            $this->rateLimitMax = 1;
        }
    }
478041
public function beforeRender(Event $event)
    {
        $this->httpStatusCode = $this->response->getStatusCode();

        $messageArr = $this->response->withStatus($this->httpStatusCode);

        if (Configure::read('ApiRequest.debug') && isset($this->viewVars['error'])) {
            $this->apiResponse[$this->responseFormat['messageKey']] = $this->viewVars['error']->getMessage();
        } else {
            $this->apiResponse[$this->responseFormat['messageKey']] = !empty($messageArr[$this->httpStatusCode]) ? $messageArr[$this->httpStatusCode] : 'Unknown error!';
        }

        Configure::write('apiExceptionMessage', isset($this->viewVars['error']) ? $this->viewVars['error']->getMessage() : null);

        parent::beforeRender($event);

        $this->viewBuilder()->setClassName('RestApi.ApiError');

        return null;
    }
743295
public function authorizeRequest(Event $event)
    {
        /* @var \Guzzle\Service\Command\CommandInterface $command */
        $command = $event['command'];

        $request = $command->getRequest();
        $request->setAuth($this->apiKey);
    }
1188908
public function beforeSend()
    {
        $this->client->beforeSend($this);

        $event = new RequestEvent();
        $event->request = $this;
        $this->trigger(self::EVENT_BEFORE_SEND, $event);
    }
479954
public function beforePrepare(Event $event)
	{
		$command = $event['command'];
		$operation = $command->getOperation();

		if ($command->getName() !== 'updateEntity' && $command->offsetExists('_id') === true) return;

		$operation->addParam(new Parameter(array(
			'name' => 'body_id',
			'location' => 'json',
			'type' => 'string',
			'default' => $command->offsetGet('_id'),
			'static' => true,
			'sentAs' => '_id'
		)));
	}
779540
public function onRequestComplete(Event $event)
	{
		if ($body = $event['response']->getBody()) {
			if (substr($body, 0, 3) === "\xef\xbb\xbf") {
				// UTF-8
				$event['response']->setBody(substr($body, 3));
			} else if (substr($body, 0, 4) === "\xff\xfe\x00\x00" ||
					   substr($body, 0, 4) === "\x00\x00\xfe\xff"
			) {
				// UTF-32
				$event['response']->setBody(substr($body, 4));
			} else if (substr($body, 0, 2) === "\xff\xfe" ||
					   substr($body, 0, 2) === "\xfe\xff"
			) {
				// UTF-16
				$event['response']->setBody(substr($body, 2));
			}
		}
	}
1116964
private function getBeforeEvent(BeforeEvent $event)
    {
        $request = $event->getRequest();

        if ('api.embed.ly' === $request->getHost()) {
            $requestQuery = $request->getQuery();

            $requestQuery->setEncodingType(false);
            $requestQuery->set('key', $this->apiKey);

            $request->setQuery($requestQuery);
        }
    }
256849
public function beforeRender(Event $event)
    {
        $entity = null;
        if (isset($event->getSubject()->entity)) {
            $entity = $event->getSubject()->entity;
        }
        $this->publishRelatedModels(null, $entity);
    }
44692
public function beforeApiRequestSend($event)
    {
        $accessToken = $this->getAccessToken();
        if (!is_object($accessToken) || !$accessToken->getIsValid()) {
            throw new Exception('Invalid access token.');
        }

        $this->applyAccessTokenToRequest($event->request, $accessToken);
    }
895925
public function onRequestSent(Event $event)
    {
        $request = $event['request'];
        $response = $event['response'];
        $handle = $event['handle'];

        if ($wire = $request->getParams()->get('request_wire')) {
            $request = clone $request;
            $request->setBody($wire);
        }

        if ($wire = $request->getParams()->get('response_wire')) {
            $response = clone $response;
            $response->setBody($wire);
        }

        // Send the log message to the adapter, adding a category and host
        $priority = $response && $response->isError() ? LOG_ERR : LOG_DEBUG;
        $message = $this->formatter->format($request, $response, $handle);
        $this->logAdapter->log($message, $priority, array(
            'request'  => $request,
            'response' => $response,
            'handle'   => $handle
        ));
    }
857105
public function beforeSend($event = null)
    {
        if (isset($this->stream)) {
            return;
        }
        if (isset($this->controller) && $this->task) {
            if ($this->isInstructable) {
                $this->format = static::FORMAT_JSON;
                $this->data = $this->generateInstructions();
            } else {
                $this->handleFlashStatus();
                if ($this->redirect) {
                    $this->redirect($this->redirect);
                } elseif ($this->refresh) {
                    $this->refresh();
                } else {
                    if (is_null($this->content)) {
                        $this->content = $this->renderContent(true);
                    }
                }
            }
        }
    }
1074832
public function afterHandle($event)
    {
        parent::afterHandle($event);
        
        // reset form_token
        if ($this->request->getMethod() === 'POST') {
            
            if ($this->errorInfo && !$this->errorInfo['errcode'] && $this->options['csrfTokenEnabled'] && $this->options['csrfTokenAutoReset']) {
                $this->resetCsrfToken();
            }
        }
    }
1085221
public function beforeFind(Event $event)
    {
        //Inject default query handlers
        $queryParameters = Hash::merge($this->getConfig('queryParameters'), [
            'sort' => [
                'callable' => [$this, '_sortParameter'],
            ],
            'include' => [
                'callable' => [$this, '_includeParameter']
            ],
            'fields' => [
                'callable' => [$this, '_fieldSetsParameter']
            ]
        ]);

        foreach ($queryParameters as $parameter => $options) {
            if (is_callable($options)) {
                $options = [
                    'callable' => $options
                ];
            }

            if (!is_callable($options['callable'])) {
                throw new \InvalidArgumentException('Invalid callable supplied for query parameter ' . $parameter);
            }

            $options['callable']($this->_request()->getQuery($parameter), $event->getSubject(), $options);
        }
    }
845048
public function beforeRender(Event $event)
    {
        if (!Configure::read('debug')) {
            return;
        }

        $this->_action()->setConfig('serialize.queryLog', 'queryLog');
        $this->_controller()->set('queryLog', $this->_getQueryLogs());
    }
750795
private function captureStepEvent(Event $event)
    {
        if ($event instanceof AfterStepTested) {
            $this->afterStepTestedEvents[$event->getStep()->getLine()] = $event;
        }
        if ($event instanceof AfterStepSetup) {
            $this->afterStepSetupEvents[$event->getStep()->getLine()] = $event;
        }
    }
502759
public function onFind($event)
    {
        if ($this->isAttributeWriteable($event) && $this->onBeforeFind($event)) {
            if ($this->i18n) {
                $event->sender->setAttribute($this->name, $this->i18nDecodedGetActive($this->i18nFieldDecode($event->sender->getAttribute($this->name), $this->i18nEmptyValue)));
            }
            
            $this->onAfterFind($event);
        }
    }