New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding documentation and object to 'create' function. #56
Conversation
khardsonhurley
commented
Jun 22, 2017
- Removed 'foo_id' and replaced with foo:{} object.
- Updated documentation.
- Removed 'foo_id' and replaced with foo:{} object. - Updated documentation.
README.rst
Outdated
|
||
:: | ||
|
||
import pygerduty |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be import pygerduty.v2
or the module won't be available.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
got it, will fix.
pygerduty/v2.py
Outdated
@@ -57,6 +57,7 @@ def __init__(self, pagerduty, base_container=None): | |||
|
|||
self.pagerduty = pagerduty | |||
self.base_container = base_container | |||
self.type = None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this used anywhere?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I added it and was going to use it but ended up not using it. I will remove.
pygerduty/v2.py
Outdated
'id': kwarg_value, | ||
'type': new_key | ||
} | ||
elif kwarg_key.endswith('_ids'): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe if it ends in _ids
it will be a list of ids ["ID1", "ID2"]
that needs to be translated to a list of objects
[{"type": "sometype", "id": "ID1"}, ...]
This seems to be doing the same thing as _id
pygerduty/v2.py
Outdated
'type': new_key | ||
} | ||
elif kwarg_key.endswith('_ids'): | ||
new_key = kwarg_key[:-3] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe we need to go from foo_ids
to foos
. This will go from foo_ids
to foo_
. Additionally we should be able to reuse some of this logic for ids
from id
. Would you mind moving this two functions id_to_obj
and ids_to_objs
, the latter making use of the former, and adding tests to verify the translations?
- Added two tests to test this functionality.
pygerduty/v2.py
Outdated
else: | ||
new_kwargs[kwarg_key] = kwarg_value | ||
data[self.sname] = new_kwargs | ||
|
||
response = self.pagerduty.request("POST", path, data=_json_dumper(data), extra_headers=extra_headers) | ||
return self.container(self, **response.get(self.sname, {})) | ||
|
||
@staticmethod | ||
def id_to_obj(key, value): | ||
new_key = key[:-3] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gmjosack I struggled with figuring out where to put the string slice. Originally I left it inside of the create function but found that hard to test. Putting it in both id_to_obj
and ids_to_objs
it caused the key to be sliced twice since ids_to_objs
depends on id_to_obj
. It created something like this:
[{'id': 'PF9KMXH', 'type': 'servi'}, {'id': 'PIJ90N7', 'type': 'servi'}]
My solution was to only do it in id_to_obj
, but then I realized we check for '_ids' two times (here and in create function) and this did not see very dry. Is there a better way to do this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One option is to only pass values to these functions and worry about renaming the key in the outer function that I suggested.
pygerduty/v2.py
Outdated
|
||
for kwarg_key, kwarg_value in kwargs.iteritems(): | ||
if kwarg_key.endswith('_id'): | ||
new_kwargs = Collection.id_to_obj(kwarg_key, kwarg_value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like you're overwriting new_kwargs here? Can you move this entire loop into a function and add a test for it to make sure we're not losing kwargs? The test should have kwargs that are and aren't transformed and test both id and ids types.
pygerduty/v2.py
Outdated
def id_to_obj(key, value): | ||
new_key = key[:-3] | ||
if key.endswith('_ids'): | ||
new_key = key[:-4] + "s" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are there any resources that end in y
that might be ies
instead of s
? e.g. policy_ids
-> policies
. I believe we have a pluralize
function that could be reused?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not see anywhere where policy_ids
are used in the API reference. The v1 reference actually shows:
"escalation_rules": [
{
"id": "PYO9B13",
"escalation_delay_in_minutes": 22,
...
}
}
compared to in v2:
{
"escalation_policies": [
{
"id": "PANZZEQ",
"type": "escalation_policy",
...
}
}
pygerduty/v2.py
Outdated
else: | ||
new_kwargs[kwarg_key] = kwarg_value | ||
data[self.sname] = new_kwargs | ||
|
||
response = self.pagerduty.request("POST", path, data=_json_dumper(data), extra_headers=extra_headers) | ||
return self.container(self, **response.get(self.sname, {})) | ||
|
||
@staticmethod | ||
def id_to_obj(key, value): | ||
new_key = key[:-3] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One option is to only pass values to these functions and worry about renaming the key in the outer function that I suggested.
tests/collection_test.py
Outdated
|
||
def test_id_to_obj(): | ||
|
||
kwarg = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these tabs for indents?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have been having issues with my tabs, sometimes it does tabs = four spaces and other times it does not. I will check on it.
tests/collection_test.py
Outdated
}, | ||
{ | ||
"id": "PIJ90N7", | ||
"type": "services" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type should be service
here I believe. It's the key to this list that needs to be plural that's passed to the api.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gmjosack Actually I just looked back and this is the reference thing we talked about before:
"services": [
{
"id": "PIJ90N7",
"type": "service_reference"
}
],
You said that you think it won't mind right? I will go ahead and change it to service if you think that will work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, pretty sure it happily takes either service
or service_reference
but I believe it has to be singular.
- First one processes the kwargs and ensures that no kwargs are lost. - Second one cuts off the suffix _id or _ids so that this method is testable.
tests/collection_test.py
Outdated
"end_time": "2012-06-16T14:00:00-04:00Z", | ||
"description": "Description goes here", | ||
"service_ids": [ | ||
"PF9KMXH" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you include more than one ID here to ensure it works with multiple?
tests/collection_test.py
Outdated
|
||
new_kwargs = pygerduty.v2.Collection.process_kwargs(kwargs_1) | ||
|
||
print new_kwargs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you remove this print?
tests/collection_test.py
Outdated
|
||
def test_process_kwargs(): | ||
|
||
kwargs_1 = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a singular _id
test in here as well to make sure everything works together?
- Added a singular _id test to ensure that the library can process _id into an object. - Added another id to the list of service_ids to ensure it works with multiple id inputs. - Cleaned up code.