Skip to content

Commit

Permalink
Fix replace variables in request body
Browse files Browse the repository at this point in the history
  • Loading branch information
caleeli committed May 26, 2021
1 parent c6a9866 commit 1d71159
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 8 deletions.
30 changes: 30 additions & 0 deletions ProcessMaker/Http/Requests/RecordRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace ProcessMaker\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class RecordRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return false;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
//
];
}
}
91 changes: 83 additions & 8 deletions ProcessMaker/Traits/MakeHttpRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

namespace ProcessMaker\Traits;

use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Facades\Log;
use Mustache_Engine;
use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Illuminate\Support\Arr;
use Mustache_Engine;
use ProcessMaker\Exception\HttpResponseException;
use ProcessMaker\Models\FormalExpression;
use Psr\Http\Message\ResponseInterface;

trait MakeHttpRequests
Expand Down Expand Up @@ -52,13 +53,12 @@ private function getMustache() {
public function request(array $data = [], array $config = [])
{
try {
$request = $this->prepareRequest($data, $config);

// if using the new version of data connectors
if (array_key_exists('outboundConfig', $config)) {
if (array_key_exists('outboundConfig', $config) || array_key_exists('dataMapping', $config)) {
$request = $this->prepareRequestWithOutboundConfig($data, $config);
return $this->responseWithHeaderData($this->call(...$request), $data, $config);
}
else {
} else {
$request = $this->prepareRequest($data, $config);
return $this->response($this->call(...$request), $data, $config);
}
} catch (ClientException $exception) {
Expand Down Expand Up @@ -97,6 +97,81 @@ private function prepareRequest(array &$data, array &$config)
return $request;
}

/**
* Prepare data to be used in body (mustache)
*
* @param array $requestData
* @param array $outboundConfig
* @param string $type PARAM HEADER BODY
*
* @return array
*/
private function prepareData(array $requestData, array $outboundConfig, $type)
{
$data = $requestData;
foreach ($outboundConfig as $outbound) {
if ($outbound['type'] === $type) {
$data[$outbound['key']] = $this->evalExpression($outbound['value'], $requestData);
}
}
return $data;
}

/**
* Evaluate a BPMN expression
* ex.
* foo
* _request.id
* _user.id
* {{ form.age }}
* "{{ form.lastname }} {{ form.firstname }}"
*
* @return mixed
*/
private function evalExpression($expression, array $data)
{
try {
$formal = new FormalExpression();
$formal->setBody($expression);
return $formal($data);
} catch (Exception $exception) {
return "{$expression}: " . $exception->getMessage();
}
}

/**
* Prepares data for the http request replacing mustache with pm instance and OutboundConfig
*
* @param array $data, request data
* @param array $config, datasource configuration
*
* @return array
*/
private function prepareRequestWithOutboundConfig(array $requestData, array &$config)
{
$data = $this->prepareData($requestData, $config['outboundConfig'], 'PARAM');
$endpoint = $this->endpoints[$config['endpoint']];
$method = $this->getMustache()->render($endpoint['method'], $data);

$url = $this->addQueryStringsParamsToUrl($endpoint, $config, $data);

$this->verifySsl = array_key_exists('verify_certificate', $this->credentials)
? $this->credentials['verify_certificate']
: true;

$data = $this->prepareData($requestData, $config['outboundConfig'], 'HEADER');
$headers = $this->addHeaders($endpoint, $config, $data);
$data = $this->prepareData($requestData, $config['outboundConfig'], 'BODY');
$body = $this->getMustache()->render($endpoint['body'], $data);
$bodyType = null;
if (isset($endpoint['body_type'])) {
$bodyType = $this->getMustache()->render($endpoint['body_type'], $data);
}
$request = [$method, $url, $headers, $body, $bodyType];
$request = $this->addAuthorizationHeaders(...$request);
return $request;
}

/**
* Add authorization parameters
*
Expand Down

0 comments on commit 1d71159

Please sign in to comment.