base.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from django.test import TestCase
  2. class LoonflowTest(TestCase):
  3. def setUp(self):
  4. # print('start test')
  5. pass
  6. def tearDown(self):
  7. # print('end test')
  8. pass
  9. class LoonflowApiCall(object):
  10. """
  11. loonflow api调用
  12. """
  13. def __init__(self):
  14. from service.common.common_service import CommonService
  15. flag, msg = CommonService.gen_signature('ops')
  16. if not flag:
  17. return dict(code=-1, msg=msg)
  18. self.signature = msg.get('signature', '')
  19. self.timestamp = msg.get('timestamp', '')
  20. self.headers = {'HTTP_SIGNATURE': self.signature, 'HTTP_TIMESTAMP': self.timestamp, 'HTTP_APPNAME':'ops'}
  21. def api_call(self, method, url, params={}):
  22. import json
  23. from django.test.client import Client
  24. c = Client()
  25. if method not in ('get', 'post', 'patch', 'delete', 'put'):
  26. return json.loads(dict(code=-1, msg='method is invalid'))
  27. if method == 'get':
  28. response_content = c.get(url, data=params, **self.headers).content
  29. elif method == 'post':
  30. response_content = c.post(url, data=json.dumps(params), content_type='application/json', **self.headers).content
  31. elif method == 'patch':
  32. response_content = c.patch(url, data=json.dumps(params), content_type='application/json', **self.headers).content
  33. elif method == 'delete':
  34. response_content = c.delete(url, data=json.dumps(params), content_type='application/json', **self.headers).content
  35. elif method == 'put':
  36. response_content = c.put(url, data=json.dumps(params), content_type='application/json', **self.headers).content
  37. response_content_dict = json.loads(str(response_content, encoding='utf-8'))
  38. return response_content_dict