Guest User

Untitled

a guest
Mar 20th, 2018
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. import os
  2. import requests
  3.  
  4.  
  5. # Magento
  6. magento_endpoint = os.environ['MAGENTO_ENDPOINT']
  7. magento_username = os.environ['MAGENTO_USERNAME']
  8. magento_password = os.environ['MAGENTO_PASSWORD']
  9.  
  10.  
  11. class Magento2:
  12. endpoint = None
  13. headers = None
  14.  
  15. def __init__(self, endpoint=None, username=None, password=None):
  16. self.endpoint = endpoint
  17.  
  18. # Connect to the API
  19. credentials = {'username': username, 'password': password}
  20. authentication = requests.post('{}'
  21. '/rest/V1/integration/admin/token'.
  22. format(endpoint),
  23. json=credentials)
  24.  
  25. # The request failed
  26. if authentication.status_code != 200:
  27. print('Authentication error')
  28.  
  29. # Set authentication headers that we will reuse in API calls
  30. self.headers = {
  31. 'Content-Type': 'application/json',
  32. 'Authorization': 'Bearer {}'.format(authentication.json())
  33. }
  34.  
  35.  
  36. magento = Magento2(endpoint=magento_endpoint, username=magento_username,
  37. password=magento_password)
  38.  
  39. customer = {
  40. 'customer':
  41. {
  42. 'store_id': 1,
  43. 'website_id': 1,
  44. 'group_id': 1,
  45. 'firstname': 'Anonymous',
  46. 'lastname': 'Anonymous',
  47. 'email': 'valid@email.org',
  48. 'taxvat': None,
  49. 'addresses': [
  50. {
  51. 'firstname': 'Anonymous',
  52. 'lastname': 'Anonymous',
  53. 'telephone': '0123456789',
  54. 'company': 'Anonymous Inc.',
  55. 'street': ['Where the streets have no name 1'],
  56. 'postcode': '1000AA',
  57. 'city': 'Amsterdam',
  58. 'country_id': 'NL',
  59. 'default_billing': False,
  60. 'default_shipping': False
  61. },
  62. {
  63. 'firstname': 'Anonymous',
  64. 'lastname': 'Anonymous',
  65. 'telephone': '0123456789',
  66. 'company': 'Anonymous BV',
  67. 'street': ['Where the streets have no name 1'],
  68. 'postcode': '1000AA',
  69. 'city': 'Amsterdam',
  70. 'country_id': 'NL',
  71. 'default_billing': False,
  72. 'default_shipping': False
  73. }
  74. ]
  75. }
  76. }
  77.  
  78. # Save the customer
  79. save = requests.post('{}/rest/V1/customers/'.format(
  80. magento.endpoint), headers=magento.headers,
  81. json=customer)
  82. print(save.json())
  83. save = requests.put('{}/rest/V1/customers/{}'.format(
  84. magento.endpoint, 1), headers=magento.headers,
  85. json=customer)
  86. print(save.json())
Add Comment
Please, Sign In to add comment