Skip to content

Commit

Permalink
Create client class to include all migration methods, refactor code, …
Browse files Browse the repository at this point in the history
…add function header comments to client methods
  • Loading branch information
Bruno committed Oct 29, 2023
1 parent a1ff375 commit aff1040
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 12 deletions.
3 changes: 0 additions & 3 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
from .classes.migration import Migrate

print("Hello World!")
19 changes: 10 additions & 9 deletions classes/migration.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# imports
import requests
from auth import Auth
from .auth import Auth

class Migrate():
""" Migrate data to HubSpot """
Expand Down Expand Up @@ -35,28 +35,29 @@ def get_data(self, params):
return data


def post_data(self, data):
def post_data(self, data, path):
""" Post data to path
Arguments:
data {dict} -- Data to post to path
"""
response = requests.post(self.path, json=data, headers=self.headers)
url = f"{self.base_path}/{path}"
response = requests.post(url, json=data, headers=self.headers)
if response.status_code in self.status_codes:
print(f"Successfully posted data to {self.path} 🎉")
print(f"Successfully posted data to {url} 🎉")
else:
print(f"Error posting data to {self.path}: {response.status_code} - {response.text} ❌")
print(f"Error posting data to {url}: {response.status_code} - {response.text} ❌")
return response


def patch_data(self, data, id=None):
def patch_data(self, data, path, id=None):
""" Patch data to path
Arguments:
data {dict} -- Data to patch to path
id {str} -- ID of the data to patch
"""
url = f"{self.path}/{id}"
url = f"{path}/{id}"
response = requests.patch(url, json=data, headers=self.headers)
if response.status_code in self.status_codes:
print(f"Successfully put data to {url} 🎉")
Expand All @@ -65,13 +66,13 @@ def patch_data(self, data, id=None):
return response


def delete_data(self, id=None):
def delete_data(self, path, id=None):
""" Delete data from path
Arguments:
id {str} -- ID of the data to delete
"""
url = f"{self.path}/{id}"
url = f"{path}/{id}"
response = requests.delete(url, headers=self.headers)
if response.status_code in self.status_codes:
print(f"Successfully deleted data from {url} with ID: {id} 🎉")
Expand Down
61 changes: 61 additions & 0 deletions client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from .classes.migration import Migrate

class MigrationClient:
def __init__(self, config, hubspot):
self.migrate = Migrate(dict, config, hubspot)


# Migrate contacts
def migrate_contact(self, data):
""" Migrate contact to HubSpot
Arguments:
data {dict} -- Data to migrate to HubSpot
"""
return self.migrate.post_data(data, 'contacts')

def update_contact(self, data, contact_id):
""" Update contact in HubSpot
Areguments:
data {dict} -- Data to update in HubSpot
contact_id {str} -- ID of the contact to update
"""
return self.migrate.put_data(data, contact_id, 'contacts')

def delete_contact(self, contact_id):
""" Delete contact in HubSpot
Arguments:
contact_id {str} -- ID of the contact to delete
"""
return self.migrate.delete_data(contact_id, 'contacts')


# Migrate companies
def migrate_company(self, data):
""" Migrate company to HubSpot
Arguments:
data {dict} -- Data to migrate to HubSpot
"""
return self.migrate.post_data(data, 'companies')

def update_company(self, data, company_id):
""" Update company in HubSpot
Areguments:
data {dict} -- Data to update in HubSpot
company_id {str} -- ID of the company to update
"""
return self.migrate.put_data(data, company_id, 'companies')

def delete_company(self, company_id):
""" Delete company in HubSpot
Arguments:
company_id {str} -- ID of the company to delete
"""
return self.migrate.delete_data(company_id, 'companies')


0 comments on commit aff1040

Please sign in to comment.