1
0
mirror of https://github.com/ToxicCrack/PrintABrick.git synced 2025-05-16 20:30:09 -07:00

migrate rebrickable api to v3

This commit is contained in:
David Hübner 2017-02-06 01:33:46 +01:00
parent 0b589ba2ca
commit 83992cdcdd
12 changed files with 560 additions and 525 deletions

View File

@ -7,7 +7,7 @@
{{ dump(localPart) }}
<div id="model" style="height: 300px; width: 300px; padding: 5px; display: inline-block"></div>
<img src="{{ part.partImgUrl }}">
<img src="{{ part.imgUrl }}">
{% endblock %}

View File

@ -3,7 +3,7 @@ services:
class: AppBundle\Api\Client\Brickset\Brickset
arguments: ['%brickset_apikey%']
client.rebrickable:
class: AppBundle\Api\Client\Rebrickable\Rebrickable
class: AppBundle\Api\Client\Rebrickable\Rebrickable_v3
arguments: ['%rebrickable_apikey%']
manager.brickset:

View File

@ -1,23 +0,0 @@
<?php
namespace AppBundle\Api\Client\Rebrickable\Converter;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
class PartPropertyNameConverter implements NameConverterInterface
{
public function normalize($propertyName)
{
return $propertyName;
}
public function denormalize($propertyName)
{
switch ($propertyName) {
case 'part_name': return 'name';
case 'part_id': return 'id';
case 'part_type_id': return 'typeId';
default: return $propertyName;
}
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace AppBundle\Api\Client\Rebrickable\Converter;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
class PropertyNameConverter implements NameConverterInterface
{
public function normalize($propertyName)
{
return $propertyName;
}
public function denormalize($propertyName)
{
switch ($propertyName) {
case 'part_num': return 'number';
case 'part_cat_id': return 'categoryId';
case 'part_img_url': return 'imgUrl';
case 'part_url': return 'url';
case 'set_num': return 'number';
case 'set_img_url': return 'imgUrl';
case 'set_url': return 'url';
default: return $propertyName;
}
}
}

View File

@ -5,215 +5,60 @@ namespace AppBundle\Api\Client\Rebrickable\Entity;
class Color
{
/**
* Internal Rebrickable color ID used.
* Internal Rebrickable color ID.
*
* @var int
*/
protected $rb_color_id;
/**
* Array of mapped LDraw colors.
*
* @var array
*/
protected $ldraw_color_id;
/**
* Array of mapped BrickLink colors.
*
* @var array
*/
protected $bricklink_color_id;
/**
* Array of mapped BrickOwl colors.
*
* @var array
*/
protected $brickowl_color_id;
private $id;
/**
* Color name.
*
* @var string
*/
protected $color_name;
private $name;
/**
* Number of parts the color appears in.
*
* @var int
*/
protected $num_parts;
/**
* Number of sets the color appears in.
*
* @var int
*/
protected $num_sets;
/**
* First year it was used.
*
* @var int
*/
protected $year1;
/**
* Last year it was used.
*
* @var int
*/
protected $year2;
/**
* Hex codes for the RGB value of this color: RRGGBB.
* RGB hex code.
*
* @var string
*/
protected $rgb;
private $rgb;
/**
* Color constructor.
* Wether color is transparent.
*
* @var bool
*/
public function __construct()
{
}
private $isTrans;
/**
* @return int
*/
public function getRbColorId()
public function getId()
{
return $this->rb_color_id;
return $this->id;
}
/**
* @param int $rb_color_id
* @param int $id
*/
public function setRbColorId($rb_color_id)
public function setId($id)
{
$this->rb_color_id = $rb_color_id;
}
/**
* @return array
*/
public function getLdrawColorId()
{
return $this->ldraw_color_id;
}
/**
* @param array $ldraw_color_id
*/
public function setLdrawColorId($ldraw_color_id)
{
$this->ldraw_color_id = $ldraw_color_id;
}
/**
* @return array
*/
public function getBricklinkColorId()
{
return $this->bricklink_color_id;
}
/**
* @param array $bricklink_color_id
*/
public function setBricklinkColorId($bricklink_color_id)
{
$this->bricklink_color_id = $bricklink_color_id;
}
/**
* @return array
*/
public function getBrickowlColorId()
{
return $this->brickowl_color_id;
}
/**
* @param array $brickowl_color_id
*/
public function setBrickowlColorId($brickowl_color_id)
{
$this->brickowl_color_id = $brickowl_color_id;
$this->id = $id;
}
/**
* @return string
*/
public function getColorName()
public function getName()
{
return $this->color_name;
return $this->name;
}
/**
* @param string $color_name
* @param string $name
*/
public function setColorName($color_name)
public function setName($name)
{
$this->color_name = $color_name;
}
/**
* @return int
*/
public function getNumParts()
{
return $this->num_parts;
}
/**
* @param int $num_parts
*/
public function setNumParts($num_parts)
{
$this->num_parts = $num_parts;
}
/**
* @return int
*/
public function getNumSets()
{
return $this->num_sets;
}
/**
* @param int $num_sets
*/
public function setNumSets($num_sets)
{
$this->num_sets = $num_sets;
}
/**
* @return int
*/
public function getYear1()
{
return $this->year1;
}
/**
* @param int $year1
*/
public function setYear1($year1)
{
$this->year1 = $year1;
}
/**
* @return int
*/
public function getYear2()
{
return $this->year2;
}
/**
* @param int $year2
*/
public function setYear2($year2)
{
$this->year2 = $year2;
$this->name = $name;
}
/**
@ -225,10 +70,26 @@ class Color
}
/**
* @param string $rgbl
* @param string $rgb
*/
public function setRgb($rgb)
{
$this->rgb = $rgb;
}
/**
* @return bool
*/
public function isIsTrans()
{
return $this->isTrans;
}
/**
* @param bool $isTrans
*/
public function setIsTrans($isTrans)
{
$this->isTrans = $isTrans;
}
}

View File

@ -5,17 +5,11 @@ namespace AppBundle\Api\Client\Rebrickable\Entity;
class Part
{
/**
* Part ID.
* Part ID number.
*
* @var int
*/
private $id;
/**
* Quantity of part in set returned from getSetParts.
*
* @var int
*/
private $qty;
private $number;
/**
* Part Name.
*
@ -23,72 +17,89 @@ class Part
*/
private $name;
/**
* Part type 1 = normal part, 2 = spare part.
* Part category id.
*
* @var int
*/
private $type;
private $categoryId;
/**
* Year the part first appeared in sets.
*
* @var int
*/
private $year1;
private $yearFrom;
/**
* Year the part was last seen in sets.
*
* @var int
*/
private $year2;
/**
* Part category/type description.
*
* @var string
*/
private $category;
private $yearTo;
/**
* Array of colors the part appears in.
*
* @var array
*/
private $colors;
/**
* Array of related Part IDs used by external systems.
*
* @var array
*/
private $external_part_ids;
private $externalIds;
/**
* Rebrickable URL to the Part Details page.
*
* @var string
*/
private $part_url;
private $url;
/**
* Rebrickable URL of the main part image (tries to use most common color).
*
* @var string
*/
private $part_img_url;
private $imgUrl;
public function __construct()
/**
* @return int
*/
public function getPartNum()
{
return $this->partNum;
}
/**
* @param int $partNum
*/
public function setPartNum($partNum)
{
$this->partNum = $partNum;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* @return int
*/
public function getId()
public function getCategoryId()
{
return $this->id;
return $this->categoryId;
}
/**
* @param $part_id
* @param int $categoryId
*/
public function setId($id)
public function setCategoryId($categoryId)
{
$this->id = $id;
$this->categoryId = $categoryId;
}
/**
@ -110,144 +121,80 @@ class Part
/**
* @return int
*/
public function getQty()
public function getYearFrom()
{
return $this->qty;
return $this->yearFrom;
}
/**
* @param $qty
* @param int $yearFrom
*/
public function setQty($qty)
public function setYearFrom($yearFrom)
{
$this->qty = $qty;
$this->yearFrom = $yearFrom;
}
/**
* @return int
*/
public function getYearTo()
{
return $this->yearTo;
}
/**
* @param int $yearTo
*/
public function setYearTo($yearTo)
{
$this->yearTo = $yearTo;
}
/**
* @return array
*/
public function getExternalIds()
{
return $this->externalIds;
}
/**
* @param array $externalIds
*/
public function setExternalIds($externalIds)
{
$this->externalIds = $externalIds;
}
/**
* @return string
*/
public function getName()
public function getUrl()
{
return $this->name;
return $this->url;
}
/**
* @param mixed $name
* @param string $url
*/
public function setName($name)
public function setUrl($url)
{
$this->name = $name;
$this->url = $url;
}
/**
* @return mixed
* @return string
*/
public function getYear1()
public function getImgUrl()
{
return $this->year1;
return $this->imgUrl;
}
/**
* @param mixed $year1
* @param string $imgUrl
*/
public function setYear1($year1)
public function setImgUrl($imgUrl)
{
$this->year1 = $year1;
}
/**
* @return mixed
*/
public function getYear2()
{
return $this->year2;
}
/**
* @param mixed $year2
*/
public function setYear2($year2)
{
$this->year2 = $year2;
}
/**
* @return mixed
*/
public function getCategory()
{
return $this->category;
}
/**
* @param mixed $category
*/
public function setCategory($category)
{
$this->category = $category;
}
/**
* @return mixed
*/
public function getColors()
{
return $this->colors;
}
/**
* @param mixed $colors
*/
public function setColors($colors)
{
$this->colors = $colors;
}
/**
* @return mixed
*/
public function getExternalPartIds()
{
return $this->external_part_ids;
}
/**
* @param mixed $external_part_ids
*/
public function setExternalPartIds($external_part_ids)
{
$this->external_part_ids = $external_part_ids;
}
/**
* @return mixed
*/
public function getPartUrl()
{
return $this->part_url;
}
/**
* @param mixed $part_url
*/
public function setPartUrl($part_url)
{
$this->part_url = $part_url;
}
/**
* @return mixed
*/
public function getPartImgUrl()
{
return $this->part_img_url;
}
/**
* @param $part_img_url
*/
public function setPartImgUrl($part_img_url)
{
$this->part_img_url = $part_img_url;
$this->imgUrl = $imgUrl;
}
}

View File

@ -0,0 +1,69 @@
<?php
namespace AppBundle\Api\Client\Rebrickable\Entity;
class PartCategory
{
/**
* @var int
*/
private $id;
/**
* @var string
*/
private $name;
/**
* @var int
*/
private $partCount;
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @param int $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* @return int
*/
public function getPartCount()
{
return $this->partCount;
}
/**
* @param int $partCount
*/
public function setPartCount($partCount)
{
$this->partCount = $partCount;
}
}

View File

@ -9,41 +9,58 @@ class Set
*
* @var int
*/
protected $set_id;
private $number;
/**
* Set description.
*
* @var string
*/
protected $name;
private $name;
/**
* Number of sets the part appears in.
* Year of set release.
*
* @var int
*/
protected $num_parts;
private $year;
/**
* Set constructor.
* Count of parts in set.
*
* @var int
*/
public function __construct()
{
}
private $numParts;
/**
* Rebrickable internal theme id.
*
* @var int
*/
private $themeId;
/**
* Rebrickable URL of the main set image.
*
* @var string
*/
private $imgUrl;
/**
* Rebrickable URL to the Set Details page.
*
* @var string
*/
private $url;
/**
* @return int
*/
public function getSetId()
public function getNumber()
{
return $this->set_id;
return $this->number;
}
/**
* @param int $set_id
* @param int $number
*/
public function setSetId($set_id)
public function setNumber($number)
{
$this->set_id = $set_id;
$this->number = $number;
}
/**
@ -65,16 +82,80 @@ class Set
/**
* @return int
*/
public function getNumParts()
public function getYear()
{
return $this->num_parts;
return $this->year;
}
/**
* @param int $num_parts
* @param int $year
*/
public function setNumParts($num_parts)
public function setYear($year)
{
$this->num_parts = $num_parts;
$this->year = $year;
}
/**
* @return int
*/
public function getNumParts()
{
return $this->numParts;
}
/**
* @param int $numParts
*/
public function setNumParts($numParts)
{
$this->numParts = $numParts;
}
/**
* @return int
*/
public function getThemeId()
{
return $this->themeId;
}
/**
* @param int $themeId
*/
public function setThemeId($themeId)
{
$this->themeId = $themeId;
}
/**
* @return string
*/
public function getImgUrl()
{
return $this->imgUrl;
}
/**
* @param string $imgUrl
*/
public function setImgUrl($imgUrl)
{
$this->imgUrl = $imgUrl;
}
/**
* @return string
*/
public function getUrl()
{
return $this->url;
}
/**
* @param string $url
*/
public function setUrl($url)
{
$this->url = $url;
}
}

View File

@ -0,0 +1,75 @@
<?php
namespace AppBundle\Api\Client\Rebrickable\Entity;
class Theme
{
/**
* Rebrickable internal id of theme.
*
* @var int
*/
private $id;
/**
* Rebrickable internal id of parent theme.
*
* @var int
*/
private $parentId;
/**
* Name of theme.
*
* @var string
*/
private $name;
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @param int $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* @return int
*/
public function getParentId()
{
return $this->parentId;
}
/**
* @param int $parentId
*/
public function setParentId($parentId)
{
$this->parentId = $parentId;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}
}

View File

@ -1,69 +0,0 @@
<?php
namespace AppBundle\Api\Client\Rebrickable;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ConnectException;
use Symfony\Component\Asset\Exception\LogicException;
class Rebrickable
{
const BASE_URI = 'https://rebrickable.com/api/';
const FORMAT = 'json';
/**
* @var Client
*/
private $client;
/**
* @var string
*/
private $apiKey;
/**
* RebrickableAPI constructor.
*/
public function __construct($apiKey)
{
$this->client = new Client(['base_uri' => self::BASE_URI]);
$this->apiKey = $apiKey;
}
/**
* @param $method
* @param $parameters
*
* @return null|string
*/
public function call($method, $parameters = [])
{
$parameters['query']['key'] = $this->apiKey;
$parameters['query']['format'] = self::FORMAT;
try {
$response = $this->client->request('GET', $method, $parameters);
if ($response->getStatusCode() === 200) {
$content = $response->getBody()->getContents();
if ($content === 'INVALIDKEY') {
//TODO
throw new LogicException('Invalid API Key');
} elseif ($content === 'NOSET' || $content === 'NOPART') {
return null;
}
return $content;
}
//TODO
throw new LogicException($response->getStatusCode());
} catch (ConnectException $e) {
//TODO
throw new LogicException($e->getMessage());
} catch (ClientException $e) {
//TODO
throw new LogicException($e->getMessage());
}
}
}

View File

@ -0,0 +1,67 @@
<?php
namespace AppBundle\Api\Client\Rebrickable;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ConnectException;
use Symfony\Component\Asset\Exception\LogicException;
class Rebrickable_v3
{
const BASE_URI = 'https://rebrickable.com/api/v3/';
/**
* @var Client
*/
private $client;
/**
* @var string
*/
private $apiKey;
/**
* RebrickableAPI constructor.
*/
public function __construct($apiKey)
{
$this->client = new Client(['base_uri' => self::BASE_URI]);
$this->apiKey = $apiKey;
}
/**
* @param $method
* @param $uri
* @param array $options
*
* @return string
*/
public function call($method, $uri, $options = [])
{
$options['query']['key'] = $this->apiKey;
$options['headers'] = [
'Accept' => 'application/json',
];
try {
$response = $this->client->request($method, $uri, $options);
$content = $response->getBody()->getContents();
return $content;
} catch (ConnectException $e) {
//TODO
throw new LogicException($e);
} catch (ClientException $e) {
//TODO
if ($e->getCode() == 404) {
throw new LogicException('Not Found');
}
if ($e->getCode() == 500) {
throw new LogicException('Invalid token');
}
throw new LogicException($e);
}
}
}

View File

@ -2,11 +2,13 @@
namespace AppBundle\Api\Manager;
use AppBundle\Api\Client\Rebrickable\Converter\PartPropertyNameConverter;
use AppBundle\Api\Client\Rebrickable\Converter\PropertyNameConverter;
use AppBundle\Api\Client\Rebrickable\Entity\Color;
use AppBundle\Api\Client\Rebrickable\Entity\Part;
use AppBundle\Api\Client\Rebrickable\Entity\PartCategory;
use AppBundle\Api\Client\Rebrickable\Entity\Set;
use AppBundle\Api\Client\Rebrickable\Rebrickable;
use AppBundle\Api\Client\Rebrickable\Entity\Theme;
use AppBundle\Api\Client\Rebrickable\Rebrickable_v3;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
@ -17,151 +19,149 @@ class RebrickableManager
const FORMAT = 'json';
/**
* @var Rebrickable
* @var Rebrickable_v3
*/
private $rebrickableClient;
/**
* @var Serializer
*/
private $serializer;
/**
* RebrickableManager constructor.
*
* @param Rebrickable $rebrickableClient
* @param Rebrickable_v3 $rebrickableClient
*/
public function __construct(Rebrickable $rebrickableClient)
public function __construct(Rebrickable_v3 $rebrickableClient)
{
$this->rebrickableClient = $rebrickableClient;
$this->serializer = $this->initSerializer();
}
private function getSerializer()
private function initSerializer()
{
$encoders = [new JsonEncoder()];
$nameConverter = new PartPropertyNameConverter();
$objectNormalizer = new ObjectNormalizer(null, $nameConverter);
$objectNormalizer = new ObjectNormalizer(null, new PropertyNameConverter());
$normalizers = [$objectNormalizer, new ArrayDenormalizer()];
$serializer = new Serializer($normalizers, $encoders);
return $serializer;
}
/**
* Get a list of all parts (normal + spare) used in a set.
*
* @param string $setName unique rebrickable set name
*
* @return Part[]|null
*/
public function getSetParts($setName)
{
$parameters = [
'query' => [
'set' => $setName,
],
];
$data = $this->rebrickableClient->call('get_set_parts', $parameters);
$serializer = $this->getSerializer();
$partsSTD = json_decode($data, true)[0]['parts'];
if ($data) {
$parts = $serializer->denormalize($partsSTD, Part::class.'[]', self::FORMAT);
foreach ($parts as $key => &$part) {
$part->setCategory($this->getPartTypes()[$partsSTD[$key]['part_type_id']]);
$part->setColors([
0 => [
'color_name' => $partsSTD[$key]['color_name'],
'rb_color_id' => $partsSTD[$key]['rb_color_id'],
'ldraw_color_id' => $partsSTD[$key]['ldraw_color_id'],
],
]);
}
return $data;
}
return null;
}
/**
* Get details about a specific part.
*
* @param $partID
* @param $id
*
* @return Part|null
* @return Part
*/
public function getPart($partID)
public function getPart($id)
{
$parameters = [
'query' => [
'part_id' => $partID,
'inc_ext' => 1,
],
];
$data = $this->rebrickableClient->call('GET', 'lego/parts/'.$id);
$data = $this->rebrickableClient->call('get_part', $parameters);
$serializer = $this->getSerializer();
return $data ? $serializer->deserialize($data, Part::class, self::FORMAT) : null;
return $this->serializer->deserialize($data, Part::class, self::FORMAT);
}
/**
* Get associative array of colors used by all parts where key == rb_color_id.
* Get details about a specific Color.
*
* @return Color[]|null
* @param $id
*
* @return Color
*/
public function getColors()
public function getColor($id)
{
$data = json_decode($this->rebrickableClient->call('get_colors'), true);
$data = $this->rebrickableClient->call('GET', 'lego/colors/'.$id);
$serializer = $this->getSerializer();
$colors = [];
foreach ($data as $item) {
$color = $serializer->denormalize($item, Color::class, self::FORMAT);
$colors[$color->getRbColorId()] = $color;
}
return $data ? $colors : [];
return $this->serializer->deserialize($data, Color::class, self::FORMAT);
}
/**
* Get associative array of themes used by all parts where key == part_type_id.
* Get details for a specific Set.
*
* @return string[]
* @param $id
*
* @return Set
*/
public function getPartTypes()
public function getSet($id)
{
$data = json_decode($this->rebrickableClient->call('get_part_types'), true)['part_types'];
$data = $this->rebrickableClient->call('GET', 'lego/sets/'.$id);
$types = [];
foreach ($data as $item) {
$types[$item['part_type_id']] = $item['desc'];
}
return $this->serializer->deserialize($data, Set::class, self::FORMAT);
}
return $data ? $types : null;
/**
* Return details for a specific Theme.
*
* @param $id
*
* @return Theme
*/
public function getTheme($id)
{
$data = $this->rebrickableClient->call('GET', 'lego/themes/'.$id);
return $this->serializer->deserialize($data, Theme::class, self::FORMAT);
}
/**
* Return details for a specific PartCategory.
*
* @param $id
*
* @return PartCategory
*/
public function getPartCategory($id)
{
$data = $this->rebrickableClient->call('GET', 'lego/part_categories/'.$id);
return $this->serializer->deserialize($data, PartCategory::class, self::FORMAT);
}
/**
* Get the list of sets that a specific part/color appears in.
*
* @param $partID
* @param $colorID
* @param $partId
* @param $colorId
* @param $page
*
* @return Set[]
*/
public function getPartSets($partID, $colorID)
public function getPartSets($partId, $colorId, $page = null)
{
$parameters = [
$options = [
'query' => [
'part_id' => $partID,
'color_id' => $colorID,
'page' => $page,
],
];
$serializer = $this->getSerializer();
$response = $this->rebrickableClient->call('GET', 'lego/parts/'.$partId.'/colors/'.$colorId.'/sets', $options);
$data = json_decode($response, true)['results'];
$data = json_decode($this->rebrickableClient->call('get_part_sets', $parameters), true)[0]['sets'];
return $this->serializer->denormalize($data, Set::class.'[]', self::FORMAT);
}
return $data ? $serializer->denormalize($data, Set::class.'[]', self::FORMAT) : null;
/**
* Get a list of all parts (normal + spare) used in a set.
*
* @param $setId
* @param $page
*
* @return
*/
public function getSetParts($setId, $page = null)
{
$options = [
'query' => [
'page' => $page,
],
];
//
// $response = $this->rebrickableClient->call('GET','lego/sets/'.$setId.'/parts', $options);
//
// $data = json_decode($response, true)['results'];
//
// return $this->serializer->denormalize($data, Part::class . '[]', self::FORMAT);
}
}