Esempio n. 1
0
        def handler(**args):
            '''Dynamically created handler for a Flickr API call'''

            # Set some defaults
            defaults = {'method': method,
                        'auth_token': self.token,
                        'api_key': self.apiKey,
                        'format': 'rest'}
            for key, default_value in defaults.iteritems():
                if key not in args:
                    args[key] = default_value
                # You are able to remove a default by assigning None
                if key in args and args[key] is None:
                    del args[key]

            LOG.debug("Calling %s(%s)" % (method, args))

            postData = self.encode_and_sign(args)

            f = urllib.urlopen(url, postData)
            data = f.read()
            f.close()

            # Return the raw response when a non-REST format
            # was chosen.
            if args['format'] != 'rest':
                return data
            
            result = XMLNode.parseXML(data, True)
            if self.fail_on_error:
                FlickrAPI.testFailure(result, True)

            return result
Esempio n. 2
0
    def parse_xmlnode(self, rest_xml):
        """Parses a REST XML response from Flickr into an XMLNode object."""

        rsp = XMLNode.parse(rest_xml, store_xml=True)
        if rsp['stat'] == 'ok':
            return rsp

        err = rsp.err[0]
        raise FlickrError(six.u('Error: %(code)s: %(msg)s') % err, code=err['code'])
Esempio n. 3
0
    def parse_xmlnode(self, rest_xml):
        '''Parses a REST XML response from Flickr into an XMLNode object.'''

        rsp = XMLNode.parse(rest_xml, store_xml=True)
        if rsp['stat'] == 'ok':
            return rsp
        
        err = rsp.err[0]
        raise FlickrError(u'Error: %(code)s: %(msg)s' % err)
Esempio n. 4
0
    def parse_xmlnode(self, rest_xml):
        '''Parses a REST XML response from Flickr into an XMLNode object.'''

        rsp = XMLNode.parse(rest_xml, store_xml=True)
        if rsp['stat'] == 'ok':
            return rsp

        err = rsp.err[0]
        raise FlickrError(u'Error: %(code)s: %(msg)s' % err)
Esempio n. 5
0
    def parse_xmlnode(self, rest_xml):
        """Parses a REST XML response from Flickr into an XMLNode object."""

        rsp = XMLNode.parse(rest_xml, store_xml=True)
        if rsp['stat'] == 'ok':
            return rsp

        err = rsp.err[0]
        raise FlickrError(six.u('Error: %(code)s: %(msg)s') % err, code=err['code'])
Esempio n. 6
0
    def parse_xmlnode(self, rest_xml):
        u'''Parses a REST XML response from Flickr into an XMLNode object.''' #$NON-NLS-1$

        rsp = XMLNode.parse(rest_xml, store_xml=True)
        if rsp[u'stat'] == u'ok' or not self.fail_on_error: #$NON-NLS-2$ #$NON-NLS-1$
            return rsp
        
        err = rsp.err[0]
        raise FlickrError(u'Error: %(code)s: %(msg)s' % err) #$NON-NLS-1$
Esempio n. 7
0
    def __getCachedToken(self):
        """Read and return a cached token, or None if not found.

        The token is read from the cached token file, which is basically the
        entire RSP response containing the auth element.
        """

        try:
            f = file(self.__getCachedTokenFilename(), "r")
            
            data = f.read()
            f.close()

            rsp = XMLNode.parseXML(data)

            return rsp.auth[0].token[0].elementText

        except Exception:
            return None
Esempio n. 8
0
    def testMoreParsing(self):
        '''Tests parsing of XML.

        This used to be a doctest, but it was very hard to make it compatible
        with both Python 2 and 3.
        '''

        xml_str = '''<xml foo="32">
            <taggy bar="10">Name0</taggy>
            <taggy bar="11" baz="12">Name1</taggy>
            </xml>'''

        f = XMLNode.parse(xml_str)
        self.assertEqual(f.name, 'xml')
        self.assertEqual(f['foo'], '32')
        self.assertEqual(f.taggy[0].name, 'taggy')
        self.assertEqual(f.taggy[0]["bar"], '10')
        self.assertEqual(f.taggy[0].text, 'Name0')
        self.assertEqual(f.taggy[1].name, 'taggy')
        self.assertEqual(f.taggy[1]["bar"], '11')
        self.assertEqual(f.taggy[1]["baz"], '12')
Esempio n. 9
0
        def handler(**args):
            '''Dynamically created handler for a Flickr API call'''

            explicit_format = 'format' in args

            if self.token_cache.token and not self.secret:
                raise ValueError("Auth tokens cannot be used without "
                                 "API secret")

            # Set some defaults
            defaults = {'method': method,
                        'auth_token': self.token_cache.token,
                        'api_key': self.api_key,
                        'format': 'rest'}

            for key, default_value in defaults.iteritems():
                if key not in args:
                    args[key] = default_value
                # You are able to remove a default by assigning None
                if key in args and args[key] is None:
                    del args[key]

            LOG.debug("Calling %s(%s)" % (method, args))

            post_data = self.encode_and_sign(args)

            flicksocket = urllib.urlopen(url, post_data)
            data = flicksocket.read()
            flicksocket.close()

            # Return the raw response when the user requested
            # a specific format.
            if explicit_format:
                return data
            
            result = XMLNode.parse(data, True)
            if self.fail_on_error:
                FlickrAPI.test_failure(result, True)

            return result
Esempio n. 10
0
    def __send_multipart(self, url, body, progress_callback=None):
        '''Sends a Multipart object to an URL.
        
        Returns the resulting XML from Flickr.
        '''

        LOG.debug("Uploading to %s" % url)
        request = urllib2.Request(url)
        request.add_data(str(body))

        (header, value) = body.header()
        request.add_header(header, value)
        
        if progress_callback:
            response = reportinghttp.urlopen(request, progress_callback)
        else:
            response = urllib2.urlopen(request)
        rspXML = response.read()

        result = XMLNode.parse(rspXML)
        if self.fail_on_error:
            FlickrAPI.test_failure(result, True)

        return result
Esempio n. 11
0
    def testGroupInfoXml(self):
        '''This XML exposed a bug in 1.0, should parse okay now.'''
 
        XMLNode.parse(group_info_xml)
Esempio n. 12
0
 def setUp(self):
     self.doc = XMLNode.parse(xml, True)