Example #1
0
 def _shared_to(self, context, data, delete, replace):
     key = context == "email" and "address" or "id"
     params = MultiDict()
     for value in data:
         params.add("%s[][%s]" % (context, key), value)
     return self._subresource_dispatcher(
         "shared-to/%s" % context,
         scid=self.scid,
         delete=delete,
         postdata=replace and params or None,
         putdata=not replace and params or None,
         allowed_methods=["GET", "DELETE", "PUT", "POST"],
     )
Example #2
0
 def _shared_to(self, context, data, delete, replace):
     key = context == 'email' and 'address' or 'id'
     params = MultiDict()
     for value in data:
         params.add('%s[][%s]' % (context, key), value)
     return self._subresource_dispatcher(
         'shared-to/%s' % context,
         scid=scid,
         delete=delete,
         postdata=replace and params or None,
         putdata=not replace and params or None,
         allowed_methods=['GET', 'DELETE', 'PUT', 'POST', ]
     )
Example #3
0
    def request(self, method, path=None, payload=None, headers=None,
                params_dict=None, raw=False, **params):

        headers = MultiDict(headers or {})
        if payload is not None and not headers.iget('content-type'):
            headers.update({'Content-Type': self.default_content_type})

        # Default Keep-Alive is set to 4 seconds in TMOS.
        if self.no_keepalive:
            headers.update({'Connection': 'Close'})

        if path is not None:
            if self.trailing_slash and path[-1] != '/':
                path += '/'

        if payload is not None and headers and headers.iget('Content-Type'):
            mimetype = RAW_MIMETYPE if raw \
                else mimetype_from_headers(headers)
            payload = BaseRestResource._parse(mimetype, payload)

        if self.default_params is not None:
            condition = True
            if self.default_params_condition is not None:
                condition = self.default_params_condition(path)
            if condition:
                params_dict = params_dict or {}
                temp_dict = self.default_params.copy()
                temp_dict.update(params_dict)
                params_dict = temp_dict

        wrapped_response = None
        try:
            # If we want to save timing information, start the timer
            if self.save_timings:
                start_time = time.time()
            response = super(BaseRestResource, self).request(method, path=path,
                                                             payload=payload,
                                                             headers=headers,
                                                             params_dict=params_dict,
                                                             **params)
            wrapped_response = WrappedResponse(response, raw=raw)
        except ResourceError as e:
            raise e
        finally:
            # If we want to save timing information, compute and save the total
            # time. If there was an exception thrown during the HTTP call, then
            # we won't save the timing info, which is okay.
            # The saved timing information is in seconds, with both whole and
            # fractional values, i.e. 3.13.
            if self.save_timings:
                end_time = time.time()
                total_time = end_time - start_time
                timing_info[method].append(total_time)
                if BasicProfiler.state == BasicProfilerState.enabled:
                    BasicProfiler.save_result(path, req_type=method, start_time=start_time, end_time=end_time)

        return wrapped_response
Example #4
0
 def _shared_to(self, context, data, delete, replace):
     key = context == 'email' and 'address' or 'id'
     params = MultiDict()
     for value in data:
         params.add('%s[][%s]' % (context, key), value)
     return self._subresource_dispatcher('shared-to/%s' % context,
                                         scid=scid,
                                         delete=delete,
                                         postdata=replace and params
                                         or None,
                                         putdata=not replace and params
                                         or None,
                                         allowed_methods=[
                                             'GET',
                                             'DELETE',
                                             'PUT',
                                             'POST',
                                         ])
Example #5
0
    def request(self,
                method,
                path=None,
                payload=None,
                headers=None,
                params_dict=None,
                raw=False,
                **params):

        headers = MultiDict(headers or {})
        if payload is not None and not headers.iget('content-type'):
            headers.update({'Content-Type': self.default_content_type})

        # Default Keep-Alive is set to 4 seconds in TMOS.
        if self.no_keepalive:
            headers.update({'Connection': 'Close'})

        if path is not None:
            if self.trailing_slash and path[-1] != '/':
                path += '/'

        if payload is not None and headers and headers.iget('Content-Type'):
            mimetype = RAW_MIMETYPE if raw \
                else mimetype_from_headers(headers)
            payload = BaseRestResource._parse(mimetype, payload)

        if self.default_params is not None:
            condition = True
            if self.default_params_condition is not None:
                condition = self.default_params_condition(path)
            if condition:
                params_dict = params_dict or {}
                temp_dict = self.default_params.copy()
                temp_dict.update(params_dict)
                params_dict = temp_dict

        wrapped_response = None
        try:
            # If we want to save timing information, start the timer
            if self.save_timings:
                start_time = time.time()
            response = super(BaseRestResource,
                             self).request(method,
                                           path=path,
                                           payload=payload,
                                           headers=headers,
                                           params_dict=params_dict,
                                           **params)
            wrapped_response = WrappedResponse(response, raw=raw)
        except ResourceError, e:
            raise e
Example #6
0
def soundcloud_flat_dict(data, baseprefix):
    new = MultiDict()

    def _flattening(current, prefix):
        # stream handling!
        if hasattr(current,
                   'read'):  # some kind of filelike, has also __iter__!
            new.add(prefix, current)
        elif hasattr(current, 'items'):  # some kind of dict
            for key, value in current.items():
                _flattening(value, '%s[%s]' % (prefix, key))
        elif hasattr(current, '__iter__'):  # some kind of list
            for listvalue in current:
                _flattening(listvalue, '%s[]' % prefix)
        else:
            new.add(prefix, current)

    _flattening(data, baseprefix)
    return new
Example #7
0
    def request(self, method, path=None, payload=None, headers=None,
                params_dict=None, raw=False, **params):

        headers = MultiDict(headers or {})
        if payload is not None and not headers.iget('content-type'):
            headers.update({'Content-Type': self.default_content_type})

        # Default Keep-Alive is set to 4 seconds in TMOS.
        if self.no_keepalive:
            headers.update({'Connection': 'Close'})

        if path is not None:
            if self.trailing_slash and path[-1] != '/':
                path += '/'

        if payload is not None and headers and headers.iget('Content-Type'):
            mimetype = RAW_MIMETYPE if raw \
                else mimetype_from_headers(headers)
            payload = BaseRestResource._parse(mimetype, payload)

        if self.default_params is not None:
            condition = True
            if self.default_params_condition is not None:
                condition = self.default_params_condition(path)
            if condition:
                params_dict = params_dict or {}
                params_dict.update(self.default_params)

        wrapped_response = response = None
        try:
            response = super(BaseRestResource, self).request(method, path=path,
                                                             payload=payload,
                                                             headers=headers,
                                                             params_dict=params_dict,
                                                             **params)
            wrapped_response = WrappedResponse(response, raw=raw)
        except ResourceError, e:
            response = e.response
            raise e
Example #8
0
 def _headers__set(self, value):
     self._headers = MultiDict(copy.copy(value))
Example #9
0
 def _headers__get(self):
     if not isinstance(self._headers, MultiDict):
         self._headers = MultiDict(self._headers or [])
     return self._headers
Example #10
0
 def same(self, resp, sizer, matcher, exp):
     t.eq(resp.status, exp["status"])
     t.eq(resp.version, exp["version"])
     t.eq(resp.headers, MultiDict(exp["headers"]))
     matcher(resp, exp["body"], sizer)
     t.eq(resp.trailers, exp.get("trailers", []))