partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

OctoPartService.php (7395B)


      1 <?php
      2 
      3 namespace PartKeepr\OctoPartBundle\Services;
      4 
      5 use Guzzle\Http\Client;
      6 use Predis\Client as PredisClient;
      7 
      8 class OctoPartService
      9 {
     10     const OCTOPART_ENDPOINT = "https://octopart.com/api/v4/endpoint";
     11 
     12     const OCTOPART_QUERY = <<<'EOD'
     13     query MyPartSearch($q: String!, $filters: Map, $limit: Int!, $start: Int, $country: String = "DE", $currency: String = "EUR") {
     14         search(q: $q, filters: $filters, limit: $limit, start: $start, country: $country, currency: $currency) {
     15           hits
     16       
     17           results {
     18             part {
     19               id
     20               mpn
     21               slug
     22               short_description
     23               counts
     24               manufacturer {
     25                 name
     26               }
     27               best_datasheet {
     28                 name
     29                 url
     30                 credit_string
     31                 credit_url
     32                 page_count
     33                 mime_type
     34               }
     35               best_image {
     36                   url
     37               }
     38               specs {
     39                 attribute {
     40                   name
     41                   group
     42                 }
     43                 display_value
     44               }
     45               document_collections {
     46                 name
     47                 documents {
     48                   name
     49                   url
     50                   credit_string
     51                   credit_url
     52                 }
     53               }
     54               descriptions {
     55                 credit_string
     56                 text
     57               }
     58               cad {
     59                 add_to_library_url
     60               }
     61               reference_designs {
     62                   name
     63                   url
     64               }
     65               sellers {
     66                 company {
     67                   homepage_url
     68                   is_verified
     69                   name
     70                   slug
     71                 }
     72                 is_authorized
     73                 is_broker
     74                 is_rfq
     75                 offers {
     76                   click_url
     77                   inventory_level
     78                   moq
     79                   packaging
     80                   prices {
     81                     conversion_rate
     82                     converted_currency
     83                     converted_price
     84                     currency
     85                     price
     86                     quantity
     87                   }
     88                   sku
     89                   updated
     90                 }
     91               }
     92             }
     93           }
     94         }
     95     }
     96 EOD;
     97 
     98     const OCTOPART_PARTQUERY = <<<'EOD'
     99     query MyPartSearch($id: String!, $country: String = "DE", $currency: String = "EUR") {
    100         parts(ids: [$id], country: $country, currency: $currency) {
    101                     id
    102                     mpn
    103                     slug
    104                     short_description
    105                     counts
    106                     manufacturer {
    107                       name
    108                     }
    109                     best_datasheet {
    110                       name
    111                       url
    112                       credit_string
    113                       credit_url
    114                       page_count
    115                       mime_type
    116                     }
    117                     best_image {
    118                         url
    119                     }
    120                     specs {
    121                       attribute {
    122                         name
    123                         group
    124                       }
    125                       display_value
    126                     }
    127                     document_collections {
    128                       name
    129                       documents {
    130                         name
    131                         url
    132                         credit_string
    133                         credit_url
    134                       }
    135                     }
    136                     descriptions {
    137                       credit_string
    138                       text
    139                     }
    140                     cad {
    141                       add_to_library_url
    142                     }
    143                     reference_designs {
    144                         name
    145                         url
    146                     }
    147                     sellers {
    148                       company {
    149                         homepage_url
    150                         is_verified
    151                         name
    152                         slug
    153                       }
    154                       is_authorized
    155                       is_broker
    156                       is_rfq
    157                       offers {
    158                         click_url
    159                         inventory_level
    160                         moq
    161                         packaging
    162                         prices {
    163                           conversion_rate
    164                           converted_currency
    165                           converted_price
    166                           currency
    167                           price
    168                           quantity
    169                         }
    170                         sku
    171                         updated
    172                       }
    173                     }
    174                 }}
    175 EOD;
    176 
    177     private $apiKey;
    178     private $limit = "3";
    179 
    180     public function __construct($apiKey, $limit)
    181     {
    182         $this->apiKey = $apiKey;
    183         $this->limit = $limit;
    184     }
    185 
    186     public function getPartByUID($uid)
    187     {
    188         try {
    189             $redisclient = new PredisClient();
    190             $redisclient->connect();
    191             $part = $redisclient->get($uid);
    192             if ($part) {
    193                 return json_decode($part, true);
    194             }
    195             $redisclient->disconnect();
    196         } catch (\Exception $e) {
    197         }
    198 
    199         $client = new Client();
    200         $request = $client->createRequest('POST', self::OCTOPART_ENDPOINT);
    201         $request->setHeader('Content-Type', 'application/json');
    202         $request->getQuery()->add("token", $this->apiKey);
    203 
    204         $graphql = [
    205             "query"         => self::OCTOPART_PARTQUERY,
    206             "operationName" => "MyPartSearch",
    207             "variables"     => [
    208                 "id" => $uid,
    209             ],
    210         ];
    211 
    212         $request->setBody(json_encode($graphql));
    213         $request->send();
    214 
    215         $body = $request->getResponse()->getBody();
    216 
    217         $data = json_decode($body, true);
    218 
    219         return $data["data"]["parts"][0];
    220     }
    221 
    222     public function getPartyByQuery($q, $startpage = 1)
    223     {
    224         $client = new Client();
    225 
    226         $request = $client->createRequest('POST', self::OCTOPART_ENDPOINT);
    227         $request->setHeader('Content-Type', 'application/json');
    228         $request->getQuery()->add("token", $this->apiKey);
    229 
    230         $graphql = [
    231             "query"         => self::OCTOPART_QUERY,
    232             "operationName" => "MyPartSearch",
    233             "variables"     => [
    234                 "q"     => $q,
    235                 "limit" => $this->limit,
    236                 "start" => ($startpage - 1) * $this->limit,   // "start" is 0-based
    237             ],
    238         ];
    239 
    240         $request->setBody(json_encode($graphql));
    241         $request->send();
    242 
    243         $body = $request->getResponse()->getBody();
    244 
    245         $parts = json_decode($body, true);
    246 
    247         // work around the low number of allowed accesses to octopart's api
    248         try {
    249             $redisclient = new PredisClient();
    250             $redisclient->connect();
    251             $results = $parts["data"]["search"]["results"];
    252             foreach ($results as $result) {
    253                 $id = $result["part"]["id"];
    254                 $redisclient->set($id, json_encode($result["part"]));
    255             }
    256             $redisclient->disconnect();
    257         } catch (\Exception $e) {
    258         }
    259 
    260         return $parts;
    261     }
    262 }