lionheart / python-harvest

A Python wrapper for the Harvest time-tracking API.
Apache License 2.0
55 stars 49 forks source link

Harvest API v2 support #40

Closed Brad-eki closed 5 years ago

Brad-eki commented 5 years ago

Hi everyone,

I've been working on supporting the Harvest API v2. I have chosen to re-package the ordered dict response objects as dataclasses. This was working well enough until I found two objects with fields named "from". The trouble: "from" is a Python key word and so I'm having trouble instantiating a dataclass object where one argument is "from" (I'm surprised the dataclass didn't complain).

@dataclass class TimeImport: summary_type: str from: str = None to: str = None

The problematic syntax: my_time_import = harvest.TimeImport(summary_type='task', from='2017-03-01', to='2017-03-31')

Current approach is to not support the "from" attribute on those two objects. Obviously this isn't a solution.

I expect a petition to have those fields re-named or aliased may not get far.

Any advice on how to work around this would be appreciated.

To find the fields in the doco: In the invoices section, under "Create an invoice based on tracked time and expenses" on the line_items_import object there are two children objects, time import and expense import. Each of these children have date fields called "from". Everywhere else in the API there's a descriptor as to what it is from eg; sent_from.

https://help.getharvest.com/api-v2/invoices-api/invoices/invoices/

I'm yet to identify any other situations where "from" has been used as a field name without qualification.

Currently I have introduced support for:

HTTP PUSH dataclasses

get, create, update, delete;

get;

Cheers.

dlo commented 5 years ago

Hey @Brad-eki! This is fantastic. Easy fix on the parameter naming issue—just double splat a dictionary in lieu of function arguments. E.g.:

my_time_import = harvest.TimeImport(**{
    'summary_type': 'task',
    'from': '2017-03-01',
    'to': '2017-03-31'
})
Brad-eki commented 5 years ago

@dlo Thank you very much

That approach did occur to me very shortly after I sent the comment. I probably should have had a coffee break before sending... :D

It is probably the only way. I don't like it because those objects will require that approach. Equally, I guess I can shrug as I didn't define the API.

dlo commented 5 years ago

😊

Yeah, it's an unfortunate keyword clash, but it's acceptable since there's no alternative.

Brad-eki commented 5 years ago

I reckon we can close this one.

Cheers @dlo