plentymarkets.py 796 B

1234567891011121314151617181920212223242526272829
  1. from json import dumps, loads
  2. import re
  3. from oauthlib.common import to_unicode
  4. def plentymarkets_compliance_fix(session):
  5. def _to_snake_case(n):
  6. return re.sub("(.)([A-Z][a-z]+)", r"\1_\2", n).lower()
  7. def _compliance_fix(r):
  8. # Plenty returns the Token in CamelCase instead of _
  9. if (
  10. "application/json" in r.headers.get("content-type", {})
  11. and r.status_code == 200
  12. ):
  13. token = loads(r.text)
  14. else:
  15. return r
  16. fixed_token = {}
  17. for k, v in token.items():
  18. fixed_token[_to_snake_case(k)] = v
  19. r._content = to_unicode(dumps(fixed_token)).encode("UTF-8")
  20. return r
  21. session.register_compliance_hook("access_token_response", _compliance_fix)
  22. return session