jscott1989 / python-pipedrive

Python API for interacting with the pipedrive.com API
MIT License
93 stars 44 forks source link

linting, default method values, and safer logging #11

Closed kevhill closed 7 years ago

kevhill commented 7 years ago

Great library! I was basically about to write this myself but luckily did a google search first.

Because I'll be using it in a production environment, having the api token print to stdout was a bit of a red flag, but luckily it was easy to fix. I figured I'd push up those changes in case anyone else wanted some more control over logging.

My approach just uses the native logging module, and only print the full uri with the api key when you set up a logger to print DEBUG level logging events. You can test the approach with this code after setting PIPEDRIVE_API_KEY as an environment variable

import logging
from pipedrive import Pipedrive
import os
import sys

root_logger = logging.getLogger()
stdout = logging.StreamHandler(sys.stdout)
root_logger.addHandler(stdout)
stdout.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))

PIPEDRIVE_API_KEY = os.environ.get('PIPEDRIVE_API_KEY')
pd = Pipedrive(PIPEDRIVE_API_KEY)

root_logger.setLevel(logging.DEBUG)
print('Calling API with log level DEBUG. Should see full uri')
pd.organizations()
print(' Success\n')

root_logger.setLevel(logging.INFO)
print('Calling API with log level INFO. Should not see any output')
pd.organizations()
print(' Success\n')

I also cleaned up the code to conform to PEP8 and added some default values to the request wrapper for ease of use.

jscott1989 commented 7 years ago

👍 Looks good. Thanks for the contribution.