Skip to content

Commit

Permalink
Merge branch 'csv-formatter-patch-1'
Browse files Browse the repository at this point in the history
  • Loading branch information
Zack Carlson authored and Zack Carlson committed Mar 22, 2024
2 parents 7ab2d80 + faa6e76 commit c062349
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 90 deletions.
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
"require": {
"php": "~7.2 || ~8.0",
"firebase/php-jwt": "^5.2|~6.0",
"illuminate/cache": "~6.0|~7.0|~8.0|~9.0|~10.0",
"illuminate/contracts": "~6.0|~7.0|~8.0|~9.0|~10.0",
"illuminate/config": "~6.0|~7.0|~8.0|~9.0|~10.0",
"illuminate/http": "~6.0|~7.0|~8.0|~9.0|~10.0",
"illuminate/routing": "~6.0|~7.0|~8.0|~9.0|~10.0",
"illuminate/cache": "~6.0|~7.0|~8.0|~9.0|~10.0|~11.0",
"illuminate/contracts": "~6.0|~7.0|~8.0|~9.0|~10.0|~11.0",
"illuminate/config": "~6.0|~7.0|~8.0|~9.0|~10.0|~11.0",
"illuminate/http": "~6.0|~7.0|~8.0|~9.0|~10.0|~11.0",
"illuminate/routing": "~6.0|~7.0|~8.0|~9.0|~10.0|~11.0",
"nesbot/carbon": "^2.0",
"guzzlehttp/guzzle": "~6.0|~7.0"
},
Expand Down
14 changes: 7 additions & 7 deletions src/Omniphx/Forrest/Authentications/OAuthJWT.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,24 @@ public static function getJWT($iss, $aud, $sub, $privateKey)
return JWT::encode($payload, $privateKey, 'RS256');
}

public function authenticate($url = null)
public function authenticate($fullInstanceUrl = null)
{
$domain = $url ?? $this->credentials['loginURL'] . '/services/oauth2/token';
$username = $this->credentials['username'];
// OAuth Client ID
$fullInstanceUrl = $fullInstanceUrl ?? $this->getInstanceURL() . '/services/oauth2/token';

$consumerKey = $this->credentials['consumerKey'];
// Private Key
$loginUrl = $this->credentials['loginURL'];
$username = $this->credentials['username'];
$privateKey = $this->credentials['privateKey'];

// Generate the form parameters
$assertion = static::getJWT($consumerKey, $domain, $username, $privateKey);
$assertion = static::getJWT($consumerKey, $loginUrl, $username, $privateKey);
$parameters = [
'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
'assertion' => $assertion
];

// \Psr\Http\Message\ResponseInterface
$response = $this->httpClient->request('post', $domain, ['form_params' => $parameters]);
$response = $this->httpClient->request('post', $fullInstanceUrl, ['form_params' => $parameters]);

$authToken = json_decode($response->getBody()->getContents(), true);

Expand Down
10 changes: 5 additions & 5 deletions src/Omniphx/Forrest/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
* @method string|array compactLayouts(string $resource, array $options = [])
* @method string|array flexiPage(string $resource, array $options = [])
* @method string|array knowledgeManagement(string $resource, array $options = [])
* @method string|array sobjects(string $resource, array $options = [])
* @method string|array sobjects(string $resource = "", array $options = [])
* @method string|array actions(string $resource, array $options = [])
* @method string|array support(string $resource, array $options = [])
*
Expand Down Expand Up @@ -212,13 +212,13 @@ private function handleRequest()
} else {
$this->setFormatter($this->settings['defaults']['format']);
}

if (isset($this->options['headers'])) {
$this->parameters['headers'] = array_replace_recursive($this->formatter->setHeaders(), $this->options['headers']);
} else {
$this->parameters['headers'] = $this->formatter->setHeaders();
}

if (isset($this->options['body'])) {
if ($this->parameters['headers']['Content-Type'] == $this->formatter->getDefaultMIMEType()) {
$this->parameters['body'] = $this->formatter->setBody($this->options['body']);
Expand All @@ -228,13 +228,13 @@ private function handleRequest()
} else {
unset($this->parameters['body']);
}

if (isset($this->options['query'])) {
$this->parameters['query'] = http_build_query($this->options['query']);
} else {
unset($this->parameters['query']);
}

if (isset($this->options['json'])) {
$this->parameters['json'] = $this->options['json'];
} else {
Expand Down
132 changes: 63 additions & 69 deletions src/Omniphx/Forrest/Formatters/CsvFormatter.php
Original file line number Diff line number Diff line change
@@ -1,69 +1,63 @@
<?php

namespace Omniphx\Forrest\Formatters;

use Omniphx\Forrest\Interfaces\FormatterInterface;

class CsvFormatter implements FormatterInterface
{
protected $tokenRepository;
protected $settings;
protected $headers;
protected $mimeType = 'application/json';
protected $acceptMimeType = 'text/csv';

public function __construct($tokenRepository, $settings) {
$this->tokenRepository = $tokenRepository;
$this->settings = $settings;
}

public function setHeaders()
{
$accessToken = $this->tokenRepository->get()['access_token'];
$tokenType = $this->tokenRepository->get()['token_type'];

$this->headers['Accept'] = $this->getDefaultAcceptMIMEType();
$this->headers['Content-Type'] = $this->getDefaultMIMEType();
$this->headers['Authorization'] = "$tokenType $accessToken";

$this->setCompression();

return $this->headers;
}

private function setCompression()
{
if (!$this->settings['defaults']['compression']) return;

$this->headers['Accept-Encoding'] = $this->settings['defaults']['compressionType'];
$this->headers['Content-Encoding'] = $this->settings['defaults']['compressionType'];
}

public function setBody($data)
{
return $data;
}

public function formatResponse($response)
{
$body = $response->getBody();
$header = $response->getHeaders();
$contents = (string) $body;

return [
'header' => $header,
'body' => $contents,

];
}

public function getDefaultMIMEType()
{
return $this->mimeType;
}

public function getDefaultAcceptMIMEType()
{
return $this->acceptMimeType;
}
}
<?php

namespace Omniphx\Forrest\Formatters;

use Omniphx\Forrest\Interfaces\FormatterInterface;

class CsvFormatter implements FormatterInterface
{
protected $tokenRepository;
protected $settings;
protected $headers;
protected $mimeType = 'application/json';
protected $acceptMimeType = 'text/csv';

public function __construct($tokenRepository, $settings) {
$this->tokenRepository = $tokenRepository;
$this->settings = $settings;
}

public function setHeaders()
{
$accessToken = $this->tokenRepository->get()['access_token'];
$tokenType = $this->tokenRepository->get()['token_type'];

$this->headers['Accept'] = $this->getDefaultAcceptMIMEType();
$this->headers['Content-Type'] = $this->getDefaultMIMEType();
$this->headers['Authorization'] = "$tokenType $accessToken";

$this->setCompression();

return $this->headers;
}

private function setCompression()
{
if (!$this->settings['defaults']['compression']) return;

$this->headers['Accept-Encoding'] = $this->settings['defaults']['compressionType'];
$this->headers['Content-Encoding'] = $this->settings['defaults']['compressionType'];
}

public function setBody($data)
{
return $data;
}

public function formatResponse($response)
{
$body = $response->getBody();
$contents = (string) $body;
return $contents;
}

public function getDefaultMIMEType()
{
return $this->mimeType;
}

public function getDefaultAcceptMIMEType()
{
return $this->acceptMimeType;
}
}
69 changes: 69 additions & 0 deletions src/Omniphx/Forrest/Formatters/CsvHeadersFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Omniphx\Forrest\Formatters;

use Omniphx\Forrest\Interfaces\FormatterInterface;

class CsvHeadersFormatter implements FormatterInterface
{
protected $tokenRepository;
protected $settings;
protected $headers;
protected $mimeType = 'application/json';
protected $acceptMimeType = 'text/csv';

public function __construct($tokenRepository, $settings) {
$this->tokenRepository = $tokenRepository;
$this->settings = $settings;
}

public function setHeaders()
{
$accessToken = $this->tokenRepository->get()['access_token'];
$tokenType = $this->tokenRepository->get()['token_type'];

$this->headers['Accept'] = $this->getDefaultAcceptMIMEType();
$this->headers['Content-Type'] = $this->getDefaultMIMEType();
$this->headers['Authorization'] = "$tokenType $accessToken";

$this->setCompression();

return $this->headers;
}

private function setCompression()
{
if (!$this->settings['defaults']['compression']) return;

$this->headers['Accept-Encoding'] = $this->settings['defaults']['compressionType'];
$this->headers['Content-Encoding'] = $this->settings['defaults']['compressionType'];
}

public function setBody($data)
{
return $data;
}

public function formatResponse($response)
{
$body = $response->getBody();
$header = $response->getHeaders();
$contents = (string) $body;

return [
'header' => $header,
'body' => $contents,

];
}

public function getDefaultMIMEType()
{
return $this->mimeType;
}

public function getDefaultAcceptMIMEType()
{
return $this->acceptMimeType;
}
}
6 changes: 2 additions & 4 deletions src/Omniphx/Forrest/Providers/Laravel/Facades/Forrest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,10 @@
* @method static string|array compactLayouts(string $resource, array $options = [])
* @method static string|array flexiPage(string $resource, array $options = [])
* @method static string|array knowledgeManagement(string $resource, array $options = [])
* @method static string|array sobjects(string $resource, array $options = [])
* @method static string|array sobjects(string $resource = "", array $options = [])
* @method static string|array actions(string $resource, array $options = [])
* @method static string|array support(string $resource, array $options = [])
* @method static \GuzzleHttp\ClientInterface getClient()
* @method static string getInstanceURL()
* @method static string getBaseUrl()
* @method static \Omniphx\Forrest\Interfaces\RedirectInterface callback()
*/
class Forrest extends Facade
{
Expand Down

0 comments on commit c062349

Please sign in to comment.