~terrycojones/txfluiddb/perms-Dec-2009

« back to all changes in this revision

Viewing changes to txfluiddb/client.py

  • Committer: Tristan Seligmann
  • Date: 2009-08-28 19:20:12 UTC
  • mfrom: (3.1.4 authentication)
  • Revision ID: mithrandi@mithrandi.net-20090828192012-6asfrrkbdfueuh4k
MergeĀ lp:~tristan/txfluiddb/authentication.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
from urllib import quote, urlencode
2
2
from re import compile, U
 
3
from base64 import b64encode
3
4
 
4
5
import simplejson as json
5
6
 
329
330
 
330
331
 
331
332
 
 
333
class BasicCreds(object):
 
334
    """
 
335
    Credentials for HTTP Basic Authentication.
 
336
 
 
337
    @type username: C{str}
 
338
    @ivar username: The username.
 
339
 
 
340
    @type password: C{str}
 
341
    @ivar password: The password.
 
342
    """
 
343
    def __init__(self, username, password):
 
344
        self.username = username
 
345
        self.password = password
 
346
 
 
347
    def encode(self):
 
348
        """
 
349
        Encode these credentials.
 
350
 
 
351
        @rtype:  C{str}
 
352
        @return: Encoded form for use in HTTP Authorization header.
 
353
        """
 
354
        return 'Basic %s' % b64encode(
 
355
            '%s:%s' % (self.username, self.password))
 
356
 
 
357
 
 
358
 
 
359
FLUIDDB_ENDPOINT = 'http://fluiddb.fluidinfo.com/'
 
360
 
332
361
class Endpoint(object):
333
362
    """
334
363
    A FluidDB endpoint.
340
369
 
341
370
    @type version: C{str} or C{None}
342
371
    @ivar version: The API version to use, or C{None} for the latest.
 
372
 
 
373
    @type creds: L{BasicCreds}
 
374
    @ivar creds: A credentials object, or C{None} to authenticate anonymously.
343
375
    """
344
 
    def __init__(self, baseURL='http://fluiddb.fluidinfo.com/', version=None):
 
376
    def __init__(self, baseURL=FLUIDDB_ENDPOINT, version=None, creds=None):
 
377
        if creds is not None and not isinstance(creds, BasicCreds):
 
378
            raise ValueError('Only basic authentication is supported')
 
379
 
345
380
        self.baseURL = baseURL
346
381
        self.version = version
 
382
        self.creds = creds
347
383
 
348
384
 
349
385
    def getRootURL(self):
390
426
            data = json.dumps(data)
391
427
            headers['Content-Type'] = 'application/json'
392
428
 
 
429
        if self.creds is not None:
 
430
            headers['Authorization'] = self.creds.encode()
 
431
 
393
432
        def _parse(res):
394
433
            if res:
395
434
                return json.loads(unicode(res, 'utf-8'))
403
442
 
404
443
 
405
444
 
406
 
SUPPORTED_ENCODINGS = set([u'base-64'])
 
445
_SUPPORTED_ENCODINGS = set([u'base-64'])
407
446
 
408
447
class Object(_HasPath):
409
448
    """
543
582
        """
544
583
        url = self.getURL(endpoint, suffix=tag.components) + '?format=json'
545
584
        encodings = ';'.join(enc.encode('ascii')
546
 
                             for enc in SUPPORTED_ENCODINGS)
 
585
                             for enc in _SUPPORTED_ENCODINGS)
547
586
        d = endpoint.submit(url=url,
548
587
                            method='GET',
549
588
                            headers={'Accept-Encoding': encodings})
619
658
    def __init__(self, contentType, data):
620
659
        self.contentType = contentType
621
660
        self.data = data
 
661
 
 
662
 
 
663
 
 
664
__all__ = ['Namespace', 'Tag', 'BasicCreds', 'FLUIDDB_ENDPOINT', 'Endpoint',
 
665
           'Object', 'Blob']