Skip to content

Commit

Permalink
Fix multipart encoding of nested parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe committed Apr 5, 2019
1 parent ea36406 commit eabbcd9
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 10 deletions.
21 changes: 21 additions & 0 deletions pip-wheel-metadata/stripe.dist-info/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License

Copyright (c) 2010-2018 Stripe (http://stripe.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
68 changes: 68 additions & 0 deletions pip-wheel-metadata/stripe.dist-info/METADATA
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
Metadata-Version: 2.1
Name: stripe
Version: 2.24.0
Summary: Python bindings for the Stripe API
Home-page: https://github.com/stripe/stripe-python
Author: Stripe
Author-email: support@stripe.com
License: MIT
Project-URL: Bug Tracker, https://github.com/stripe/stripe-python/issues
Project-URL: Documentation, https://stripe.com/docs/api/python
Project-URL: Source Code, https://github.com/stripe/stripe-python
Keywords: stripe api payments
Platform: UNKNOWN
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 2
Classifier: Programming Language :: Python :: 2.7
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.4
Classifier: Programming Language :: Python :: 3.5
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: Implementation :: PyPy
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*
Description-Content-Type: text/x-rst
Requires-Dist: requests[security] (>=2.20) ; python_version < "3.0"
Requires-Dist: requests (>=2.20) ; python_version >= "3.0"

Official Stripe Bindings for Python
===================================

A Python library for Stripe's API.


Setup
-----

You can install this package by using the pip tool and installing:

$ pip install stripe

Or:

$ easy_install stripe


Setting up a Stripe Account
---------------------------

Sign up for Stripe at https://dashboard.stripe.com/register.

Using the Stripe API
--------------------

Documentation for the python bindings can be found alongside Stripe's other bindings here:

- https://stripe.com/docs
- https://stripe.com/docs/api/python

In the standard documentation (the first link), most of the reference pages will have examples in Stripe's official bindings (including Python). Just click on the Python tab to get the relevant documentation.

In the full API reference for python (the second link), the right half of the page will provide example requests and responses for various API calls.


1 change: 1 addition & 0 deletions pip-wheel-metadata/stripe.dist-info/top_level.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
stripe
11 changes: 7 additions & 4 deletions stripe/multipart_data_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import random
import io

from stripe import six
import stripe


class MultipartDataGenerator(object):
Expand All @@ -14,7 +14,10 @@ def __init__(self, chunk_size=1028):
self.chunk_size = chunk_size

def add_params(self, params):
for key, value in six.iteritems(params):
# Flatten parameters first
params = dict(stripe.api_requestor._api_encode(params))

for key, value in stripe.six.iteritems(params):
if value is None:
continue

Expand Down Expand Up @@ -58,9 +61,9 @@ def get_post_data(self):
return self.data.getvalue()

def _write(self, value):
if isinstance(value, six.binary_type):
if isinstance(value, stripe.six.binary_type):
array = bytearray(value)
elif isinstance(value, six.text_type):
elif isinstance(value, stripe.six.text_type):
array = bytearray(value, encoding="utf-8")
else:
raise TypeError(
Expand Down
6 changes: 4 additions & 2 deletions stripe/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@


def utf8(value):
if six.PY2 and isinstance(value, six.text_type):
if hasattr(value, "read"):
return value
elif six.PY2 and isinstance(value, six.text_type):
return value.encode("utf-8")
else:
return value
return str(value)


def is_appengine_dev():
Expand Down
4 changes: 3 additions & 1 deletion tests/api_resources/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ def test_is_creatable(self, setup_upload_api_base, request_mock):
)
test_file = tempfile.TemporaryFile()
resource = stripe.File.create(
purpose="dispute_evidence", file=test_file
purpose="dispute_evidence",
file=test_file,
file_link_data={"create": True},
)
request_mock.assert_api_base(stripe.upload_api_base)
request_mock.assert_requested(
Expand Down
4 changes: 3 additions & 1 deletion tests/api_resources/test_file_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ def test_is_creatable(self, setup_upload_api_base, request_mock):
)
test_file = tempfile.TemporaryFile()
resource = stripe.FileUpload.create(
purpose="dispute_evidence", file=test_file
purpose="dispute_evidence",
file=test_file,
file_link_data={"create": True},
)
request_mock.assert_api_base(stripe.upload_api_base)
request_mock.assert_requested(
Expand Down
4 changes: 2 additions & 2 deletions tests/test_http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -725,13 +725,13 @@ def test_encode_dict(self):

values = [t for t in stripe.api_requestor._api_encode(body)]

assert ("foo[dob][month]", 1) in values
assert ("foo[dob][month]", "1") in values
assert ("foo[name]", "bat") in values

def test_encode_array(self):
body = {"foo": [{"dob": {"month": 1}, "name": "bat"}]}

values = [t for t in stripe.api_requestor._api_encode(body)]

assert ("foo[0][dob][month]", 1) in values
assert ("foo[0][dob][month]", "1") in values
assert ("foo[0][name]", "bat") in values
30 changes: 30 additions & 0 deletions tests/test_multipart_data_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ def run_test_multipart_data_with_file(self, test_file):
"key1": b"ASCII value",
"key2": u"Üñìçôdé value",
"key3": test_file,
"key4": {
"string": "Hello!",
"int": 234,
"float": 3.14159,
"bool": True,
"dict": {"foo": "bar"},
},
}
generator = MultipartDataGenerator()
generator.add_params(params)
Expand All @@ -36,6 +43,29 @@ def run_test_multipart_data_with_file(self, test_file):
http_body,
)
assert re.search(r"Content-Type: application/octet-stream", http_body)
assert re.search(
r"Content-Disposition: form-data; name=\"key4\[string\]\"",
http_body,
)
assert re.search(r"Hello!", http_body)
assert re.search(
r"Content-Disposition: form-data; name=\"key4\[int\]\"", http_body
)
assert re.search(r"234", http_body)
assert re.search(
r"Content-Disposition: form-data; name=\"key4\[float\]\"",
http_body,
)
assert re.search(r"3.14159", http_body)
assert re.search(
r"Content-Disposition: form-data; name=\"key4\[bool\]\"", http_body
)
assert re.search(r"True", http_body)
assert re.search(
r"Content-Disposition: form-data; name=\"key4\[dict\]\[foo\]\"",
http_body,
)
assert re.search(r"bar", http_body)

test_file.seek(0)
file_contents = test_file.read()
Expand Down

0 comments on commit eabbcd9

Please sign in to comment.