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

Dev json request #3

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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
51 changes: 51 additions & 0 deletions src/Downloader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace Fykosak\FKSDBDownloaderCore;

use Fykosak\FKSDBDownloaderCore\Requests\Request;

class Downloader
{
private array $config;

private string $url;
private string $username;
private string $password;

public function __construct(string $url, string $username, string $password)
{
$this->url = $url;
$this->username = $username;
$this->password = $password;
}

/**
* @throws DownloaderException
*/
public function download(Request $request): array
{
$options = [
'http' => [
'header' => "Content-type: application/json\r\n" .
"Accept: application/json\r\n" .
"Authorization: Basic " . base64_encode($this->username . ':' . $this->password),
'method' => 'GET',
'content' => json_encode($request->getParams()),
],
];
$context = stream_context_create($options);

$result = file_get_contents(
$this->url . $request->getMethod(),
false,
$context
);
if ($result === false) {
restore_error_handler();
throw new DownloaderException(error_get_last()['message']);
}
return json_decode($result, true);
}
}
9 changes: 9 additions & 0 deletions src/DownloaderException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Fykosak\FKSDBDownloaderCore;

class DownloaderException extends \Exception
{
}
19 changes: 13 additions & 6 deletions src/Requests/EventListRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,34 @@

class EventListRequest implements Request
{
private array $eventTypeIds;
/** @var int[] */
private array $eventTypes;

public function __construct(array $eventTypeIds)
/**
* @param int[] $eventTypes
*/
public function __construct(array $eventTypes)
{
$this->eventTypeIds = $eventTypeIds;
$this->eventTypes = $eventTypes;
}

/**
* @phpstan-return array{eventTypes:int[]}
*/
public function getParams(): array
{
return [
'eventTypeIds' => $this->eventTypeIds,
'eventTypes' => $this->eventTypes,
];
}

public function getCacheKey(): string
{
return sprintf('eventList.%s', join('-', $this->eventTypeIds));
return sprintf('event-list.%s', join('-', $this->eventTypes));
}

public function getMethod(): string
{
return 'GetEventList';
return 'events/';
}
}
2 changes: 1 addition & 1 deletion src/Requests/EventRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ public function getParams(): array

final public function getMethod(): string
{
return 'GetEvent';
return 'events/' . $this->eventId;
}
}
17 changes: 6 additions & 11 deletions src/Requests/OrganizersRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,19 @@ public function __construct(int $contestId, ?int $year = null)

public function getMethod(): string
{
return 'GetOrganizers';
return 'contests/' . $this->contestId . '/organizers';
}

/**
* @phpstan-return array{year?:int}
*/
public function getParams(): array
{
if (isset($this->year)) {
return [
'contestId' => $this->contestId,
'year' => $this->year,
];
}
return [
'contestId' => $this->contestId,
];
return isset($this->year) ? ['year' => $this->year] : [];
}

public function getCacheKey(): string
{
return sprintf('orgs.%s-%s', $this->contestId, $this->year ?? 'all');
return sprintf('organizers.%s-%s', $this->contestId, $this->year ?? 'all');
}
}
32 changes: 32 additions & 0 deletions src/Requests/ParticipantsRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Fykosak\FKSDBDownloaderCore\Requests;

class ParticipantsRequest implements Request
{
private int $eventId;

public function __construct(int $eventId)
{
$this->eventId = $eventId;
}

public function getMethod(): string
{
return 'events/' . $this->eventId . '/participants';
}

public function getParams(): array
{
return [
'eventId' => $this->eventId,
];
}

public function getCacheKey(): string
{
return sprintf('participant-list.%d', $this->eventId);
}
}
9 changes: 3 additions & 6 deletions src/Requests/SeriesResultsRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,28 @@ class SeriesResultsRequest implements Request
{
private int $contestId;
private int $year;
private int $series;

public function __construct(int $contestId, int $year, int $series)
public function __construct(int $contestId, int $year)
{
$this->contestId = $contestId;
$this->year = $year;
$this->series = $series;
}

public function getMethod(): string
{
return 'GetSeriesResults';
return sprintf('contests/%d/years/%d/results', $this->contestId, $this->year);
}

public function getParams(): array
{
return [
'contestId' => $this->contestId,
'year' => $this->year,
'series' => $this->series,
];
}

public function getCacheKey(): string
{
return sprintf('series.results.%s.%s.%s', $this->contestId, $this->year, $this->series);
return sprintf('series.results.%s.%s', $this->contestId, $this->year);
}
}
4 changes: 2 additions & 2 deletions src/Requests/StatsRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function __construct(int $contestId, int $year)

public function getMethod(): string
{
return 'GetStats';
return 'contests/' . $this->contestId . '/years/' . $this->year . '/stats';
}

public function getParams(): array
Expand All @@ -30,6 +30,6 @@ public function getParams(): array

public function getCacheKey(): string
{
return sprintf('task.stats.%s.%s', $this->contestId, $this->year);
return sprintf('task-stats.%s.%s', $this->contestId, $this->year);
}
}
32 changes: 32 additions & 0 deletions src/Requests/TeamsRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Fykosak\FKSDBDownloaderCore\Requests;

class TeamsRequest implements Request
{
private int $eventId;

public function __construct(int $eventId)
{
$this->eventId = $eventId;
}

public function getMethod(): string
{
return 'events/' . $this->eventId . '/teams';
}

public function getParams(): array
{
return [
'eventId' => $this->eventId,
];
}

public function getCacheKey(): string
{
return sprintf('team-list.%d', $this->eventId);
}
}
22 changes: 7 additions & 15 deletions tools/downloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,17 @@

declare(strict_types=1);

use Fykosak\FKSDBDownloaderCore\Downloader;
use Fykosak\FKSDBDownloaderCore\FKSDBDownloader;
use Fykosak\FKSDBDownloaderCore\Requests\Request;

require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/pass.php';

$downloader = new FKSDBDownloader(FKSDB_WSDL, FKSDB_USER, FKSDB_PASS, FKSDB_API);
$newDownloader = new Downloader(
FKSDB_API,
FKSDB_USER,
FKSDB_PASS,
);

return function (Request $request, bool $soap = true) use ($downloader): ?string {
try {
if ($soap) {
header('Content-Type: text/xml');
return $downloader->download($request);
} else {
header('Content-Type: text/json');
return $downloader->downloadJSON($request);
}
} catch (Throwable $exception) {
var_dump($exception);
}
return null;
};
return $newDownloader;
Loading