Skip to content
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
.settings
vendor
composer.lock
index.php
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
"rest"
],
"require": {
"php": ">=5.3"
"php": ">=7.0",
"ext-json": "*",
"ext-yaml": "*"
},
"autoload": {
"psr-4": {
Expand Down
4 changes: 3 additions & 1 deletion src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
class Api extends Resource
{

const API_URI = 'https://allegroapi.io';
const API_URI = 'https://api.allegro.pl';

const TOKEN_URI = 'https://allegro.pl/auth/oauth/token';

const AUTHORIZATION_URI = 'https://allegro.pl/auth/oauth/authorize';

const API_YAML_URI = 'https://developer.allegro.pl/swagger.yaml';

/**
* Api constructor.
* @param string $clientId
Expand Down
39 changes: 33 additions & 6 deletions src/Resource.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<?php

namespace Allegro\REST;

class Resource
{

protected $_apiResource;

/**
* Resource constructor.
* @param string $id
Expand All @@ -15,6 +18,26 @@ public function __construct($id, Resource $parent)
$this->parent = $parent;
}

protected function apiResourse()
{
if (empty($this->_apiResource)) {
$apiResourse = (json_decode(file_get_contents(__DIR__ . '/swagger.json'), true))['paths'];
array_walk(
$apiResourse,
function (&$path, $index) {
$path = array_shift(array_keys(($get = array_shift((array_shift($path))['responses']))['content']));
}
);
$res = array();
foreach ($apiResourse as $key => $value) {
preg_match_all('/(\/[A-Z-a-z]*)+/m', $key, $match);
$res[] = array('value' => $value, 'path' => implode('', array_shift($match)), 'pattern' => $key);
}
$this->_apiResource = $res;
}
return $this->_apiResource;
}

/**
* @return mixed
*/
Expand Down Expand Up @@ -54,13 +77,16 @@ public function commands()
public function get($data = null)
{
$uri = $this->getUri();

$content = (array_shift(array_filter($this->apiResourse(), function ($item) use ($uri) {
return strpos($uri, $item['path']);
})))['value'];
echo $content;
if ($data !== null) {
$uri .= '?';
$uri .= http_build_query($data);
}

return $this->sendApiRequest($uri, 'GET');
echo '<br>'.$uri;
return $this->sendApiRequest($uri, 'GET', array(), $content);
}

/**
Expand Down Expand Up @@ -115,16 +141,17 @@ public function __call($name, $args)
* @param array $data
* @return bool|string
*/
protected function sendApiRequest($url, $method, $data = array())
protected function sendApiRequest($url, $method, $data = array(), $content = 'application/vnd.allegro.public.v1+json')
{
$token = $this->getAccessToken();
$key = $this->getApiKey();


$headers = array(
"Authorization: Bearer $token",
"Api-Key: $key",
"Content-Type: application/vnd.allegro.public.v1+json",
"Accept: application/vnd.allegro.public.v1+json"
"Content-Type: " . $content,
"Accept: " . $content,
);

$data = json_encode($data);
Expand Down
2 changes: 1 addition & 1 deletion src/Sandbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
class Sandbox extends Api
{

const API_URI = 'https://sandbox.allegroapi.io';
const API_URI = 'https://api.allegro.pl.allegrosandbox.pl';

const TOKEN_URI = 'https://allegro.pl.allegrosandbox.pl/auth/oauth/token';

Expand Down
1 change: 1 addition & 0 deletions src/swagger.json

Large diffs are not rendered by default.

Loading