1
0
mirror of https://github.com/ToxicCrack/PrintABrick.git synced 2025-05-17 04:40:08 -07:00

Update Rebrickable client

This commit is contained in:
David Hübner 2016-11-20 22:56:26 +01:00
parent 2d57bc8ed3
commit a2e270de14
5 changed files with 716 additions and 235 deletions

View File

@ -0,0 +1,24 @@
<?php
namespace AppBundle\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';
case 'rb_color_id': return 'colorId';
default: return $propertyName;
}
}
}

View File

@ -0,0 +1,233 @@
<?php
/**
* Created by PhpStorm.
* User: hubnedav
* Date: 11/18/16
* Time: 1:45 AM
*/
namespace AppBundle\Client\Rebrickable\Entity;
class Color
{
/**
* Internal Rebrickable color ID used
* @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;
/**
* Color name
* @var string
*/
protected $color_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
* @var string
*/
protected $rgb;
/**
* Color constructor.
*/
public function __construct()
{
}
/**
* @return int
*/
public function getRbColorId()
{
return $this->rb_color_id;
}
/**
* @param int $rb_color_id
*/
public function setRbColorId($rb_color_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;
}
/**
* @return string
*/
public function getColorName()
{
return $this->color_name;
}
/**
* @param string $color_name
*/
public function setColorName($color_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;
}
/**
* @return string
*/
public function getRgb()
{
return $this->rgb;
}
/**
* @param string $rgbl
*/
public function setRgb($rgb)
{
$this->rgb = $rgb;
}
}

View File

@ -3,160 +3,242 @@
* Created by PhpStorm.
* User: hubnedav
* Date: 11/11/16
* Time: 12:45 AM
* Time: 12:45 AM.
*/
namespace AppBundle\Client\Rebrickable\Entity;
class Part
{
protected $id;
protected $name;
protected $category;
protected $typeId;
protected $colors;
protected $external_part_ids;
protected $part_url;
protected $part_img_url;
/**
* Part ID.
*
* @var int
*/
private $id;
/**
* Quantity of part in set returned from getSetParts.
*
* @var int
*/
private $qty;
/**
* Part Name.
*
* @var string
*/
private $name;
/**
* Year the part first appeared in sets.
*
* @var int
*/
private $year1;
/**
* Year the part was last seen in sets.
*
* @var int
*/
private $year2;
/**
* Part category/type description.
*
* @var string
*/
private $category;
public function __construct($part_id, $part_name, $category, $part_type_id, $colors, $external_part_ids, $part_url, $part_img_url)
{
$this->id = $part_id;
$this->name = $part_name;
$this->category = $category;
$this->typeId = $part_type_id;
$this->colors = $colors;
$this->external_part_ids = $external_part_ids;
$this->part_url = $part_url;
$this->part_img_url = $part_img_url;
}
private $typeId;
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* Array of colors the part appears in.
*
* @var array
*/
private $colors;
/**
* @param mixed $id
*/
public function setId($id)
{
$this->id = $id;
}
private $colorId;
/**
* @return mixed
*/
public function getName()
{
return $this->name;
}
/**
* Array of related Part IDs used by external systems.
*
* @var array
*/
private $external_part_ids;
/**
* Rebrickable URL to the Part Details page.
*
* @var string
*/
private $part_url;
/**
* Rebrickable URL of the main part image (tries to use most common color).
*
* @var string
*/
private $part_img_url;
/**
* @param mixed $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* @return mixed
*/
public function getCategory()
{
return $this->category;
}
public function __construct()
{
}
/**
* @param mixed $category
*/
public function setCategory($category)
{
$this->category = $category;
}
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @return mixed
*/
public function getTypeId()
{
return $this->typeId;
}
/**
* @param $part_id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* @param mixed $typeId
*/
public function setTypeId($typeId)
{
$this->typeId = $typeId;
}
/**
* @return int
*/
public function getQty()
{
return $this->qty;
}
/**
* @return mixed
*/
public function getColors()
{
return $this->colors;
}
/**
* @param $qty
*/
public function setQty($qty)
{
$this->qty = $qty;
}
/**
* @param mixed $colors
*/
public function setColors($colors)
{
$this->colors = $colors;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @return mixed
*/
public function getExternalPartIds()
{
return $this->external_part_ids;
}
/**
* @param mixed $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* @param mixed $external_part_ids
*/
public function setExternalPartIds($external_part_ids)
{
$this->external_part_ids = $external_part_ids;
}
/**
* @return mixed
*/
public function getYear1()
{
return $this->year1;
}
/**
* @return mixed
*/
public function getPartUrl()
{
return $this->part_url;
}
/**
* @param mixed $year1
*/
public function setYear1($year1)
{
$this->year1 = $year1;
}
/**
* @param mixed $part_url
*/
public function setPartUrl($part_url)
{
$this->part_url = $part_url;
}
/**
* @return mixed
*/
public function getYear2()
{
return $this->year2;
}
/**
* @return mixed
*/
public function getPartImgUrl()
{
return $this->part_img_url;
}
/**
* @param mixed $year2
*/
public function setYear2($year2)
{
$this->year2 = $year2;
}
/**
* @param mixed $part_img_url
*/
public function setPartImgUrl($part_img_url)
{
$this->part_img_url = $part_img_url;
}
/**
* @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;
}
}

View File

@ -0,0 +1,80 @@
<?php
namespace AppBundle\Client\Rebrickable\Entity;
class Set
{
/**
* Set ID if relevan to type.
*
* @var int
*/
protected $set_id;
/**
* Set description.
*
* @var string
*/
protected $name;
/**
* Number of sets the part appears in.
*
* @var int
*/
protected $num_parts;
/**
* Set constructor.
*/
public function __construct()
{
}
/**
* @return int
*/
public function getSetId()
{
return $this->set_id;
}
/**
* @param int $set_id
*/
public function setSetId($set_id)
{
$this->set_id = $set_id;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* @return int
*/
public function getNumParts()
{
return $this->num_parts;
}
/**
* @param int $num_parts
*/
public function setNumParts($num_parts)
{
$this->num_parts = $num_parts;
}
}

View File

@ -2,126 +2,188 @@
namespace AppBundle\Client\Rebrickable;
use AppBundle\Client\Rebrickable\Entity\Color;
use AppBundle\Client\Rebrickable\Entity\Part;
use AppBundle\Client\Rebrickable\Entity\Set;
use AppBundle\Client\Rebrickable\Converter\PartPropertyNameConverter;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ConnectException;
use Symfony\Component\Asset\Exception\LogicException;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
class Rebrickable
{
const BASE_URI = 'https://rebrickable.com/api/';
const FORMAT = 'json';
const BASE_URI = 'https://rebrickable.com/api/';
const FORMAT = 'json';
private $client;
private $client;
private $apiKey;
private $apiKey;
/**
* RebrickableAPI constructor.
*/
public function __construct($apiKey)
{
$this->client = new Client(['base_uri' => self::BASE_URI]);
$this->apiKey = $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
*/
private function call($method, $parameters = [])
{
$parameters['query']['key'] = $this->apiKey;
$parameters['query']['format'] = self::FORMAT;
private function call($method, $parameters) {
$parameters['query']['key'] = $this->apiKey;
$parameters['query']['format'] = self::FORMAT;
try {
$response = $this->client->request('GET', $method, $parameters);
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;
} else {
return $content;
}
} else {
//TODO
throw new LogicException($response->getStatusCode());
}
} catch (ConnectException $e) {
//TODO
throw new LogicException($e->getMessage());
} catch (ClientException $e) {
//TODO
throw new LogicException($e->getMessage());
}
}
if ($response->getStatusCode() === 200)
{
$content = $response->getBody()->getContents();
if($content === 'INVALIDKEY') {
//TODO
throw new LogicException("Invalid API Key");
} else {
return $content;
}
}
else
{
//TODO
throw new LogicException($response->getStatusCode());
}
} catch (ConnectException $e) {
//TODO
throw new LogicException($e->getMessage());
} catch (ClientException $e) {
//TODO
throw new LogicException($e->getMessage());
}
}
private function getSerializer()
{
$encoders = array(new JsonEncoder());
$nameConverter = new PartPropertyNameConverter();
$normalizers = array(new ObjectNormalizer(null,$nameConverter), new ArrayDenormalizer());
$serializer = new Serializer($normalizers, $encoders);
/**
* Get a list of all parts (normal + spare) used in a set.
*
* @param string $setName unique rebrickable set name
*
* @return array
*/
public function getSetParts($setName)
{
$parameters = [
'query' => [
'set' => $setName,
]
];
return $serializer;
}
return $this->call('get_set_parts', $parameters);
}
/**
* 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,
],
];
/**
* Get details about a specific part.
*
* @param $partID
*
* @return
*/
public function getPart($partID)
{
$parameters = [
'query' => [
'part_id' => $partID,
'inc_ext' => 1
]
];
$data = $this->call('get_set_parts', $parameters);
return $this->call('get_part',$parameters);
}
$serializer = $this->getSerializer();
$partsSTD = json_decode($data, true)[0]['parts'];
/**
* Get the list of colors used by all parts.
*
* @return
*/
public function getColors()
{
return $this->call('get_colors', []);
}
return $data ? $serializer->denormalize($partsSTD, Part::class.'[]', self::FORMAT) : null;
}
/**
* Get the list of sets that a specific part/color appears in.
*
* @param $partID
* @param $colorID
*
* @return
*/
public function getPartSets($partID, $colorID)
{
$parameters = [
'query' => [
'part_id' => $partID,
'color_id' => $colorID
]
];
/**
* Get details about a specific part.
*
* @param $partID
*
* @return Part|null
*/
public function getPart($partID)
{
$parameters = [
'query' => [
'part_id' => $partID,
'inc_ext' => 1,
],
];
return $this->call('get_part_sets', $parameters);
}
$data = $this->call('get_part', $parameters);
$serializer = $this->getSerializer();
return $data ? $serializer->deserialize($data, Part::class, self::FORMAT) : null;
}
/**
* Get associative array of colors used by all parts where key == rb_color_id
*
* @return Color[]|null
*/
public function getColors()
{
$data = json_decode($this->call('get_colors'), true);
$serializer = $this->getSerializer();
$colors = [];
foreach ($data as $item) {
$color = $serializer->denormalize($item, Color::class, self::FORMAT);
$colors[$color->getRbColorId()] = $color;
}
return $data ? $colors : null;
}
/**
* Get associative array of themes used by all parts where key == part_type_id
*
* @return string[]
*/
public function getPartTypes()
{
$data = json_decode($this->call('get_part_types'), true)['part_types'];
$types = [];
foreach ($data as $item) {
$types[$item['part_type_id']] = $item['desc'];
}
return $data ? $types : null;
}
/**
* Get the list of sets that a specific part/color appears in.
*
* @param $partID
* @param $colorID
*
* @return Set[]
*/
public function getPartSets($partID, $colorID)
{
$parameters = [
'query' => [
'part_id' => $partID,
'color_id' => $colorID,
],
];
$serializer = $this->getSerializer();
$data = json_decode($this->call('get_part_sets', $parameters), true)[0]['sets'];
return $data ? $serializer->denormalize($data, Set::class.'[]', self::FORMAT) : null;
}
}