diff --git a/hubmigrate/MOCK_DATA.xlsx b/hubmigrate/MOCK_DATA.xlsx new file mode 100644 index 0000000..6ff4c27 Binary files /dev/null and b/hubmigrate/MOCK_DATA.xlsx differ diff --git a/hubmigrate/classes/auth.py b/hubmigrate/classes/auth.py index 984183f..88c4b36 100644 --- a/hubmigrate/classes/auth.py +++ b/hubmigrate/classes/auth.py @@ -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 \ No newline at end of file + # 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() \ No newline at end of file diff --git a/hubmigrate/classes/migration.py b/hubmigrate/classes/migration.py index 0014638..6d0424e 100644 --- a/hubmigrate/classes/migration.py +++ b/hubmigrate/classes/migration.py @@ -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() }"} @@ -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 diff --git a/hubmigrate/cli.py b/hubmigrate/cli.py new file mode 100644 index 0000000..e08c6c3 --- /dev/null +++ b/hubmigrate/cli.py @@ -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() \ No newline at end of file diff --git a/hubmigrate/client.py b/hubmigrate/client.py index 05a9af8..265418a 100644 --- a/hubmigrate/client.py +++ b/hubmigrate/client.py @@ -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): diff --git a/hubmigrate/requirements.txt b/hubmigrate/requirements.txt index 50faade..ebd1ceb 100644 --- a/hubmigrate/requirements.txt +++ b/hubmigrate/requirements.txt @@ -2,4 +2,5 @@ pytest coverage python-decouple requests -pandas \ No newline at end of file +pandas +python-decouple \ No newline at end of file diff --git a/hubmigrate/sample_company.json b/hubmigrate/sample_company.json index 1b18792..5f53ccf 100644 --- a/hubmigrate/sample_company.json +++ b/hubmigrate/sample_company.json @@ -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" } \ No newline at end of file diff --git a/hubmigrate/sample_company.xlsx b/hubmigrate/sample_company.xlsx deleted file mode 100644 index 1719562..0000000 --- a/hubmigrate/sample_company.xlsx +++ /dev/null @@ -1,2 +0,0 @@ -name,country,city,address,zip,website,phone -Politie Utrecht,Netherland,Utrecht,Marco Pololaan 215,3526GB,https://www.politie.nl/,0900-8844