<s> from py2neo import PRODUCT <EOL> from py2neo . database . auth import ServerAddress , keyring <EOL> from py2neo . database . status import GraphError , Unauthorized <EOL> from py2neo . packages . httpstream import http , ClientError , ServerError , Resource as _Resource , ResourceTemplate as _ResourceTemplate <EOL> from py2neo . packages . httpstream . http import JSONResponse , user_agent <EOL> from py2neo . packages . httpstream . numbers import UNAUTHORIZED <EOL> from py2neo . packages . httpstream . packages . urimagic import URI <EOL> from py2neo . util import raise_from <EOL> http . default_encoding = "<STR_LIT>" <EOL> _http_headers = { <EOL> ( None , None , None ) : [ <EOL> ( "<STR_LIT>" , user_agent ( PRODUCT ) ) , <EOL> ( "<STR_LIT>" , "<STR_LIT:true>" ) , <EOL> ] , <EOL> } <EOL> def set_http_header ( key , value , scheme = None , host = None , port = None ) : <EOL> """<STR_LIT>""" <EOL> address_key = ( scheme , host , port ) <EOL> if address_key in _http_headers : <EOL> _http_headers [ address_key ] . append ( ( key , value ) ) <EOL> else : <EOL> _http_headers [ address_key ] = [ ( key , value ) ] <EOL> def get_http_headers ( scheme , host , port ) : <EOL> """<STR_LIT>""" <EOL> uri_headers = { } <EOL> for ( s , h , p ) , headers in _http_headers . items ( ) : <EOL> if ( s is None or s == scheme ) and ( h is None or h == host ) and ( p is None or p == port ) : <EOL> uri_headers . update ( headers ) <EOL> for address , auth in keyring . items ( ) : <EOL> if auth and address . host == host and address . http_port == port : <EOL> uri_headers [ "<STR_LIT>" ] = auth . http_authorization <EOL> return uri_headers <EOL> class Resource ( _Resource ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , uri , metadata = None , headers = None ) : <EOL> uri = URI ( uri ) <EOL> self . _resource = _Resource . __init__ ( self , uri ) <EOL> self . _headers = dict ( headers or { } ) <EOL> self . __base = super ( Resource , self ) <EOL> if metadata is None : <EOL> self . __initial_metadata = None <EOL> else : <EOL> self . __initial_metadata = dict ( metadata ) <EOL> self . __last_get_response = None <EOL> uri = uri . string <EOL> dbms_uri = uri [ : uri . find ( "<STR_LIT:/>" , uri . find ( "<STR_LIT>" ) + <NUM_LIT:2> ) ] + "<STR_LIT:/>" <EOL> if dbms_uri == uri : <EOL> self . __dbms = self <EOL> else : <EOL> from py2neo . database import DBMS <EOL> self . __dbms = DBMS ( dbms_uri ) <EOL> self . __ref = NotImplemented <EOL> @ property <EOL> def graph ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __dbms . graph <EOL> @ property <EOL> def headers ( self ) : <EOL> """<STR_LIT>""" <EOL> headers = get_http_headers ( self . __uri__ . scheme , self . __uri__ . host , self . __uri__ . port ) <EOL> headers . update ( self . _headers ) <EOL> return headers <EOL> @ property <EOL> def metadata ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . __last_get_response is None : <EOL> if self . __initial_metadata is not None : <EOL> return self . __initial_metadata <EOL> self . get ( ) <EOL> return self . __last_get_response . content <EOL> def resolve ( self , reference , strict = True ) : <EOL> """<STR_LIT>""" <EOL> return Resource ( _Resource . resolve ( self , reference , strict ) . uri ) <EOL> @ property <EOL> def dbms ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __dbms <EOL> def get ( self , headers = None , redirect_limit = <NUM_LIT:5> , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> headers = dict ( self . headers , ** ( headers or { } ) ) <EOL> kwargs . update ( cache = True ) <EOL> try : <EOL> response = self . __base . get ( headers = headers , redirect_limit = redirect_limit , ** kwargs ) <EOL> except ( ClientError , ServerError ) as error : <EOL> if error . status_code == UNAUTHORIZED : <EOL> raise Unauthorized ( self . uri . string ) <EOL> if isinstance ( error , JSONResponse ) : <EOL> content = dict ( error . content , request = error . request , response = error ) <EOL> else : <EOL> content = { } <EOL> message = content . pop ( "<STR_LIT:message>" , "<STR_LIT>" % error . status_code ) <EOL> raise_from ( GraphError ( message , ** content ) , error ) <EOL> else : <EOL> self . __last_get_response = response <EOL> return response <EOL> def put ( self , body = None , headers = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> headers = dict ( self . headers , ** ( headers or { } ) ) <EOL> try : <EOL> response = self . __base . put ( body , headers , ** kwargs ) <EOL> except ( ClientError , ServerError ) as error : <EOL> if error . status_code == UNAUTHORIZED : <EOL> raise Unauthorized ( self . uri . string ) <EOL> if isinstance ( error , JSONResponse ) : <EOL> content = dict ( error . content , request = error . request , response = error ) <EOL> else : <EOL> content = { } <EOL> message = content . pop ( "<STR_LIT:message>" , "<STR_LIT>" % error . status_code ) <EOL> raise_from ( GraphError ( message , ** content ) , error ) <EOL> else : <EOL> return response <EOL> def post ( self , body = None , headers = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> headers = dict ( self . headers , ** ( headers or { } ) ) <EOL> try : <EOL> response = self . __base . post ( body , headers , ** kwargs ) <EOL> except ( ClientError , ServerError ) as error : <EOL> if error . status_code == UNAUTHORIZED : <EOL> raise Unauthorized ( self . uri . string ) <EOL> if isinstance ( error , JSONResponse ) : <EOL> content = dict ( error . content , request = error . request , response = error ) <EOL> else : <EOL> content = { } <EOL> message = content . pop ( "<STR_LIT:message>" , "<STR_LIT>" % error . status_code ) <EOL> raise_from ( GraphError ( message , ** content ) , error ) <EOL> else : <EOL> return response <EOL> def delete ( self , headers = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> headers = dict ( self . headers , ** ( headers or { } ) ) <EOL> try : <EOL> response = self . __base . delete ( headers , ** kwargs ) <EOL> except ( ClientError , ServerError ) as error : <EOL> if error . status_code == UNAUTHORIZED : <EOL> raise Unauthorized ( self . uri . string ) <EOL> if isinstance ( error , JSONResponse ) : <EOL> content = dict ( error . content , request = error . request , response = error ) <EOL> else : <EOL> content = { } <EOL> message = content . pop ( "<STR_LIT:message>" , "<STR_LIT>" % error . status_code ) <EOL> raise_from ( GraphError ( message , ** content ) , error ) <EOL> else : <EOL> return response <EOL> class ResourceTemplate ( _ResourceTemplate ) : <EOL> """<STR_LIT>""" <EOL> error_class = GraphError <EOL> def expand ( self , ** values ) : <EOL> """<STR_LIT>""" <EOL> return Resource ( self . uri_template . expand ( ** values ) ) </s>
