def encode_params(self, data=None, **kwargs): """ Build the body for a text/plain request. Will successfully encode parameters when passed as a dict or a list of 2-tuples. Order is retained if data is a list of 2-tuples but arbitrary if parameters are supplied as a dict. """ charset = kwargs.get("charset", self.charset) collection_format = kwargs.get("collection_format", self.collection_format) output_str = kwargs.get("output_str", self.output_str) if data is None: return b"", self.get_content_type(charset) elif isinstance(data, bytes): return data, self.get_content_type(charset) elif isinstance(data, str): return self._encode(data, charset), self.get_content_type(charset) elif hasattr(data, 'read'): return data, self.get_content_type(charset) elif collection_format == 'multi' and hasattr(data, '__iter__'): result = [] for k, vs in to_key_val_list(data): if isinstance(vs, basestring) or not hasattr(vs, '__iter__'): vs = [vs] for v in vs: result.append(b"=".join([ self._encode(k, charset), self._encode(v, charset, output_str) ])) return b'\n'.join(result), self.get_content_type(charset) elif collection_format == 'plain' and hasattr(data, '__iter__'): results = [] for k, vs in to_key_val_dict(data).items(): results.append(b"=".join([ self._encode(k, charset), self._encode(vs, charset, output_str) ])) return b'\n'.join(results), self.get_content_type(charset) elif hasattr(data, '__iter__'): results = [] for k, vs in to_key_val_dict(data).items(): if isinstance(vs, list): v = self.COLLECTION_SEPARATORS[collection_format].join( e for e in vs) key = k + '[]' else: v = vs key = k results.append(b"=".join([ self._encode(key, charset), self._encode(v, charset, output_str) ])) return b"\n".join(results), self.get_content_type(charset) else: return str(data).encode(charset) if charset else str(data),\ self.get_content_type(charset)
def encode_params(self, data=None, **kwargs): """ Build the body for a text/plain request. Will successfully encode parameters when passed as a dict or a list of 2-tuples. Order is retained if data is a list of 2-tuples but arbitrary if parameters are supplied as a dict. """ charset = kwargs.get("charset", self.charset) collection_format = kwargs.get("collection_format", self.collection_format) output_str = kwargs.get("output_str", self.output_str) if data is None: return "", self.get_content_type(charset) elif isinstance(data, (str, bytes)): return data, self.get_content_type(charset) elif hasattr(data, 'read'): return data, self.get_content_type(charset) elif collection_format == 'multi' and hasattr(data, '__iter__'): result = [] for k, vs in to_key_val_list(data): if isinstance(vs, basestring) or not hasattr(vs, '__iter__'): vs = [vs] for v in vs: result.append(b"=".join([self._encode(k, charset), self._encode(v, charset, output_str)])) return b'\n'.join(result), self.get_content_type(charset) elif collection_format == 'plain' and hasattr(data, '__iter__'): results = [] for k, vs in to_key_val_dict(data).items(): results.append(b"=".join([self._encode(k, charset), self._encode(vs, charset, output_str)])) return b'\n'.join(results), self.get_content_type(charset) elif hasattr(data, '__iter__'): results = [] for k, vs in to_key_val_dict(data).items(): if isinstance(vs, list): v = self.COLLECTION_SEPARATORS[collection_format].join(e for e in vs) key = k + '[]' else: v = vs key = k results.append(b"=".join([self._encode(key, charset), self._encode(v, charset, output_str)])) return b"\n".join(results), self.get_content_type(charset) else: return str(data).encode(charset) if charset else str(data), self.get_content_type(charset)
def encode_params(self, data=None, **kwargs): """ Build the body for a application/json request. """ if isinstance(data, basestring): raise ValueError("Data must not be a string.") if data is None: return "", self.content_type fields = to_key_val_dict(data or "") try: body = json.dumps(fields) except: body = json.dumps(fields, encoding='latin-1') return body, self.content_type
def encode_params(self, data=None, **kwargs): """ Build the body for a application/json request. """ if isinstance(data, basestring): raise ValueError("Data must not be a string.") if data is None: return b"", self.content_type fields = to_key_val_dict(data or "") try: body = json.dumps(fields) except: body = json.dumps(fields, encoding='latin-1') return str(body).encode(), self.content_type
def encode_params(self, data=None, **kwargs): """ Encode parameters in a piece of data. Will successfully encode parameters when passed as a dict or a list of 2-tuples. Order is retained if data is a list of 2-tuples but arbitrary if parameters are supplied as a dict. """ collection_format = kwargs.get("collection_format", self.collection_format) output_str = kwargs.get("output_str", self.output_str) sort = kwargs.get("sort", self.sort) if data is None: return "", self.content_type elif isinstance(data, (str, bytes)): return data, self.content_type elif hasattr(data, 'read'): return data, self.content_type elif collection_format == 'multi' and hasattr(data, '__iter__'): result = [] for k, vs in to_key_val_list(data, sort=sort): if isinstance(vs, basestring) or not hasattr(vs, '__iter__'): vs = [vs] for v in vs: result.append( (k.encode('utf-8') if isinstance(k, str) else k, v.encode('utf-8') if isinstance(v, str) else to_string(v, lang=output_str))) return urlencode(result, doseq=True), self.content_type elif collection_format == 'encoded' and hasattr(data, '__iter__'): return urlencode(data, doseq=False), self.content_type elif hasattr(data, '__iter__'): results = [] for k, vs in to_key_val_dict(data).items(): if isinstance(vs, list): v = self.COLLECTION_SEPARATORS[collection_format].join( quote_plus(e) for e in vs) key = k + '[]' else: v = quote_plus(vs) key = k results.append("%s=%s" % (key, v)) return '&'.join(results), self.content_type else: return data, self.content_type
def encode_params(self, data=None, **kwargs): """ Encode parameters in a piece of data. Will successfully encode parameters when passed as a dict or a list of 2-tuples. Order is retained if data is a list of 2-tuples but arbitrary if parameters are supplied as a dict. """ collection_format = kwargs.get("collection_format", self.collection_format) output_str = kwargs.get("output_str", self.output_str) sort = kwargs.get("sort", self.sort) if data is None: return "", self.content_type elif isinstance(data, (str, bytes)): return data, self.content_type elif hasattr(data, 'read'): return data, self.content_type elif collection_format == 'multi' and hasattr(data, '__iter__'): result = [] for k, vs in to_key_val_list(data, sort=sort): if isinstance(vs, basestring) or not hasattr(vs, '__iter__'): vs = [vs] for v in vs: result.append( (k.encode('utf-8') if isinstance(k, str) else k, v.encode('utf-8') if isinstance(v, str) else to_string(v, lang=output_str))) return urlencode(result, doseq=True), self.content_type elif collection_format == 'encoded' and hasattr(data, '__iter__'): return urlencode(data, doseq=False), self.content_type elif hasattr(data, '__iter__'): results = [] for k, vs in to_key_val_dict(data).items(): if isinstance(vs, list): v = self.COLLECTION_SEPARATORS[collection_format].join(quote_plus(e) for e in vs) key = k + '[]' else: v = quote_plus(vs) key = k results.append("%s=%s" % (key, v)) return '&'.join(results), self.content_type else: return data, self.content_type
def test_to_key_val_dict_exception(self): try: to_key_val_dict(1) self.assertTrue(False) except ValueError: pass
def test_to_key_val_dict_none(self): res = to_key_val_dict(None) self.assertEqual(res, {})
def test_to_key_val_dict_tuple_list_three_elements(self): json_obj = [("Id", 0), ("key2", ""), ("key2", "val"), ("key2", "val2")] res = to_key_val_dict(json_obj) self.assertEqual(res, {"Id": 0, "key2": ["", "val", "val2"]})
def test_to_key_val_dict_tuple_list_array_append(self): json_obj = [("Id", 0), ("key2", [""]), ("key2", "val")] res = to_key_val_dict(json_obj) self.assertEqual(res, {"Id": 0, "key2": ["", "val"]})
def test_to_key_val_dict_tuple_list(self): json_obj = [("Id", 0), ("key2", ""), ("key3", "")] res = to_key_val_dict(json_obj) self.assertEqual(res, {"Id": 0, "key2": "", "key3": ""})
def test_to_key_val_dict_json(self): json_obj = {"Id": 0, "key2": "", "key3": ""} res = to_key_val_dict(json_obj) self.assertEqual(res, {"Id": 0, "key2": "", "key3": ""})
def test_to_key_val_dict_invalid_array_of_dicts(self): try: to_key_val_dict([{"a": 1}, {"b": 2}]) self.assertTrue(False) except ValueError: pass
def test_to_key_val_dict_invalid_number_of_items(self): try: to_key_val_dict([(1, 2, 3), (1, 2, 3)]) self.assertTrue(False) except ValueError: pass
def test_to_key_val_dict_tuple_list_array(self): json_obj = [("Id", 0), ("key2", ""), ("key2", "val")] res = to_key_val_dict(json_obj) self.assertEqual(res, {"Id": 0, "key2": ["", "val"]})