Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return Headers with CSV Formatter #337

Merged
merged 6 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 63 additions & 63 deletions src/Omniphx/Forrest/Formatters/CsvFormatter.php
Original file line number Diff line number Diff line change
@@ -1,63 +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();
$contents = (string) $body;
return $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;
}
}
Loading