jpetrucciani / hubspot3

python3.6+ hubspot client based on hapipy, but modified to use the newer endpoints and non-legacy python
MIT License
147 stars 73 forks source link

Add a CLI #38

Closed W1ldPo1nter closed 5 years ago

W1ldPo1nter commented 5 years ago

This PR will extend the hubspot3 package by a command line interface, which would make it possible to use all of the package's API-interacting functionality outside of a Python project. This would be useful for things like:

Usage

Using a checkout of the source, it could be called like this: python -m hubspot3 ...

Due to the added entrypoint in the setup.py, the usage becomes even easier after the package was actually installed via setuptools/pip: hubspot3 ...

The actual arguments passed to the CLI would always have the following format: python -m hubspot3 <client args> <api name> <method name> <method args> where

An example call would therefore look like this: python -m hubspot3 --api_key demo contacts get_contact_by_id --contact_id 51 Assuming a contact with this ID exists, the CLI will then output the JSON-encoded results from the API on stdout.

Further descriptions and options can be found in the added sphinx documentation.

Special options

The CLI also offers two special options:

More info on how to use these options can also be found in the added sphinx documentation.

Maintenance

The CLI is designed to be low-maintenance and to not require any changes to the existing code of the API clients. It is built on Google's Fire library, which allows to transform objects of all kinds into CLIs. We introduced some wrapper classes for the Hubspot3 main client and the "sub-clients" that make sure that only the right things are exposed via the Fire-generated CLI:

New clients should therefore automatically work as long as they are added as a property to the Hubspot3 main class, so there will be no need to touch the CLI code when adding new clients/methods. The only time the CLI code will require some love is when properties on the Hubspot3 main client or methods on "sub-clients" should be explicitly excluded from the CLI (the code for this is already in place).

Requirements

The CLI requires one new python package: Google's Fire. This requirement was added to the setup.py as an extra_requirement, so it won't be installed for people who don't want to use the CLI. The package could therefore still be installed as hubspot3 if CLI usage is not intended or as hubspot3[cli] if someone wants to use the CLI as well. You could also move the fire requirement to the regular install_requires if you don't think this separation of requirements is useful.

I hope this was enough of an explanation. Thank you for maintaining this library and we hope the addition of this CLI is something you consider useful, so it can become a part of hubspot3.

jpetrucciani commented 5 years ago

Woah - thanks for the incredibly detailed PR! 😄

I'll review this today sometime, and hopefully I can get it merged in soon!

W1ldPo1nter commented 5 years ago

No worries, it would totally be understandable if this would take some time to review.

Also, if there are any questions after you were able to take a first look at it, I'll be happy to answer them or make adjustments if required.

jpetrucciani commented 5 years ago

I've got this merged in now! I'll create a new release on pip soon.

The only changes I made were applying black styles, which I always use (and I recommend it if you haven't checked it out! It's amazing to not have to worry about styling anymore).

Don't feel obligated to always use black unless you want to - it's quick for me to run on the code after merging if that's the case!

jpetrucciani commented 5 years ago

This is now available as release 3.2.16 on pip!

Thanks again (and to @hkage and @kantimati) for this awesome contribution! 😄