plentymarkets.py 737 B

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