idx
int64
0
63k
question
stringlengths
61
4.03k
target
stringlengths
6
1.23k
8,500
def put_info ( self , key , value ) : return self . instance . put_task_info ( self . name , key , value )
Put associated information of the task .
8,501
def append_little_endian32 ( self , unsigned_value ) : if not 0 <= unsigned_value <= wire_format . UINT32_MAX : raise errors . EncodeError ( 'Unsigned 32-bit out of range: %d' % unsigned_value ) self . append_raw_bytes ( struct . pack ( wire_format . FORMAT_UINT32_LITTLE_ENDIAN , unsigned_value ) )
Appends an unsigned 32 - bit integer to the internal buffer in little - endian byte order .
8,502
def append_little_endian64 ( self , unsigned_value ) : if not 0 <= unsigned_value <= wire_format . UINT64_MAX : raise errors . EncodeError ( 'Unsigned 64-bit out of range: %d' % unsigned_value ) self . append_raw_bytes ( struct . pack ( wire_format . FORMAT_UINT64_LITTLE_ENDIAN , unsigned_value ) )
Appends an unsigned 64 - bit integer to the internal buffer in little - endian byte order .
8,503
def append_var_uint32 ( self , value ) : if not 0 <= value <= wire_format . UINT32_MAX : raise errors . EncodeError ( 'Value out of range: %d' % value ) self . append_var_uint64 ( value )
Appends an unsigned 32 - bit integer to the internal buffer encoded as a varint .
8,504
def append_varint64 ( self , value ) : if not wire_format . INT64_MIN <= value <= wire_format . INT64_MAX : raise errors . EncodeError ( 'Value out of range: %d' % value ) if value < 0 : value += ( 1 << 64 ) self . append_var_uint64 ( value )
Appends a signed 64 - bit integer to the internal buffer encoded as a varint .
8,505
def append_var_uint64 ( self , unsigned_value ) : if not 0 <= unsigned_value <= wire_format . UINT64_MAX : raise errors . EncodeError ( 'Value out of range: %d' % unsigned_value ) while True : bits = unsigned_value & 0x7f unsigned_value >>= 7 if unsigned_value : bits |= 0x80 self . _buffer . append ( bits ) if not unsi...
Appends an unsigned 64 - bit integer to the internal buffer encoded as a varint .
8,506
def groupby ( expr , by , * bys ) : if not isinstance ( by , list ) : by = [ by , ] if len ( bys ) > 0 : by = by + list ( bys ) return GroupBy ( _input = expr , _by = by )
Group collection by a series of sequences .
8,507
def value_counts ( expr , sort = True , ascending = False , dropna = False ) : names = [ expr . name , 'count' ] typos = [ expr . dtype , types . int64 ] return ValueCounts ( _input = expr , _schema = Schema . from_lists ( names , typos ) , _sort = sort , _ascending = ascending , _dropna = dropna )
Return object containing counts of unique values .
8,508
def _hashes ( self , item ) : item = self . _binary ( item ) m = hashlib . sha1 ( ) m . update ( item ) digits = m . hexdigest ( ) for i in range ( int ( self . num_hashes // 8 ) ) : m . update ( self . _binary ( str ( i ) ) ) digits += m . hexdigest ( ) hashes = [ int ( digits [ i * 5 : i * 5 + 5 ] , 16 ) % self . has...
To create the hash functions we use the SHA - 1 hash of the string and chop that up into 20 bit values and then mod down to the length of the Bloom filter .
8,509
def join ( left , right , on = None , how = 'inner' , suffixes = ( '_x' , '_y' ) , mapjoin = False ) : if on is None and not mapjoin : on = [ name for name in left . schema . names if name in right . schema . _name_indexes ] if isinstance ( suffixes , ( tuple , list ) ) and len ( suffixes ) == 2 : left_suffix , right_s...
Join two collections .
8,510
def inner_join ( left , right , on = None , suffixes = ( '_x' , '_y' ) , mapjoin = False ) : return join ( left , right , on , suffixes = suffixes , mapjoin = mapjoin )
Inner join two collections .
8,511
def left_join ( left , right , on = None , suffixes = ( '_x' , '_y' ) , mapjoin = False , merge_columns = None ) : joined = join ( left , right , on , how = 'left' , suffixes = suffixes , mapjoin = mapjoin ) return joined . _merge_joined_fields ( merge_columns )
Left join two collections .
8,512
def union ( left , right , distinct = False ) : left , right = _make_different_sources ( left , right ) return UnionCollectionExpr ( _lhs = left , _rhs = right , _distinct = distinct )
Union two collections .
8,513
def concat ( left , rights , distinct = False , axis = 0 ) : from . . utils import to_collection if isinstance ( rights , Node ) : rights = [ rights , ] if not rights : raise ValueError ( 'At least one DataFrame should be provided.' ) if axis == 0 : for right in rights : left = union ( left , right , distinct = distinc...
Concat collections .
8,514
def _drop ( expr , data , axis = 0 , columns = None ) : from . . utils import to_collection expr = to_collection ( expr ) if axis == 0 : if not isinstance ( data , ( CollectionExpr , SequenceExpr ) ) : raise ExpressionError ( 'data should be a collection or sequence when axis == 1.' ) data = to_collection ( data ) if c...
Drop data from a DataFrame .
8,515
def setdiff ( left , * rights , ** kwargs ) : import time from . . utils import output distinct = kwargs . get ( 'distinct' , False ) if isinstance ( rights [ 0 ] , list ) : rights = rights [ 0 ] cols = [ n for n in left . schema . names ] types = [ n for n in left . schema . types ] counter_col_name = 'exc_counter_%d'...
Exclude data from a collection like except clause in SQL . All collections involved should have same schema .
8,516
def intersect ( left , * rights , ** kwargs ) : import time from . . utils import output distinct = kwargs . get ( 'distinct' , False ) if isinstance ( rights [ 0 ] , list ) : rights = rights [ 0 ] cols = [ n for n in left . schema . names ] types = [ n for n in left . schema . types ] collections = ( left , ) + rights...
Calc intersection among datasets
8,517
def _verify_and_add_jwt ( ) : if not app_context_has_jwt_data ( ) : guard = current_guard ( ) token = guard . read_token_from_header ( ) jwt_data = guard . extract_jwt_token ( token ) add_jwt_data_to_app_context ( jwt_data )
This helper method just checks and adds jwt data to the app context . Will not add jwt data if it is already present . Only use in this module
8,518
def auth_required ( method ) : @ functools . wraps ( method ) def wrapper ( * args , ** kwargs ) : _verify_and_add_jwt ( ) try : return method ( * args , ** kwargs ) finally : remove_jwt_data_from_app_context ( ) return wrapper
This decorator is used to ensure that a user is authenticated before being able to access a flask route . It also adds the current user to the current flask context .
8,519
def roles_required ( * required_rolenames ) : def decorator ( method ) : @ functools . wraps ( method ) def wrapper ( * args , ** kwargs ) : role_set = set ( [ str ( n ) for n in required_rolenames ] ) _verify_and_add_jwt ( ) try : MissingRoleError . require_condition ( current_rolenames ( ) . issuperset ( role_set ) ,...
This decorator ensures that any uses accessing the decorated route have all the needed roles to access it . If an
8,520
def roles_accepted ( * accepted_rolenames ) : def decorator ( method ) : @ functools . wraps ( method ) def wrapper ( * args , ** kwargs ) : role_set = set ( [ str ( n ) for n in accepted_rolenames ] ) _verify_and_add_jwt ( ) try : MissingRoleError . require_condition ( not current_rolenames ( ) . isdisjoint ( role_set...
This decorator ensures that any uses accessing the decorated route have one of the needed roles to access it . If an
8,521
def init_app ( self , app , user_class , is_blacklisted = None ) : PraetorianError . require_condition ( app . config . get ( 'SECRET_KEY' ) is not None , "There must be a SECRET_KEY app config setting set" , ) possible_schemes = [ 'argon2' , 'bcrypt' , 'pbkdf2_sha512' , ] self . pwd_ctx = CryptContext ( default = 'pbk...
Initializes the Praetorian extension
8,522
def _validate_user_class ( cls , user_class ) : PraetorianError . require_condition ( getattr ( user_class , 'lookup' , None ) is not None , textwrap . dedent ( ) , ) PraetorianError . require_condition ( getattr ( user_class , 'identify' , None ) is not None , textwrap . dedent ( ) , ) return user_class
Validates the supplied user_class to make sure that it has the class methods necessary to function correctly .
8,523
def authenticate ( self , username , password ) : PraetorianError . require_condition ( self . user_class is not None , "Praetorian must be initialized before this method is available" , ) user = self . user_class . lookup ( username ) MissingUserError . require_condition ( user is not None , 'Could not find the reques...
Verifies that a password matches the stored password for that username . If verification passes the matching user instance is returned
8,524
def _verify_password ( self , raw_password , hashed_password ) : PraetorianError . require_condition ( self . pwd_ctx is not None , "Praetorian must be initialized before this method is available" , ) return self . pwd_ctx . verify ( raw_password , hashed_password )
Verifies that a plaintext password matches the hashed version of that password using the stored passlib password context
8,525
def encrypt_password ( self , raw_password ) : PraetorianError . require_condition ( self . pwd_ctx is not None , "Praetorian must be initialized before this method is available" , ) return self . pwd_ctx . encrypt ( raw_password , scheme = self . hash_scheme )
Encrypts a plaintext password using the stored passlib password context
8,526
def _check_user ( self , user ) : MissingUserError . require_condition ( user is not None , 'Could not find the requested user' , ) user_validate_method = getattr ( user , self . user_class_validation_method , None ) if user_validate_method is None : return InvalidUserError . require_condition ( user_validate_method ( ...
Checks to make sure that a user is valid . First checks that the user is not None . If this check fails a MissingUserError is raised . Next checks if the user has a validation method . If the method does not exist the check passes . If the method exists it is called . If the result of the call is not truthy an InvalidU...
8,527
def encode_jwt_token ( self , user , override_access_lifespan = None , override_refresh_lifespan = None , ** custom_claims ) : ClaimCollisionError . require_condition ( set ( custom_claims . keys ( ) ) . isdisjoint ( RESERVED_CLAIMS ) , "The custom claims collide with required claims" , ) self . _check_user ( user ) mo...
Encodes user data into a jwt token that can be used for authorization at protected endpoints
8,528
def encode_eternal_jwt_token ( self , user , ** custom_claims ) : return self . encode_jwt_token ( user , override_access_lifespan = VITAM_AETERNUM , override_refresh_lifespan = VITAM_AETERNUM , ** custom_claims )
This utility function encodes a jwt token that never expires
8,529
def refresh_jwt_token ( self , token , override_access_lifespan = None ) : moment = pendulum . now ( 'UTC' ) with InvalidTokenHeader . handle_errors ( 'failed to decode JWT token' ) : data = jwt . decode ( token , self . encode_key , algorithms = self . allowed_algorithms , options = { 'verify_exp' : False } , ) self ....
Creates a new token for a user if and only if the old token s access permission is expired but its refresh permission is not yet expired . The new token s refresh expiration moment is the same as the old token s but the new token s access expiration is refreshed
8,530
def extract_jwt_token ( self , token ) : with InvalidTokenHeader . handle_errors ( 'failed to decode JWT token' ) : data = jwt . decode ( token , self . encode_key , algorithms = self . allowed_algorithms , options = { 'verify_exp' : False } , ) self . _validate_jwt_data ( data , access_type = AccessType . access ) ret...
Extracts a data dictionary from a jwt token
8,531
def _validate_jwt_data ( self , data , access_type ) : MissingClaimError . require_condition ( 'jti' in data , 'Token is missing jti claim' , ) BlacklistedError . require_condition ( not self . is_blacklisted ( data [ 'jti' ] ) , 'Token has a blacklisted jti' , ) MissingClaimError . require_condition ( 'id' in data , '...
Validates that the data for a jwt token is valid
8,532
def _unpack_header ( self , headers ) : jwt_header = headers . get ( self . header_name ) MissingTokenHeader . require_condition ( jwt_header is not None , "JWT token not found in headers under '{}'" , self . header_name , ) match = re . match ( self . header_type + r'\s*([\w\.-]+)' , jwt_header ) InvalidTokenHeader . ...
Unpacks a jwt token from a request header
8,533
def pack_header_for_user ( self , user , override_access_lifespan = None , override_refresh_lifespan = None , ** custom_claims ) : token = self . encode_jwt_token ( user , override_access_lifespan = override_access_lifespan , override_refresh_lifespan = override_refresh_lifespan , ** custom_claims ) return { self . hea...
Encodes a jwt token and packages it into a header dict for a given user
8,534
def refresh ( ) : old_token = guard . read_token_from_header ( ) new_token = guard . refresh_jwt_token ( old_token ) ret = { 'access_token' : new_token } return flask . jsonify ( ret ) , 200
Refreshes an existing JWT by creating a new one that is a copy of the old except that it has a refrehsed access expiration .
8,535
def disable_user ( ) : req = flask . request . get_json ( force = True ) usr = User . query . filter_by ( username = req . get ( 'username' , None ) ) . one ( ) usr . is_active = False db . session . commit ( ) return flask . jsonify ( message = 'disabled user {}' . format ( usr . username ) )
Disables a user in the data store
8,536
def blacklist_token ( ) : req = flask . request . get_json ( force = True ) data = guard . extract_jwt_token ( req [ 'token' ] ) blacklist . add ( data [ 'jti' ] ) return flask . jsonify ( message = 'token blacklisted ({})' . format ( req [ 'token' ] ) )
Blacklists an existing JWT by registering its jti claim in the blacklist .
8,537
def protected ( ) : custom_claims = flask_praetorian . current_custom_claims ( ) firstname = custom_claims . pop ( 'firstname' , None ) nickname = custom_claims . pop ( 'nickname' , None ) surname = custom_claims . pop ( 'surname' , None ) if nickname is None : user_string = "{} {}" . format ( firstname , surname ) els...
A protected endpoint . The auth_required decorator will require a header containing a valid JWT
8,538
def current_guard ( ) : guard = flask . current_app . extensions . get ( 'praetorian' , None ) PraetorianError . require_condition ( guard is not None , "No current guard found; Praetorian must be initialized first" , ) return guard
Fetches the current instance of flask - praetorian that is attached to the current flask app
8,539
def current_user_id ( ) : jwt_data = get_jwt_data_from_app_context ( ) user_id = jwt_data . get ( 'id' ) PraetorianError . require_condition ( user_id is not None , "Could not fetch an id for the current user" , ) return user_id
This method returns the user id retrieved from jwt token data attached to the current flask app s context
8,540
def current_user ( ) : user_id = current_user_id ( ) guard = current_guard ( ) user = guard . user_class . identify ( user_id ) PraetorianError . require_condition ( user is not None , "Could not identify the current user from the current id" , ) return user
This method returns a user instance for jwt token data attached to the current flask app s context
8,541
def current_rolenames ( ) : jwt_data = get_jwt_data_from_app_context ( ) if 'rls' not in jwt_data : return set ( [ 'non-empty-but-definitely-not-matching-subset' ] ) else : return set ( r . strip ( ) for r in jwt_data [ 'rls' ] . split ( ',' ) )
This method returns the names of all roles associated with the current user
8,542
def current_custom_claims ( ) : jwt_data = get_jwt_data_from_app_context ( ) return { k : v for ( k , v ) in jwt_data . items ( ) if k not in RESERVED_CLAIMS }
This method returns any custom claims in the current jwt
8,543
def check_redirect_uris ( uris , client_type = None ) : if client_type not in [ None , 'native' , 'web' ] : raise ValueError ( 'Invalid client type indicator used' ) if not isinstance ( uris , list ) : raise ValueError ( 'uris needs to be a list of strings' ) if len ( uris ) < 1 : raise ValueError ( 'At least one retur...
This function checks all return uris provided and tries to deduce as what type of client we should register .
8,544
def register_client ( provider_info , redirect_uris ) : client_type = check_redirect_uris ( redirect_uris ) submit_info = { 'redirect_uris' : redirect_uris , 'application_type' : client_type , 'token_endpoint_auth_method' : 'client_secret_post' } headers = { 'Content-type' : 'application/json' } resp , content = httpli...
This function registers a new client with the specified OpenID Provider and then returns the regitered client ID and other information .
8,545
def discover_OP_information ( OP_uri ) : _ , content = httplib2 . Http ( ) . request ( '%s/.well-known/openid-configuration' % OP_uri ) return _json_loads ( content )
Discovers information about the provided OpenID Provider .
8,546
def init_app ( self , app ) : secrets = self . load_secrets ( app ) self . client_secrets = list ( secrets . values ( ) ) [ 0 ] secrets_cache = DummySecretsCache ( secrets ) app . config . setdefault ( 'OIDC_SCOPES' , [ 'openid' , 'email' ] ) app . config . setdefault ( 'OIDC_GOOGLE_APPS_DOMAIN' , None ) app . config ....
Do setup that requires a Flask app .
8,547
def user_getfield ( self , field , access_token = None ) : info = self . user_getinfo ( [ field ] , access_token ) return info . get ( field )
Request a single field of information about the user .
8,548
def user_getinfo ( self , fields , access_token = None ) : if g . oidc_id_token is None and access_token is None : raise Exception ( 'User was not authenticated' ) info = { } all_info = None for field in fields : if access_token is None and field in g . oidc_id_token : info [ field ] = g . oidc_id_token [ field ] elif ...
Request multiple fields of information about the user .
8,549
def get_access_token ( self ) : try : credentials = OAuth2Credentials . from_json ( self . credentials_store [ g . oidc_id_token [ 'sub' ] ] ) return credentials . access_token except KeyError : logger . debug ( "Expired ID token, credentials missing" , exc_info = True ) return None
Method to return the current requests access_token .
8,550
def get_refresh_token ( self ) : try : credentials = OAuth2Credentials . from_json ( self . credentials_store [ g . oidc_id_token [ 'sub' ] ] ) return credentials . refresh_token except KeyError : logger . debug ( "Expired ID token, credentials missing" , exc_info = True ) return None
Method to return the current requests refresh_token .
8,551
def _retrieve_userinfo ( self , access_token = None ) : if 'userinfo_uri' not in self . client_secrets : logger . debug ( 'Userinfo uri not specified' ) raise AssertionError ( 'UserInfo URI not specified' ) if '_oidc_userinfo' in g : return g . _oidc_userinfo http = httplib2 . Http ( ) if access_token is None : try : c...
Requests extra user information from the Provider s UserInfo and returns the result .
8,552
def _after_request ( self , response ) : cookie_secure = ( current_app . config [ 'OIDC_COOKIE_SECURE' ] and current_app . config . get ( 'OIDC_ID_TOKEN_COOKIE_SECURE' , True ) ) if getattr ( g , 'oidc_id_token_dirty' , False ) : if g . oidc_id_token : signed_id_token = self . cookie_serializer . dumps ( g . oidc_id_to...
Set a new ID token cookie if the ID token has changed .
8,553
def require_login ( self , view_func ) : @ wraps ( view_func ) def decorated ( * args , ** kwargs ) : if g . oidc_id_token is None : return self . redirect_to_auth_server ( request . url ) return view_func ( * args , ** kwargs ) return decorated
Use this to decorate view functions that require a user to be logged in . If the user is not already logged in they will be sent to the Provider to log in after which they will be returned .
8,554
def require_keycloak_role ( self , client , role ) : def wrapper ( view_func ) : @ wraps ( view_func ) def decorated ( * args , ** kwargs ) : pre , tkn , post = self . get_access_token ( ) . split ( '.' ) access_token = json . loads ( b64decode ( tkn ) ) if role in access_token [ 'resource_access' ] [ client ] [ 'roles...
Function to check for a KeyCloak client role in JWT access token .
8,555
def redirect_to_auth_server ( self , destination = None , customstate = None ) : if not self . _custom_callback and customstate : raise ValueError ( 'Custom State is only avilable with a custom ' 'handler' ) if 'oidc_csrf_token' not in session : csrf_token = urlsafe_b64encode ( os . urandom ( 24 ) ) . decode ( 'utf-8' ...
Set a CSRF token in the session and redirect to the IdP .
8,556
def _is_id_token_valid ( self , id_token ) : if not id_token : return False if id_token [ 'iss' ] not in current_app . config [ 'OIDC_VALID_ISSUERS' ] : logger . error ( 'id_token issued by non-trusted issuer: %s' % id_token [ 'iss' ] ) return False if isinstance ( id_token [ 'aud' ] , list ) : if self . flow . client_...
Check if id_token is a current ID token for this application was issued by the Apps domain we expected and that the email address has been verified .
8,557
def custom_callback ( self , view_func ) : @ wraps ( view_func ) def decorated ( * args , ** kwargs ) : plainreturn , data = self . _process_callback ( 'custom' ) if plainreturn : return data else : return view_func ( data , * args , ** kwargs ) self . _custom_callback = decorated return decorated
Wrapper function to use a custom callback . The custom OIDC callback will get the custom state field passed in with redirect_to_auth_server .
8,558
def _process_callback ( self , statefield ) : try : session_csrf_token = session . get ( 'oidc_csrf_token' ) state = _json_loads ( urlsafe_b64decode ( request . args [ 'state' ] . encode ( 'utf-8' ) ) ) csrf_token = state [ 'csrf_token' ] code = request . args [ 'code' ] except ( KeyError , ValueError ) : logger . debu...
Exchange the auth code for actual credentials then redirect to the originally requested page .
8,559
def validate_token ( self , token , scopes_required = None ) : valid = self . _validate_token ( token , scopes_required ) if valid is True : return True else : return ErrStr ( valid )
This function can be used to validate tokens .
8,560
def _validate_token ( self , token , scopes_required = None ) : if scopes_required is None : scopes_required = [ ] scopes_required = set ( scopes_required ) token_info = None valid_token = False has_required_scopes = False if token : try : token_info = self . _get_token_info ( token ) except Exception as ex : token_inf...
The actual implementation of validate_token .
8,561
def accept_token ( self , require_token = False , scopes_required = None , render_errors = True ) : def wrapper ( view_func ) : @ wraps ( view_func ) def decorated ( * args , ** kwargs ) : token = None if 'Authorization' in request . headers and request . headers [ 'Authorization' ] . startswith ( 'Bearer ' ) : token =...
Use this to decorate view functions that should accept OAuth2 tokens this will most likely apply to API functions .
8,562
def delete_rows_csr ( mat , indices ) : if not isinstance ( mat , scipy . sparse . csr_matrix ) : raise ValueError ( "works only for CSR format -- use .tocsr() first" ) indices = list ( indices ) mask = np . ones ( mat . shape [ 0 ] , dtype = bool ) mask [ indices ] = False return mat [ mask ]
Remove the rows denoted by indices form the CSR sparse matrix mat .
8,563
def restrict ( self , support ) : if self . has_been_restricted == True : return self new_numerical_cols = [ ] new_categorical_cols = [ ] new_additional_numerical_cols = [ ] new_feature_names = [ ] new_vocab = { } for idx , val in enumerate ( support ) : if val == True : feature_name = self . feature_names_ [ idx ] if ...
Restrict the features to those in support using feature selection .
8,564
def check_type ( self , zenpy_objects ) : expected_type = self . api . _object_mapping . class_for_type ( self . api . object_type ) if not isinstance ( zenpy_objects , collections . Iterable ) : zenpy_objects = [ zenpy_objects ] for zenpy_object in zenpy_objects : if type ( zenpy_object ) is not expected_type : raise ...
Ensure the passed type matches this API s object_type .
8,565
def to_snake_case ( name ) : s1 = FIRST_CAP_REGEX . sub ( r'\1_\2' , name ) return ALL_CAP_REGEX . sub ( r'\1_\2' , s1 ) . lower ( )
Given a name in camelCase return in snake_case
8,566
def to_unix_ts ( start_time ) : if isinstance ( start_time , datetime ) : if is_timezone_aware ( start_time ) : start_time = start_time . astimezone ( pytz . utc ) else : log . warning ( "Non timezone-aware datetime object passed to IncrementalEndpoint. " "The Zendesk API expects UTC time, if this is not the case resul...
Given a datetime object returns its value as a unix timestamp
8,567
def as_singular ( result_key ) : if result_key . endswith ( 'ies' ) : return re . sub ( 'ies$' , 'y' , result_key ) elif result_key . endswith ( 'uses' ) : return re . sub ( "uses$" , "us" , result_key ) elif result_key . endswith ( 'addresses' ) : return result_key [ : - 2 ] elif result_key . endswith ( 's' ) : return...
Given a result key return in the singular form
8,568
def as_plural ( result_key ) : if result_key . endswith ( 'y' ) : return re . sub ( "y$" , "ies" , result_key ) elif result_key . endswith ( 'address' ) : return result_key + 'es' elif result_key . endswith ( 'us' ) : return re . sub ( "us$" , "uses" , result_key ) elif not result_key . endswith ( 's' ) : return result...
Given a result key return in the plural form .
8,569
def extract_id ( * object_types ) : def outer ( func ) : def inner ( * args , ** kwargs ) : def id_of ( x ) : return x . id if type ( x ) in object_types else x new_args = [ id_of ( arg ) for arg in args ] new_kwargs = { k : id_of ( v ) for k , v in kwargs . items ( ) } return func ( * new_args , ** new_kwargs ) return...
Decorator for extracting id from passed parameters for specific types .
8,570
def json_encode ( obj , serialize ) : if hasattr ( obj , 'to_dict' ) : return obj . to_dict ( serialize = serialize ) elif isinstance ( obj , datetime ) : return obj . date ( ) . isoformat ( ) elif isinstance ( obj , date ) : return obj . isoformat ( ) elif isinstance ( obj , ProxyDict ) : return dict ( obj ) elif isin...
Handle encoding complex types .
8,571
def _call_api ( self , http_method , url , ** kwargs ) : log . debug ( "{}: {} - {}" . format ( http_method . __name__ . upper ( ) , url , kwargs ) ) if self . ratelimit is not None : response = self . _ratelimit ( http_method = http_method , url = url , ** kwargs ) else : response = http_method ( url , ** kwargs ) if ...
Execute a call to the Zendesk API . Handles rate limiting checking the response from Zendesk and deserialization of the Zendesk response . All communication with Zendesk should go through this method .
8,572
def check_ratelimit_budget ( self , seconds_waited ) : if self . ratelimit_budget is not None : self . ratelimit_budget -= seconds_waited if self . ratelimit_budget < 1 : raise RatelimitBudgetExceeded ( "Rate limit budget exceeded!" )
If we have a ratelimit_budget ensure it is not exceeded .
8,573
def _ratelimit ( self , http_method , url , ** kwargs ) : def time_since_last_call ( ) : if self . callsafety [ 'lastcalltime' ] is not None : return int ( time ( ) - self . callsafety [ 'lastcalltime' ] ) else : return None lastlimitremaining = self . callsafety [ 'lastlimitremaining' ] if time_since_last_call ( ) is ...
Ensure we do not hit the rate limit .
8,574
def _update_callsafety ( self , response ) : if self . ratelimit is not None : self . callsafety [ 'lastcalltime' ] = time ( ) self . callsafety [ 'lastlimitremaining' ] = int ( response . headers . get ( 'X-Rate-Limit-Remaining' , 0 ) )
Update the callsafety data structure
8,575
def _process_response ( self , response , object_mapping = None ) : try : pretty_response = response . json ( ) except ValueError : pretty_response = response for handler in self . _response_handlers : if handler . applies_to ( self , response ) : log . debug ( "{} matched: {}" . format ( handler . __name__ , pretty_re...
Attempt to find a ResponseHandler that knows how to process this response . If no handler can be found raise an Exception .
8,576
def _clean_dirty_objects ( self ) : if self . _dirty_object is None : return if not is_iterable_but_not_string ( self . _dirty_object ) : self . _dirty_object = [ self . _dirty_object ] log . debug ( "Cleaning objects: {}" . format ( self . _dirty_object ) ) for o in self . _dirty_object : if isinstance ( o , BaseObjec...
Clear all dirty attributes for the last object or list of objects successfully submitted to Zendesk .
8,577
def _serialize ( self , zenpy_object ) : if not type ( zenpy_object ) == dict : log . debug ( "Setting dirty object: {}" . format ( zenpy_object ) ) self . _dirty_object = zenpy_object return json . loads ( json . dumps ( zenpy_object , default = json_encode_for_zendesk ) )
Serialize a Zenpy object to JSON
8,578
def _query_zendesk ( self , endpoint , object_type , * endpoint_args , ** endpoint_kwargs ) : _id = endpoint_kwargs . get ( 'id' , None ) if _id : item = self . cache . get ( object_type , _id ) if item : return item else : return self . _get ( url = self . _build_url ( endpoint ( * endpoint_args , ** endpoint_kwargs )...
Query Zendesk for items . If an id or list of ids are passed attempt to locate these items in the relevant cache . If they cannot be found or no ids are passed execute a call to Zendesk to retrieve the items .
8,579
def _check_response ( self , response ) : if response . status_code > 299 or response . status_code < 200 : log . debug ( "Received response code [%s] - headers: %s" % ( response . status_code , str ( response . headers ) ) ) try : _json = response . json ( ) err_type = _json . get ( "error" , '' ) if err_type == 'Reco...
Check the response code returned by Zendesk . If it is outside the 200 range raise an exception of the correct type .
8,580
def _build_url ( self , endpoint ) : if not issubclass ( type ( self ) , ChatApiBase ) and not self . subdomain : raise ZenpyException ( "subdomain is required when accessing the Zendesk API!" ) if self . subdomain : endpoint . netloc = '{}.{}' . format ( self . subdomain , self . domain ) else : endpoint . netloc = se...
Build complete URL
8,581
def tags ( self , ticket_id ) : return self . _query_zendesk ( self . endpoint . tags , 'tag' , id = ticket_id )
Lists the most popular recent tags in decreasing popularity from a specific ticket .
8,582
def incremental ( self , start_time , include = None ) : return self . _query_zendesk ( self . endpoint . incremental , self . object_type , start_time = start_time , include = include )
Retrieve bulk data from the incremental API .
8,583
def incremental ( self , start_time , ** kwargs ) : return self . _query_zendesk ( self . endpoint . incremental , self . object_type , start_time = start_time , ** kwargs )
Retrieve bulk data from the chat incremental API .
8,584
def show ( self , user , identity ) : url = self . _build_url ( self . endpoint . show ( user , identity ) ) return self . _get ( url )
Show the specified identity for the specified user .
8,585
def update ( self , user , identity ) : return UserIdentityRequest ( self ) . put ( self . endpoint . update , user , identity )
Update specified identity for the specified user
8,586
def make_primary ( self , user , identity ) : return UserIdentityRequest ( self ) . put ( self . endpoint . make_primary , user , identity )
Set the specified user as primary for the specified user .
8,587
def request_verification ( self , user , identity ) : return UserIdentityRequest ( self ) . put ( self . endpoint . request_verification , user , identity )
Sends the user a verification email with a link to verify ownership of the email address .
8,588
def verify ( self , user , identity ) : return UserIdentityRequest ( self ) . put ( self . endpoint . verify , user , identity )
Verify an identity for a user
8,589
def groups ( self , user , include = None ) : return self . _query_zendesk ( self . endpoint . groups , 'group' , id = user , include = include )
Retrieve the groups for this user .
8,590
def organizations ( self , user , include = None ) : return self . _query_zendesk ( self . endpoint . organizations , 'organization' , id = user , include = include )
Retrieve the organizations for this user .
8,591
def requested ( self , user , include = None ) : return self . _query_zendesk ( self . endpoint . requested , 'ticket' , id = user , include = include )
Retrieve the requested tickets for this user .
8,592
def cced ( self , user , include = None ) : return self . _query_zendesk ( self . endpoint . cced , 'ticket' , id = user , include = include )
Retrieve the tickets this user is cc d into .
8,593
def assigned ( self , user , include = None ) : return self . _query_zendesk ( self . endpoint . assigned , 'ticket' , id = user , include = include )
Retrieve the assigned tickets for this user .
8,594
def group_memberships ( self , user , include = None ) : return self . _query_zendesk ( self . endpoint . group_memberships , 'group_membership' , id = user , include = include )
Retrieve the group memberships for this user .
8,595
def related ( self , user ) : return self . _query_zendesk ( self . endpoint . related , 'user_related' , id = user )
Returns the UserRelated information for the requested User
8,596
def me ( self , include = None ) : return self . _query_zendesk ( self . endpoint . me , 'user' , include = include )
Return the logged in user
8,597
def user_fields ( self , user ) : return self . _query_zendesk ( self . endpoint . user_fields , 'user_field' , id = user )
Retrieve the user fields for this user .
8,598
def organization_memberships ( self , user ) : return self . _query_zendesk ( self . endpoint . organization_memberships , 'organization_membership' , id = user )
Retrieve the organization memberships for this user .
8,599
def upload ( self , fp , token = None , target_name = None , content_type = None ) : return UploadRequest ( self ) . post ( fp , token = token , target_name = target_name , content_type = content_type )
Upload a file to Zendesk .