Пример #1
0
def make_request_log_message(**args):
    '''
    Creates a string containing all relevant information
    about a request made to the Handle System, for
    logging purposes.

    :handle: The handle that the request is about.
    :url: The url the request is sent to.
    :headers: The headers sent along with the request.
    :verify: Boolean parameter passed to the requests
        module (https verification).
    :resp: The request's response.
    :op: The library operation during which the request
        was sent.
    :payload: Optional. The payload sent with the request.
    :return: A formatted string.

    '''

    mandatory_args = ['op', 'handle', 'url', 'headers', 'verify', 'resp']
    optional_args = ['payload']
    util.check_presence_of_mandatory_args(args, mandatory_args)
    util.add_missing_optional_args_with_value_none(args, optional_args)

    space = '\n   '
    message = ''
    message += '\n'+args['op']+' '+args['handle']
    message += space+'URL:          '+args['url']
    message += space+'HEADERS:      '+str(args['headers'])
    message += space+'VERIFY:       '+str(args['verify'])
    if 'payload' in args.keys():
        message += space+'PAYLOAD:'+space+str(args['payload'])
    message += space+'RESPONSECODE: '+str(args['resp'].status_code)
    message += space+'RESPONSE:'+space+str(args['resp'].content)
    return message
Пример #2
0
    def send_handle_delete_request(self, **args):
        '''
        Send a HTTP DELETE request to the handle server to delete either an
            entire handle or to some specified values from a handle record,
            using the requests module.

        :param handle: The handle.
        :param indices: Optional. A list of indices to delete. Defaults to
            None (i.e. the entire handle is deleted.). The list can contain
            integers or strings.
        :return: The server's response.
        '''

        # Check if we have write access at all:
        if not self.__has_write_access:
            raise HandleAuthenticationError(msg=self.__no_auth_message)

        # Check args:
        mandatory_args = ['handle']
        optional_args = ['indices', 'op']
        util.add_missing_optional_args_with_value_none(args, optional_args)
        util.check_presence_of_mandatory_args(args, mandatory_args)
        handle = args['handle']
        indices = args['indices']
        op = args['op']

        # Make necessary values:
        url = self.make_handle_URL(handle, indices)
        if indices is not None and len(indices) > 0:
            LOGGER.debug('__send_handle_delete_request: Deleting values '+str(indices)+' from handle '+handle+'.')
        else:
            LOGGER.debug('__send_handle_delete_request: Deleting handle '+handle+'.')
        LOGGER.debug('DELETE Request to '+url)
        head = self.__get_headers('DELETE')
        veri = self.__HTTPS_verify

        # Make request:
        resp = None
        if self.__authentication_method == self.__auth_methods['user_pw']:
            resp = self.__session.delete(url, headers=head, verify=veri)
        elif self.__authentication_method == self.__auth_methods['cert']:
            resp = self.__session.delete(url, headers=head, verify=veri, cert=self.__cert_object)
        self.__log_request_response_to_file(
            logger=REQUESTLOGGER,
            op='DELETE',
            handle=handle,
            url=url,
            headers=head,
            verify=veri,
            resp=resp
        )

        # Check response for authentication issues:
        if hsresponses.not_authenticated(resp):
            raise HandleAuthenticationError(
                operation=op,
                handle=handle,
                response=resp,
                username=self.__username
            )

        return resp
Пример #3
0
    def send_handle_put_request(self, **args):
        '''
        Send a HTTP PUT request to the handle server to write either an entire
            handle or to some specified values to an handle record, using the
            requests module.

        :param handle: The handle.
        :param list_of_entries: A list of handle record entries to be written,
         in the format [{"index":xyz, "type":"xyz", "data":"xyz"}] or similar.
        :param indices: Optional. A list of indices to delete. Defaults
         to None (i.e. the entire handle is deleted.). The list can
         contain integers or strings.
        :param overwrite: Optional. Whether the handle should be overwritten
         if it exists already.
        :return: The server's response.
        '''

        # Check if we have write access at all:
        if not self.__has_write_access:
            raise HandleAuthenticationError(msg=self.__no_auth_message)

        # Check args:
        mandatory_args = ['handle', 'list_of_entries']
        optional_args = ['indices', 'op', 'overwrite']
        util.add_missing_optional_args_with_value_none(args, optional_args)
        util.check_presence_of_mandatory_args(args, mandatory_args)
        handle = args['handle']
        list_of_entries = args['list_of_entries']
        indices = args['indices']
        op = args['op']
        overwrite = args['overwrite'] or False

        # Overwrite by index:
        if indices is not None:
            message = 'Writing handle values by index is not implemented'+\
                ' yet because the way the indices are interpreted by the'+\
                ' Handle Server may be modified soon. The entire handle'+\
                ' record has to be overwritten.'
            raise NotImplementedError(message)
            # TODO FIXME: As soon as the Handle System uses the correct indices
            # for overwriting, this may be implemented.
            # In HSv8 beta, the HS uses ?index=3 for overwriting index:4. If the
            # library used this and then the behaviour is changed, it would lead
            # to corrupt handle records, so we wait until the issue is fixed by
            # the Handle System.

        # Make necessary values:
        url = self.make_handle_URL(handle, overwrite=overwrite)
        LOGGER.debug('PUT Request to '+url)
        payload = json.dumps({'values':list_of_entries})
        LOGGER.debug('PUT Request payload: '+payload)
        head = self.__get_headers('PUT')
        veri = self.__HTTPS_verify

        # Make request:
        resp = None
        if self.__authentication_method == self.__auth_methods['user_pw']:
            resp = self.__session.put(url, data=payload, headers=head, verify=veri)
        elif self.__authentication_method == self.__auth_methods['cert']:
            resp = self.__session.put(url, data=payload, headers=head, verify=veri, cert=self.__cert_object)
        self.__log_request_response_to_file(
            logger=REQUESTLOGGER,
            op='PUT',
            handle=handle,
            url=url,
            headers=head,
            verify=veri,
            resp=resp,
            payload=payload)

        # Check response for authentication issues:
        if hsresponses.not_authenticated(resp):
            raise HandleAuthenticationError(
                operation=op,
                handle=handle,
                response=resp,
                username=self.__username
            )

        return resp, payload