~chroot64bit/zivios/gentoo-experimental

« back to all changes in this revision

Viewing changes to application/library/Zend/Service/Amazon.php

  • Committer: Mustafa A. Hashmi
  • Date: 2008-12-04 13:32:21 UTC
  • Revision ID: mhashmi@zivios.org-20081204133221-0nd1trunwevijj38
Inclusion of new installation framework with ties to zend layout and dojo layout

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
/**
 
4
 * Zend Framework
 
5
 *
 
6
 * LICENSE
 
7
 *
 
8
 * This source file is subject to the new BSD license that is bundled
 
9
 * with this package in the file LICENSE.txt.
 
10
 * It is also available through the world-wide-web at this URL:
 
11
 * http://framework.zend.com/license/new-bsd
 
12
 * If you did not receive a copy of the license and are unable to
 
13
 * obtain it through the world-wide-web, please send an email
 
14
 * to license@zend.com so we can send you a copy immediately.
 
15
 *
 
16
 * @category   Zend
 
17
 * @package    Zend_Service
 
18
 * @subpackage Amazon
 
19
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 
20
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 
21
 * @version    $Id: Amazon.php 8064 2008-02-16 10:58:39Z thomas $
 
22
 */
 
23
 
 
24
 
 
25
/**
 
26
 * @category   Zend
 
27
 * @package    Zend_Service
 
28
 * @subpackage Amazon
 
29
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 
30
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 
31
 */
 
32
class Zend_Service_Amazon
 
33
{
 
34
    /**
 
35
     * Amazon Web Services Access Key ID
 
36
     *
 
37
     * @var string
 
38
     */
 
39
    public $appId;
 
40
 
 
41
    /**
 
42
     * List of Amazon Web Service base URLs, indexed by country code
 
43
     *
 
44
     * @var array
 
45
     */
 
46
    protected $_baseUriList = array('US' => 'http://webservices.amazon.com',
 
47
                                    'UK' => 'http://webservices.amazon.co.uk',
 
48
                                    'DE' => 'http://webservices.amazon.de',
 
49
                                    'JP' => 'http://webservices.amazon.co.jp',
 
50
                                    'FR' => 'http://webservices.amazon.fr',
 
51
                                    'CA' => 'http://webservices.amazon.ca');
 
52
 
 
53
    /**
 
54
     * Reference to REST client object
 
55
     *
 
56
     * @var Zend_Rest_Client
 
57
     */
 
58
    protected $_rest;
 
59
 
 
60
 
 
61
    /**
 
62
     * Constructs a new Amazon Web Services Client
 
63
     *
 
64
     * @param  string $appId       Developer's Amazon appid
 
65
     * @param  string $countryCode Country code for Amazon service; may be US, UK, DE, JP, FR, CA
 
66
     * @throws Zend_Service_Exception
 
67
     * @return Zend_Service_Amazon
 
68
     */
 
69
    public function __construct($appId, $countryCode = 'US')
 
70
    {
 
71
        $this->appId = (string) $appId;
 
72
        $countryCode = (string) $countryCode;
 
73
 
 
74
        if (!isset($this->_baseUriList[$countryCode])) {
 
75
            /**
 
76
             * @see Zend_Service_Exception
 
77
             */
 
78
            require_once 'Zend/Service/Exception.php';
 
79
            throw new Zend_Service_Exception("Unknown country code: $countryCode");
 
80
        }
 
81
 
 
82
        /**
 
83
         * @see Zend_Rest_Client
 
84
         */
 
85
        require_once 'Zend/Rest/Client.php';
 
86
        $this->_rest = new Zend_Rest_Client($this->_baseUriList[$countryCode]);
 
87
    }
 
88
 
 
89
 
 
90
    /**
 
91
     * Search for Items
 
92
     *
 
93
     * @param  array $options Options to use for the Search Query
 
94
     * @throws Zend_Service_Exception
 
95
     * @return Zend_Service_Amazon_ResultSet
 
96
     * @see http://www.amazon.com/gp/aws/sdk/main.html/102-9041115-9057709?s=AWSEcommerceService&v=2005-10-05&p=ApiReference/ItemSearchOperation
 
97
     */
 
98
    public function itemSearch(array $options)
 
99
    {
 
100
        $defaultOptions = array('ResponseGroup' => 'Small');
 
101
        $options = $this->_prepareOptions('ItemSearch', $options, $defaultOptions);
 
102
        $this->_rest->getHttpClient()->resetParameters();
 
103
        $response = $this->_rest->restGet('/onca/xml', $options);
 
104
 
 
105
        if ($response->isError()) {
 
106
            /**
 
107
             * @see Zend_Service_Exception
 
108
             */
 
109
            require_once 'Zend/Service/Exception.php';
 
110
            throw new Zend_Service_Exception('An error occurred sending request. Status code: '
 
111
                                           . $response->getStatus());
 
112
        }
 
113
 
 
114
        $dom = new DOMDocument();
 
115
        $dom->loadXML($response->getBody());
 
116
        self::_checkErrors($dom);
 
117
 
 
118
        /**
 
119
         * @see Zend_Service_Amazon_ResultSet
 
120
         */
 
121
        require_once 'Zend/Service/Amazon/ResultSet.php';
 
122
        return new Zend_Service_Amazon_ResultSet($dom);
 
123
    }
 
124
 
 
125
 
 
126
    /**
 
127
     * Look up item(s) by ASIN
 
128
     *
 
129
     * @param  string $asin    Amazon ASIN ID
 
130
     * @param  array  $options Query Options
 
131
     * @see http://www.amazon.com/gp/aws/sdk/main.html/102-9041115-9057709?s=AWSEcommerceService&v=2005-10-05&p=ApiReference/ItemLookupOperation
 
132
     * @throws Zend_Service_Exception
 
133
     * @return Zend_Service_Amazon_Item|Zend_Service_Amazon_ResultSet
 
134
     */
 
135
    public function itemLookup($asin, array $options = array())
 
136
    {
 
137
        $defaultOptions = array('IdType' => 'ASIN', 'ResponseGroup' => 'Small');
 
138
        $options['ItemId'] = (string) $asin;
 
139
        $options = $this->_prepareOptions('ItemLookup', $options, $defaultOptions);
 
140
        $this->_rest->getHttpClient()->resetParameters();
 
141
        $response = $this->_rest->restGet('/onca/xml', $options);
 
142
 
 
143
        if ($response->isError()) {
 
144
            /**
 
145
             * @see Zend_Service_Exception
 
146
             */
 
147
            require_once 'Zend/Service/Exception.php';
 
148
            throw new Zend_Service_Exception('An error occurred sending request. Status code: '
 
149
                                           . $response->getStatus());
 
150
        }
 
151
 
 
152
        $dom = new DOMDocument();
 
153
        $dom->loadXML($response->getBody());
 
154
        self::_checkErrors($dom);
 
155
        $xpath = new DOMXPath($dom);
 
156
        $xpath->registerNamespace('az', 'http://webservices.amazon.com/AWSECommerceService/2005-10-05');
 
157
        $items = $xpath->query('//az:Items/az:Item');
 
158
 
 
159
        if ($items->length == 1) {
 
160
            /**
 
161
             * @see Zend_Service_Amazon_Item
 
162
             */
 
163
            require_once 'Zend/Service/Amazon/Item.php';
 
164
            return new Zend_Service_Amazon_Item($items->item(0));
 
165
        }
 
166
 
 
167
        /**
 
168
         * @see Zend_Service_Amazon_ResultSet
 
169
         */
 
170
        require_once 'Zend/Service/Amazon/ResultSet.php';
 
171
        return new Zend_Service_Amazon_ResultSet($dom);
 
172
    }
 
173
 
 
174
 
 
175
    /**
 
176
     * Returns a reference to the REST client
 
177
     *
 
178
     * @return Zend_Rest_Client
 
179
     */
 
180
    public function getRestClient()
 
181
    {
 
182
        return $this->_rest;
 
183
    }
 
184
 
 
185
 
 
186
    /**
 
187
     * Prepare options for request
 
188
     *
 
189
     * @param  string $query          Action to perform
 
190
     * @param  array  $options        User supplied options
 
191
     * @param  array  $defaultOptions Default options
 
192
     * @return array
 
193
     */
 
194
    protected function _prepareOptions($query, array $options, array $defaultOptions)
 
195
    {
 
196
        $options['SubscriptionId'] = $this->appId;
 
197
        $options['Service']        = 'AWSECommerceService';
 
198
        $options['Operation']      = (string) $query;
 
199
 
 
200
        // de-canonicalize out sort key
 
201
        if (isset($options['ResponseGroup'])) {
 
202
            $responseGroup = split(',', $options['ResponseGroup']);
 
203
 
 
204
            if (!in_array('Request', $responseGroup)) {
 
205
                $responseGroup[] = 'Request';
 
206
                $options['ResponseGroup'] = implode(',', $responseGroup);
 
207
            }
 
208
        }
 
209
 
 
210
        $options = array_merge($defaultOptions, $options);
 
211
        return $options;
 
212
    }
 
213
 
 
214
 
 
215
    /**
 
216
     * Check result for errors
 
217
     *
 
218
     * @param  DOMDocument $dom
 
219
     * @throws Zend_Service_Exception
 
220
     * @return void
 
221
     */
 
222
    protected static function _checkErrors(DOMDocument $dom)
 
223
    {
 
224
        $xpath = new DOMXPath($dom);
 
225
        $xpath->registerNamespace('az', 'http://webservices.amazon.com/AWSECommerceService/2005-10-05');
 
226
 
 
227
        if ($xpath->query('//az:Error')->length >= 1) {
 
228
            $code = $xpath->query('//az:Error/az:Code/text()')->item(0)->data;
 
229
            $message = $xpath->query('//az:Error/az:Message/text()')->item(0)->data;
 
230
 
 
231
            /**
 
232
             * @see Zend_Service_Exception
 
233
             */
 
234
            require_once 'Zend/Service/Exception.php';
 
235
            throw new Zend_Service_Exception("$message ($code)");
 
236
        }
 
237
    }
 
238
}