Ejemplo n.º 1
0
 /**
  * @param RequestForm $model
  * @return ResponseRecord
  */
 protected function send(RequestForm $model)
 {
     $this->module->trigger(Module::EVENT_ON_REQUEST, new RequestEvent(['form' => $model]));
     /** @var \yii\httpclient\Client $client */
     $client = Yii::createObject($this->module->clientConfig);
     $client->baseUrl = $this->module->baseUrl;
     $begin = microtime(true);
     $response = $client->createRequest()->setMethod($model->method)->setUrl($model->getUri())->setData($model->getBodyParams())->setHeaders($model->getHeaders())->send();
     $duration = microtime(true) - $begin;
     $record = new ResponseRecord();
     $record->status = $response->getStatusCode();
     $record->duration = $duration;
     foreach ($response->getHeaders() as $name => $values) {
         $name = str_replace(' ', '-', ucwords(str_replace('-', ' ', $name)));
         $record->headers[$name] = $values;
     }
     $record->content = $response->getContent();
     $this->module->trigger(Module::EVENT_ON_RESPONSE, new ResponseEvent(['form' => $model, 'record' => $record]));
     return $record;
 }
Ejemplo n.º 2
0
 /**
  * @param array $data
  * @return integer number of records that were imported
  */
 public function importCollection($data)
 {
     if (!is_array($data)) {
         throw new InvalidParamException('Data must be an array.');
     }
     // Validate
     /** @var RequestForm[] $requests */
     $requests = [];
     /** @var ResponseRecord[] $responses */
     $responses = [];
     foreach ($data as $tag => $row) {
         if (!preg_match('/^[a-f0-9]+$/', $tag)) {
             throw new InvalidParamException("Tag {$tag} must be a string and contains a-f0-9 symbols only.");
         }
         if (!isset($row['request'], $row['response'])) {
             throw new InvalidParamException("Row {$tag} must contains request and response.");
         }
         $request = new RequestForm();
         $request->setAttributes($row['request']);
         if (!$request->validate()) {
             $errors = $request->getFirstErrors();
             throw new InvalidParamException(reset($errors));
         }
         $requests[$tag] = $request;
         $response = new ResponseRecord();
         try {
             $response->status = $row['response']['status'];
             $response->duration = $row['response']['duration'];
             $response->headers = $row['response']['headers'];
             $response->content = $row['response']['content'];
         } catch (\Exception $e) {
             throw new InvalidParamException($e->getMessage(), $e->getCode(), $e);
         }
         $responses[$tag] = $response;
     }
     // Save
     $count = 0;
     $this->_collection = $this->readCollection();
     foreach ($requests as $tag => $request) {
         if (!$this->exists($tag)) {
             $this->writeData($tag, $request->getAttributes(), get_object_vars($responses[$tag]));
             $this->_collection[$tag] = ['method' => $request->method, 'endpoint' => $request->endpoint, 'description' => $request->description, 'status' => $responses[$tag]->status, 'time' => time()];
             $count++;
         }
     }
     $this->writeCollection($this->_collection);
     return $count;
 }
Ejemplo n.º 3
0
 * @var ActiveForm $form
 */
?>
<div class="rest-request-form">
    <?php 
$form = ActiveForm::begin(['action' => ['create'], 'fieldConfig' => ['labelOptions' => ['class' => 'control-label sr-only']], 'enableClientValidation' => false]);
?>
        <?php 
echo $form->field($model, 'tab', ['template' => '{input}', 'options' => ['class' => '']])->hiddenInput();
?>

        <div class="row">
            <div class="col-sm-2">

                <?php 
echo $form->field($model, 'method', ['options' => ['class' => 'form-group form-group-lg']])->dropDownList(RequestForm::methodLabels());
?>

            </div>
            <div class="col-sm-10">

                <?php 
echo $form->field($model, 'endpoint', ['template' => <<<HTML
                        {label}
                        <div class="input-group">
                            <div class="input-group-addon">{$baseUrl}</div>
                            {input}
                            <span class="input-group-btn">
                                <button class="btn btn-lg btn-primary" type="submit" tabindex="-1">Send</button>
                            </span>
                        </div>
Ejemplo n.º 4
0
 private function compareCollection($row1, $row2)
 {
     $methods = array_keys(RequestForm::methodLabels());
     if ($result = strcmp($row1['endpoint'], $row2['endpoint'])) {
         return $result;
         // 2. Order by endpoints
     } elseif ($result = array_search($row1['method'], $methods) - array_search($row2['method'], $methods)) {
         return $result;
         // 2. Order by methods
     } else {
         return $row1['time'] - $row2['time'];
         // 3. Order by time
     }
 }