Amocrm custom library
- Retrieving data from amocrm amocrm.api (v2 and v4).
- Uploading data to database.
Supported databases:
Usage
pip3 install amocrm-talenttech-oss
Retrieving data from API:
import datetime
from api.api_loader_amocrm_v4 import AmocrmApiLoader as ApiLoaderV4
date_modified_from = datetime.datetime.now() - datetime.timedelta(days=1)
args_api = {
"amocrm_api_url": "https://<NAMESPACE>.amocrm.ru/amocrm.api/v4/<ENTITY>?page={page}&limit={limit}",
"AUTH_URL":"https://<NAMESPACE>.amocrm.ru/oauth2/access_token",
"CLIENT_SECRET":"xxxx",
"CLIENT_ID": "xxx-xxx-xxxx-xxxx-xxxxxxx",
"REDIRECT_URL":"https://xxxx/xx"
}
args_s3 = {
"aws_access_key_id": <S3_ACCESS_KEY>,
"aws_secret_access_key": <S3_ACCESS_SECRET>,
"endpoint_url": <S3_ENDPOINT_URL>,
"bucket": <S3_BUCKET>
}
api_loader = ApiLoaderV4(
entity=<ENTITY>,
s3_path=<S3_PATH>,
s3_token_path=<S3_TOKEN_PATH>,
args_s3=args_s3,
args_api=args_api,
date_modified_from=date_modified_from,
with_offset=True,
batch_api=500
)
api_loader.auth(<CODE_AUTH>)
api_loader.extract()
Uploading data to vertica:
from db.vertica_uploader import UploaderDB as VerticaUploaderDB
sql_credentials = {
"database": <DATABASE>,
"schema": <SCHEMA>
"user": <VERTICA_WRITE_USER>,
"host": <VERTICA_HOST>,
"port": <VERTICA_PORT>,
"password": <VERTICA_WRITE_PASSWORD>,
"vertica_configs": <VERTICA_CONFIGS>,
}
args_s3 = {
"aws_access_key_id": <S3_ACCESS_KEY>,
"aws_secret_access_key": <S3_ACCESS_SECRET>,
"endpoint_url": <S3_ENDPOINT_URL>,
"bucket": <S3_BUCKET>
}
db_uploader = VerticaUploaderDB(
args_s3=args_s3,
s3_path=s3_path,
sql_credentials=sql_credentials,
entity=<ENTITY>,
table_name=<TABLE_NAME>,
json_columns=[<COLUM_JSON_1>, <COLUM_JSON_2>]
)
db_uploader.load_s3_to_db()
import os
import json
args = {
"action": os.getenv("action"),
"etl_name_no_version": os.getenv("ETL_NAME_NO_VERSION"),
"execution_date": parse(os.getenv("execution_date")),
"config_name": os.getenv("config_name"),
"entity": os.getenv("entity"),
"args_s3": args_netology_s3,
"sql_credentials": sql_credentials,
"db": os.getenv("db"),
"table_name": os.getenv("table_name"),
"table_to_optimize": os.getenv("table_to_optimize"),
"json_columns": os.getenv("json_columns"),
"amocrm_secrets": json.loads(os.getenv("AMOCRM_TOKEN_SECRET")),
"amocrm_api_url": os.getenv("amocrm_api_url"),
"if_modified_since": os.getenv("if_modified_since"),
"parser": os.getenv("parser"),
}
amo_runner = AmocrmRunner(**args)
amo_runner.run()