diff options
author | Filipp Lepalaan <filipp@mac.com> | 2011-04-30 15:26:20 +0300 |
---|---|---|
committer | Filipp Lepalaan <filipp@mac.com> | 2011-04-30 15:26:20 +0300 |
commit | eafe4ebe27f366bb4e24d321a5ff157feaa798b2 (patch) | |
tree | 4ac3f6742a6bdaae3aa3b9b080ce6a2b64fdb454 | |
parent | 584981a53ec5c0d1a5b6cf3d4d3f0f374ca6df3f (diff) | |
download | gsxlib-eafe4ebe27f366bb4e24d321a5ff157feaa798b2.tar.gz gsxlib-eafe4ebe27f366bb4e24d321a5ff157feaa798b2.tar.bz2 gsxlib-eafe4ebe27f366bb4e24d321a5ff157feaa798b2.zip |
added partsLookup, gsxcl
-rw-r--r-- | README.md | 6 | ||||
-rwxr-xr-x | gsxcl | 50 | ||||
-rw-r--r-- | gsxlib.php | 104 |
3 files changed, 132 insertions, 28 deletions
@@ -23,6 +23,12 @@ Best illustrated with a simple example: > MacBook Pro (15-inch 2.4/2.2GHz) ?> +If you're in the US, remember to set the fifth argument to the constructor to 'am'. + +##gsxcl## + +The package includes a rudimentary command line client to the GSX API called gsxcl. Currently it can perform warranty checks +and part lookups. ##License## @@ -0,0 +1,50 @@ +#!/usr/bin/env php +<?php + +/** + * gsxlib/gsxcl + * @package gsxlib + * @author Filipp Lepalaan <filipp@mcare.fi> + */ + +if (false) { + error_reporting(E_ALL|E_STRICT); +} + +require 'gsxlib.php'; + +if (count($argv) < 6) { + echo <<<EOT + +usage: gsxcl -s sold-to -u username -p password [-r region] [-e environment] [-m warranty|parts] [-q query] + -s sold-to your GSX Sold-To account. + -u username the Apple ID with GSX WS API access. + -p password the password for the Apple ID. + -r region Either "am" (America), "emea" (Europe, Middle-East, Africa) + "apac" (Asia-Pacific) or "la" (Latin America). Defaults to "emea". + -e environment the GSX environment. Either empty (production), "it" or "ut". Defaults to production. + -m mode What to search for. Currently either "warranty" (default) or "parts" + -q query A query string (serial number, order confirmation, repair number, EEE code, etc. + Defaults to current serial number. + +EOT; + exit(); +} + +$opts = getopt('s:u:p:r:e:m:q::'); +$gsx = new GsxLib($opts['s'], $opts['u'], $opts['p'], $opts['e'], $opts['r']); + +if (empty($opts['q'])) { + $opts['q'] = `system_profiler SPHardwareDataType | awk '/Serial Number/ {print $4}'`; +} + +switch ($opts['m']) { + case 'warranty': + print_r($gsx->warrantyStatus($opts['q'])); + break; + case 'parts': + print_r($gsx->partsLookup($opts['q'])); + break; +} + +?>
\ No newline at end of file @@ -1,7 +1,11 @@ <?php /** * gsxlib/gsxlib.php + * @package gsxlib * @author Filipp Lepalaan <filipp@mcare.fi> + * http://gsxwsut.apple.com/apidocs/html/WSReference.html?user=asp + * http://gsxwsut.apple.com/apidocs/html/WSArtifacts.html?user=asp + * @license * This program is free software. It comes without any warranty, to * the extent permitted by applicable law. You can redistribute it * and/or modify it under the terms of the Do What The Fuck You Want @@ -15,8 +19,7 @@ class GsxLib private $session_id; private $environment; - - function __construct($account, $username, $password, $environment = 'ws', $region = 'emea', $tz = 'CEST') + function __construct($account, $username, $password, $environment = '', $region = 'emea', $tz = 'CEST') { if (!class_exists('SoapClient')) { exit('Looks like your PHP lacks SOAP support'); @@ -29,16 +32,18 @@ class GsxLib $regions = array('am', 'emea', 'apac', 'la'); if (!in_array($region, $regions)) { - exit('Region must be one of: ' . implode(', ', $regions)); + exit('Region '.$region.' should be one of: ' . implode(', ', $regions)); } - $envirs = array('ws', 'ut', 'it'); + $envirs = array('ut', 'it'); - if (!in_array($environment, $envirs)) { - exit('Environment must be one of: ' . implode(', ', $envir)); + if (!empty($environment)) { + if (!in_array($environment, $envirs)) { + exit('Environment '.$environment. ' should be one of: ' . implode(', ', $envirs)); + } } - $wsdl = 'https://gsx'.$environment.'.apple.com/gsx-ws/services/'.$region.'/asp?wsdl'; + $wsdl = 'https://gsxws'.$environment.'.apple.com/gsx-ws/services/'.$region.'/asp?wsdl'; $this->client = new SoapClient($wsdl, array('exceptions' => TRUE, 'trace' => 1)); if (!$this->client) { @@ -60,11 +65,11 @@ class GsxLib try { $this->session_id = $this->client->Authenticate($a)->AuthenticateResponse->userSessionId; - } catch (Exception $e) { - exit('Authentication with GSX failed'); + } catch (SoapFault $e) { + exit('Authentication with GSX failed. Does this GSX account have access to this environment?'); } - // there's a session going, put the credentials in the + // there's a session going, put the credentials in there if (session_id()) { $_SESSION['_gsxlib_session_id'] = $this->session_id; $_SESSION['_gsxlib_session_timeout'] = time()+60*30; @@ -72,12 +77,49 @@ class GsxLib } - public function partDetails($partNumber) + /** + * a shortcut for looking up part information + * @param mixed $string + * @return [bool|string] + */ + public function partsLookup($string = null) { - if (!preg_match('/^\w+\-\w+^/', $partNumber)) { - exit('Invalid part number: ' . $partNumber); + $string = trim($string); + $what = $this->looksLike($string); + + if (!$what) { + exit('Invalid search term for part lookup: ' . $string); } + $req = array('PartsLookup' => array('lookupRequestData' => array($what => $string))); + return $this->request($req)->parts; + + } + + /** + * Try to "categorise" a string + * @param string $string + */ + private function looksLike($string, $what = null) + { + $result = false; + + $rex = array( + 'partNumber' => '/^\w+\-\w+$/i', + 'serialNumber' => '/^[a-z0-9]{7,18}$/i', + 'eeeCode' => '/^[a-z0-9]{3,4}$/i', + 'repairNumber' => '/^\d{12}$/', + 'repairConfirmationNumber' => '/^G\d{9}$/i' + ); + + foreach ($rex as $k => $v) { + if (preg_match($v, $string)) { + $result = $k; + } + } + + return ($what) ? ($result == $what) : $result; + } /** @@ -85,31 +127,37 @@ class GsxLib */ public function warrantyStatus($serialNumber) { - if (!preg_match('/^[a-z0-9]{7,18}$/i', $serialNumber)) { + $serialNumber = trim($serialNumber); + + if (!$this->looksLike($serialNumber, 'serialNumber')) { exit('Invalid serial number: ' . $serialNumber); } $a = array( - 'WarrantyStatusRequest' => array( - 'userSession' => array('userSessionId' => $this->session_id), - 'unitDetail' => array('serialNumber' => $serialNumber) - )); + 'WarrantyStatus' => array('unitDetail' => array('serialNumber' => $serialNumber)) + ); + + return $this->request($a)->warrantyDetailInfo; + + } + + private function request($req) + { + $result = false; + list($r, $p) = each($req); + $p['userSession'] = array('userSessionId' => $this->session_id); + $request = array($r.'Request' => $p); try { - $result = $this->client->WarrantyStatus($a); + $result = $this->client->$r($request); + $resp = "{$r}Response"; + return $result->$resp; } catch (SoapFault $e) { trigger_error($e->getMessage()); - return FALSE; } - return $result->WarrantyStatusResponse->warrantyDetailInfo; - - } - - public function request($args) - { - $info = $client->WarrantyStatus($a); - $out[] = $info->WarrantyStatusResponse->warrantyDetailInfo; + return $result; + } } |