Skip to content

Commit

Permalink
Merge pull request #402 from stripe/ob-remove-util-json
Browse files Browse the repository at this point in the history
Remove util.json and use json module directly everywhere
  • Loading branch information
ob-stripe authored Feb 28, 2018
2 parents 337c6d5 + 33aa7d4 commit abec0b6
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 19 deletions.
3 changes: 2 additions & 1 deletion stripe/api_requestor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import calendar
import datetime
import json
import platform
import time

Expand Down Expand Up @@ -209,7 +210,7 @@ def request_headers(self, api_key, method):
ua['application'] = stripe.app_info

headers = {
'X-Stripe-Client-User-Agent': util.json.dumps(ua),
'X-Stripe-Client-User-Agent': json.dumps(ua),
'User-Agent': user_agent,
'Authorization': 'Bearer %s' % (api_key,),
}
Expand Down
7 changes: 4 additions & 3 deletions stripe/stripe_object.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import absolute_import, division, print_function

import datetime
import json
from copy import deepcopy

import stripe
Expand Down Expand Up @@ -33,7 +34,7 @@ def _serialize_list(array, previous):


class StripeObject(dict):
class ReprJSONEncoder(util.json.JSONEncoder):
class ReprJSONEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime.datetime):
return api_requestor._encode_datetime(obj)
Expand Down Expand Up @@ -227,8 +228,8 @@ def __repr__(self):
return unicode_repr

def __str__(self):
return util.json.dumps(self, sort_keys=True, indent=2,
cls=self.ReprJSONEncoder)
return json.dumps(self, sort_keys=True, indent=2,
cls=self.ReprJSONEncoder)

def to_dict(self):
return dict(self)
Expand Down
6 changes: 4 additions & 2 deletions stripe/stripe_response.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from stripe import util
from __future__ import absolute_import, division, print_function

import json


class StripeResponse:
Expand All @@ -7,7 +9,7 @@ def __init__(self, body, code, headers):
self.body = body
self.code = code
self.headers = headers
self.data = util.json.loads(body)
self.data = json.loads(body)

@property
def idempotency_key(self):
Expand Down
2 changes: 0 additions & 2 deletions stripe/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import hmac
import io
import json
import logging
import sys
import os
Expand All @@ -20,7 +19,6 @@
__all__ = [
'io',
'parse_qsl',
'json',
'utf8',
'log_info',
'log_debug',
Expand Down
3 changes: 2 additions & 1 deletion stripe/webhook.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import absolute_import, division, print_function

import hmac
import json
import time
from hashlib import sha256

Expand All @@ -18,7 +19,7 @@ def construct_event(payload, sig_header, secret,
payload = payload.decode('utf-8')
if api_key is None:
api_key = stripe.api_key
data = util.json.loads(payload)
data = json.loads(payload)
event = stripe.Event.construct_from(data, api_key)

WebhookSignature.verify_header(payload, sig_header, secret, tolerance)
Expand Down
4 changes: 3 additions & 1 deletion tests/request_mock.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import absolute_import, division, print_function

import json

import stripe
from stripe import six
from stripe.stripe_response import StripeResponse
Expand Down Expand Up @@ -100,7 +102,7 @@ def get_response(self, method, url):
if (method, url) in self._entries:
rbody, rcode, rheaders = self._entries.pop((method, url))
if not isinstance(rbody, six.string_types):
rbody = stripe.util.json.dumps(rbody)
rbody = json.dumps(rbody)
stripe_response = StripeResponse(rbody, rcode, rheaders)
return stripe_response

Expand Down
8 changes: 4 additions & 4 deletions tests/test_api_requestor.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from __future__ import absolute_import, division, print_function

import datetime
import json
import tempfile

import pytest

import stripe
from stripe import six
from stripe.stripe_response import StripeResponse
from stripe import util

from six.moves.urllib.parse import urlsplit

Expand Down Expand Up @@ -76,7 +76,7 @@ def _user_agent_match(self, other):

def _x_stripe_ua_contains_app_info(self, other):
if self.app_info:
ua = stripe.util.json.loads(other['X-Stripe-Client-User-Agent'])
ua = json.loads(other['X-Stripe-Client-User-Agent'])
if 'application' not in ua:
return False
return ua['application'] == self.app_info
Expand Down Expand Up @@ -311,7 +311,7 @@ def test_empty_methods(self, requestor, mock_response, check_call):
assert isinstance(resp, StripeResponse)

assert resp.data == {}
assert resp.data == util.json.loads(resp.body)
assert resp.data == json.loads(resp.body)

def test_methods_with_params_and_response(self, requestor, mock_response,
check_call):
Expand All @@ -330,7 +330,7 @@ def test_methods_with_params_and_response(self, requestor, mock_response,
assert isinstance(resp, StripeResponse)

assert resp.data == {'foo': 'bar', 'baz': 6}
assert resp.data == util.json.loads(resp.body)
assert resp.data == json.loads(resp.body)

if method == 'post':
check_call(
Expand Down
7 changes: 4 additions & 3 deletions tests/test_stripe_object.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
from __future__ import absolute_import, division, print_function

import datetime
import json
import pickle
from copy import copy, deepcopy

import pytest

import stripe
from stripe import util, six
from stripe import six


SAMPLE_INVOICE = stripe.util.json.loads("""
SAMPLE_INVOICE = json.loads("""
{
"amount_due": 1305,
"attempt_count": 0,
Expand Down Expand Up @@ -168,7 +169,7 @@ def test_to_json(self):
obj = stripe.stripe_object.StripeObject.construct_from(
SAMPLE_INVOICE, 'key')

self.check_invoice_data(util.json.loads(str(obj)))
self.check_invoice_data(json.loads(str(obj)))

def check_invoice_data(self, data):
# Check rough structure
Expand Down
5 changes: 3 additions & 2 deletions tests/test_stripe_response.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import absolute_import, division, print_function

from stripe import util
import json

from stripe.stripe_response import StripeResponse


Expand All @@ -27,7 +28,7 @@ def test_body(self):

def test_data(self):
response, headers, body, code = self.mock_stripe_response()
assert response.data == util.json.loads(body)
assert response.data == json.loads(body)

@staticmethod
def mock_stripe_response():
Expand Down

0 comments on commit abec0b6

Please # to comment.