Skip to content

Commit

Permalink
adding a debug mode keyword arg to constructor and calls for baseclie…
Browse files Browse the repository at this point in the history
…nt: fixes #7 adding guysoft's examples: resolves #5
  • Loading branch information
jpetrucciani committed Apr 25, 2018
1 parent 348beec commit a123917
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 12 deletions.
64 changes: 61 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,37 @@ Built initially around hapipy, but heavily modified.

## Quick start

Here is a basic usage
### Basic Usage

```python
from hubspot3.companies import CompaniesClient
API = 'your-api'
API_KEY = 'your-api-key'

client = CompaniesClient(api_key=API)
client = CompaniesClient(api_key=API_KEY)

for company in client.get_all():
print(company)
```

### Passing Params

```python
import json
from hubspot3.deals import DealsClient

deal_id = '12345'
API_KEY = 'your_api_key'

deals_client = DealsClient(api_key=API_KEY)

params = {
'includePropertyVersions': 'true'
} # Note values are camelCase as they appear in the Hubspot Documentation!

deal_data = deals_client.get(deal_id, params=params)
print(json.dumps(deal_data))
```

## Rate Limiting

Be aware that this uses the HubSpot API directly, so you are subject to all of the guidelines that HubSpot has in place:
Expand All @@ -32,6 +51,45 @@ at the time of writing, HubSpot has the following limits in place for API reques
- 40,000 requests per day. This daily limit resets at midnight based on the time zone setting of the HubSpot account


## Extending the BaseClient - thanks [@Guysoft](https://github.com/guysoft)!

Some of the APIs are not yet complete! If you\'d like to use an API that isn\'t yet in this repo, you can extend the BaseClient class!

```python
import json
from hubspot3.base import BaseClient


PIPELINES_API_VERSION = '1'


class PipelineClient(BaseClient):
"""
Lets you extend to non-existing clients, this example extends pipelines
"""

def __init__(self, *args, **kwargs):
super(PipelineClient, self).__init__(*args, **kwargs)

def get_pipelines(self, **options):
params = {}

return self._call('pipelines', method='GET', params=params)

def _get_path(self, subpath):
return 'deals/v{}/{}'.format(
self.options.get('version') or PIPELINES_API_VERSION,
subpath
)


if __name__ == "__main__":
import json
API_KEY = "your_api_key"
a = PipelineClient(api_key=API_KEY)
print(json.dumps(a.get_pipelines()))
```


## List of available clients

Expand Down
27 changes: 23 additions & 4 deletions hubspot3/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ def __init__(
raise Exception('Cannot use both api_key and access_token.')
if not (self.api_key or self.access_token or self.refresh_token):
raise Exception('Missing required credentials.')
self.options = {'api_base': 'api.hubapi.com'}
self.options = {
'api_base': 'api.hubapi.com',
'debug': False
}
if not _PYTHON25:
self.options['timeout'] = timeout
self.options.update(extra_options)
Expand Down Expand Up @@ -129,9 +132,6 @@ def _create_request(self, conn, method, url, headers, data):
return params

def _gunzip_body(self, body):
# sio = io.StringIO(body)
# gf = gzip.GzipFile(fileobj=sio, mode='rb')
# return gf.read()
return zlib.decompress(body)

def _process_body(self, data, gzipped):
Expand Down Expand Up @@ -197,6 +197,9 @@ def _call_raw(
):
opts = self.options.copy()
opts.update(options)

debug = opts.get('debug')

url, headers, data = self._prepare_request(
subpath,
params,
Expand All @@ -205,16 +208,32 @@ def _call_raw(
doseq=doseq,
query=query
)

if debug:
print(
json.dumps(
{
'url': url,
'headers': headers,
'data': data
},
sort_keys=True,
indent=2
)
)

kwargs = {}
if not _PYTHON25:
kwargs['timeout'] = opts['timeout']

num_retries = opts.get('number_retries', 0)

# Never retry a POST, PUT, or DELETE unless explicitly told to
if method != 'GET' and not opts.get('retry_on_post'):
num_retries = 0
if num_retries > 6:
num_retries = 6

emergency_brake = 10
try_count = 0
while True:
Expand Down
8 changes: 6 additions & 2 deletions hubspot3/deals.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@ def associate(self, deal_id, object_type, object_ids, **options):
object_ids = [('id', object_id) for object_id in object_ids]
query = urllib.parse.urlencode(object_ids)

return self._call('deal/{}/associations/{}'.format(deal_id, object_type), method='PUT',
query=query, **options)
return self._call(
'deal/{}/associations/{}'.format(deal_id, object_type),
method='PUT',
query=query,
**options
)

def get_all(self, limit=None, offset=0, **options):
finished = False
Expand Down
11 changes: 9 additions & 2 deletions hubspot3/engagements.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,15 @@ def get_associated(self, object_type, object_id, **options):
offset = 0
while not finished:
print(offset)
batch = self._call('engagements/associated/{}/{}/paged'
.format(object_type, object_id), method='GET', params={'limit': querylimit, 'offset': offset}, **options)
batch = self._call(
'engagements/associated/{}/{}/paged'.format(
object_type,
object_id
),
method='GET',
params={'limit': querylimit, 'offset': offset},
**options
)
print(len(batch['results']))
output.extend(batch['results'])
finished = not batch['hasMore']
Expand Down
2 changes: 1 addition & 1 deletion hubspot3/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""


__version__ = '3.1.0'
__version__ = '3.1.1'


BASE_URL = 'https://api.hubapi.com'

0 comments on commit a123917

Please sign in to comment.