Skip to content

Commit

Permalink
refactor: some changes
Browse files Browse the repository at this point in the history
  • Loading branch information
bcostaaa01 committed Aug 31, 2024
1 parent ab556f0 commit 7e14750
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 20 deletions.
Binary file added hubmigrate/MOCK_DATA.xlsx
Binary file not shown.
22 changes: 14 additions & 8 deletions hubmigrate/classes/auth.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
# import .env file
from decouple import config
import os
from dotenv import load_dotenv


load_dotenv()

access_token = os.getenv('ACCESS_TOKEN')

class Auth:
""" Authenticate with HubSpot """
@staticmethod
def get_token():
# get HubSpot private app token from input
# token = input('Enter your HubSpot private app token: ')
# return token from .env file
access_token = config('ACCESS_TOKEN')
return access_token
# Get the HubSpot private app token from environment variables
access_token = os.environ.get('ACCESS_TOKEN')
print(access_token)
if not access_token:
raise ValueError("ACCESS_TOKEN not found. Declare it as envvar or define a default value.")
return access_token


Auth.get_token()
5 changes: 3 additions & 2 deletions hubmigrate/classes/migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Migrate():
def __init__(self, object_type, config, hubspot, test_access_token=None):
self.config = config
self.hubspot = hubspot
self.base_path = 'https://api.hubapi.com/crm/v3/objects'
self.base_path = 'https://api.hubapi.com/crm/v3/objects/'
self.path = f"{self.base_path}/{object_type}"
self.headers = {"Authorization": f"Bearer { Auth.get_token() }"}

Expand Down Expand Up @@ -58,13 +58,14 @@ def post_data(self, data, path, data_type='json'):
data_list = df.to_dict(orient='records')
else:
raise ValueError("Unsupported data type. Use 'json', 'excel', or 'csv'.")


# Post each record to HubSpot
for record in data_list:
url = f"{self.base_path}/{path}"
response = requests.post(url, json=record, headers=self.headers)
if response.status_code in self.status_codes:
print(f"Successfully posted data to {url} 🎉")
print(f"Successfully posted {record} to {url} 🎉")
else:
print(f"Error posting data to {url}: {response.status_code} - {response.text} ❌")
return response
Expand Down
53 changes: 53 additions & 0 deletions hubmigrate/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import argparse
import json
import pandas as pd
from hubmigrate.client import MigrationClient
from dotenv import load_dotenv
import os


load_dotenv()


def read_file(file_path, data_type):
if data_type == 'json':
with open(file_path, 'r') as file:
return json.load(file)
elif data_type == 'excel':
if file_path.endswith('.xlsx'):
return pd.read_excel(file_path, engine='calamine').to_dict(orient='records')
elif file_path.endswith('.xls'):
return pd.read_excel(file_path, engine='xlrd').to_dict(orient='records')
else:
raise ValueError("Unsupported Excel file format")
elif data_type == 'csv':
return pd.read_csv(file_path).to_dict(orient='records')
else:
raise ValueError("Unsupported data type")


def main():
parser = argparse.ArgumentParser(description="Migrate data to HubSpot")

parser.add_argument('object_type', choices=['contacts', 'companies'], help="Type of object to migrate")
parser.add_argument('file_path', help="Path to the data file (JSON, Excel, or CSV)")
parser.add_argument('data_type', choices=['json', 'excel', 'csv'], help="Type of data file")
parser.add_argument('--config', default='config', help="Path to the config file")
parser.add_argument('--hubspot', default='hubspot', help="HubSpot portal name")

args = parser.parse_args()

data = read_file(args.file_path, args.data_type)

client = MigrationClient(args.config, args.hubspot)

if args.object_type == 'contacts':
response = client.migrate_contact(data)
elif args.object_type == 'companies':
print(data)
response = client.migrate_company(data)

print(response)

if __name__ == '__main__':
main()
1 change: 1 addition & 0 deletions hubmigrate/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def migrate_company(self, data, data_type):
Arguments:
data {dict} -- Data to migrate to HubSpot
"""
print(data)
return self.migrate.post_data(data, 'companies', data_type)

def update_company(self, data, company_id):
Expand Down
3 changes: 2 additions & 1 deletion hubmigrate/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ pytest
coverage
python-decouple
requests
pandas
pandas
python-decouple
8 changes: 1 addition & 7 deletions hubmigrate/sample_company.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
{
"name": "Politie Utrecht",
"country": "Netherlands",
"city": "Utrecht",
"address": "Marco Pololaan 215",
"zip": "3526GB",
"website": "https://www.politie.nl/",
"phone": "0900-8844"
"name": "Politie Utrecht"
}

2 changes: 0 additions & 2 deletions hubmigrate/sample_company.xlsx

This file was deleted.

0 comments on commit 7e14750

Please sign in to comment.