<s> import hashlib <EOL> import random <EOL> import string <EOL> import time <EOL> from libcloud . common . base import ConnectionUserAndKey <EOL> from libcloud . common . base import JsonResponse <EOL> from libcloud . common . types import InvalidCredsError , ProviderError <EOL> from libcloud . utils . py3 import basestring , httplib , urlencode <EOL> SALT_CHARACTERS = string . ascii_letters + string . digits <EOL> class NFSNException ( ProviderError ) : <EOL> def __init__ ( self , value , http_code , code , driver = None ) : <EOL> self . code = code <EOL> super ( NFSNException , self ) . __init__ ( value , http_code , driver ) <EOL> class NFSNResponse ( JsonResponse ) : <EOL> def parse_error ( self ) : <EOL> if self . status == httplib . UNAUTHORIZED : <EOL> raise InvalidCredsError ( '<STR_LIT>' ) <EOL> body = self . parse_body ( ) <EOL> if isinstance ( body , basestring ) : <EOL> return body + '<STR_LIT>' % self . status <EOL> error = body . get ( '<STR_LIT:error>' , None ) <EOL> debug = body . get ( '<STR_LIT>' , None ) <EOL> value = '<STR_LIT>' <EOL> if error is not None : <EOL> value = error <EOL> if debug is not None : <EOL> value = debug <EOL> if error is not None and value is not None : <EOL> value = error + '<STR_LIT:U+0020>' + value <EOL> value = value + '<STR_LIT>' % self . status <EOL> return value <EOL> class NFSNConnection ( ConnectionUserAndKey ) : <EOL> host = '<STR_LIT>' <EOL> responseCls = NFSNResponse <EOL> allow_insecure = False <EOL> def _header ( self , action , data ) : <EOL> """<STR_LIT>""" <EOL> login = self . user_id <EOL> timestamp = self . _timestamp ( ) <EOL> salt = self . _salt ( ) <EOL> api_key = self . key <EOL> data = urlencode ( data ) <EOL> data_hash = hashlib . sha1 ( data . encode ( '<STR_LIT:utf-8>' ) ) . hexdigest ( ) <EOL> string = '<STR_LIT:;>' . join ( ( login , timestamp , salt , api_key , action , data_hash ) ) <EOL> string_hash = hashlib . sha1 ( string . encode ( '<STR_LIT:utf-8>' ) ) . hexdigest ( ) <EOL> return '<STR_LIT:;>' . join ( ( login , timestamp , salt , string_hash ) ) <EOL> def request ( self , action , params = None , data = '<STR_LIT>' , headers = None , <EOL> method = '<STR_LIT:GET>' ) : <EOL> """<STR_LIT>""" <EOL> if not headers : <EOL> headers = { } <EOL> if not params : <EOL> params = { } <EOL> header = self . _header ( action , data ) <EOL> headers [ '<STR_LIT>' ] = header <EOL> if method == '<STR_LIT:POST>' : <EOL> headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT>' <EOL> return ConnectionUserAndKey . request ( self , action , params , data , <EOL> headers , method ) <EOL> def encode_data ( self , data ) : <EOL> """<STR_LIT>""" <EOL> if data : <EOL> data = urlencode ( data ) <EOL> return data <EOL> def _salt ( self ) : <EOL> """<STR_LIT>""" <EOL> r = random . SystemRandom ( ) <EOL> return '<STR_LIT>' . join ( r . choice ( SALT_CHARACTERS ) for _ in range ( <NUM_LIT:16> ) ) <EOL> def _timestamp ( self ) : <EOL> """<STR_LIT>""" <EOL> return str ( int ( time . time ( ) ) ) </s>
