def __init__(self, options=None, **kwargs):
     if not options:
         self._options = HttpOptions()
     elif not (isinstance(options, HttpOptions)):
         raise TypeError("options must implement base.HttpOptions")
     else:
         self._options = options
Beispiel #2
0
 def __init__(self, options=None, **kwargs):
     if not options:
         self._options = HttpOptions()
     elif not (isinstance(options, HttpOptions)):
         raise_with_traceback(
             TypeError('options must implement base.HttpOptions'))
     else:
         self._options = options
Beispiel #3
0
class HttpRequestor(EventStoreRequestor):
    def __init__(self, options=None, **kwargs):
        if not options:
            self._options = HttpOptions()
        elif not (isinstance(options, HttpOptions)):
            raise_with_traceback(
                TypeError('options must implement base.HttpOptions'))
        else:
            self._options = options

    def _dispatch(self,
                  caliper_objects=None,
                  described_objects=None,
                  sensor_id=None):
        results = []
        identifiers = []

        if isinstance(caliper_objects, collections.MutableSequence):
            s = requests.Session()
            payload, ids = self._generate_payload(
                caliper_objects=caliper_objects,
                described_objects=described_objects,
                optimize=self._options.OPTIMIZE_SERIALIZATION,
                sensor_id=sensor_id)
            r = s.post(self._options.HOST,
                       data=payload['data'],
                       headers={
                           'Authorization':
                           self._options.get_auth_header_value(),
                           'Content-Type': payload['type']
                       },
                       timeout=self._options.CONNECTION_TIMEOUT)
            if ((r.status_code is requests.codes.ok)
                    or (r.status_code is requests.codes.created)):
                v = True
                identifiers += ids
            else:
                v = False
            results += len(caliper_objects) * [v]
            s.close()

        return results, identifiers

    def describe(self, caliper_entity_list=None, sensor_id=None):
        results, ids = self._dispatch(caliper_objects=caliper_entity_list,
                                      sensor_id=sensor_id)
        return results, ids

    def send(self,
             caliper_event_list=None,
             described_objects=None,
             sensor_id=None):
        results, ids = self._dispatch(caliper_objects=caliper_event_list,
                                      described_objects=described_objects,
                                      sensor_id=sensor_id)
        return results, ids
 def __init__(self, config_options=None, sensor_id=None):
     if not config_options:
         self._config = HttpOptions(optimize_serialization=True)
     elif not (isinstance(config_options, HttpOptions)):
         raise TypeError("config_options must implement HttpOptions")
     else:
         self._config = config_options
     self._id = sensor_id
     self._requestor = HttpRequestor(options=self._config)
     self._stats = SimpleStatistics()
     self._status_code = None
     self._debug = []
Beispiel #5
0
def build_sensor_from_config(config_options=None, sensor_id=None):
    return Sensor.fashion_sensor_with_config(
        config_options=config_options
        or HttpOptions(optimize_serialization=True),
        sensor_id=sensor_id)
Beispiel #6
0
def build_default_sensor(sensor_id=None):
    return Sensor.fashion_sensor_with_config(
        config_options=HttpOptions(optimize_serialization=True),
        sensor_id=sensor_id)
class HttpRequestor(EventStoreRequestor):
    def __init__(self, options=None, **kwargs):
        if not options:
            self._options = HttpOptions()
        elif not (isinstance(options, HttpOptions)):
            raise TypeError("options must implement base.HttpOptions")
        else:
            self._options = options

    def _dispatch(self,
                  caliper_objects=None,
                  described_objects=None,
                  sensor_id=None,
                  debug=False):
        results = []
        identifiers = []
        response = None

        if isinstance(caliper_objects, MutableSequence):
            s = requests.Session()
            payload, ids = self._generate_payload(
                caliper_objects=caliper_objects,
                described_objects=described_objects,
                optimize=self._options.OPTIMIZE_SERIALIZATION,
                sensor_id=sensor_id,
            )
            hdrs = {"Content-Type": payload["type"]}
            if self._options.get_auth_header_value():
                hdrs.update(
                    {"Authorization": self._options.get_auth_header_value()})
            r = s.post(self._options.HOST, data=payload["data"], headers=hdrs)
            if (r.status_code is requests.codes.ok) or (
                    r.status_code is requests.codes.created):
                v = True
                identifiers += ids
            else:
                v = False
            results += len(caliper_objects) * [v]
            if debug:
                response = r
            s.close()

        return results, identifiers, response

    def _get_config(self):
        s = requests.Session()
        hdrs = {}
        if self._options.get_auth_header_value():
            hdrs.update(
                {"Authorization": self._options.get_auth_header_value()})
        r = s.get(self._options.HOST, headers=hdrs)
        if r.status_code is requests.codes.ok:
            return r.json()
        else:
            return None

    def describe(self, caliper_entity_list=None, sensor_id=None, debug=False):
        results, ids, response = self._dispatch(
            caliper_objects=caliper_entity_list,
            sensor_id=sensor_id,
            debug=debug)
        return results, ids, response

    def get_config(self):
        return self._get_config()

    def send(
        self,
        caliper_event_list=None,
        described_objects=None,
        sensor_id=None,
        debug=False,
    ):
        results, ids, response = self._dispatch(
            caliper_objects=caliper_event_list,
            described_objects=described_objects,
            sensor_id=sensor_id,
            debug=debug,
        )
        return results, ids, response