facetoe / zenpy

Python wrapper for the Zendesk API
GNU General Public License v3.0
344 stars 162 forks source link

Users endpoint not sideloading organizations, etc. #52

Closed rishi-freshbooks closed 8 years ago

rishi-freshbooks commented 8 years ago

Hey! The users endpoint does not seem to side load organizations. https://github.com/facetoe/zenpy/blob/master/zenpy/lib/endpoint.py#L243

The http request does show the query parmater https://test.zendesk.com/api/v2/users/1.json&include=organizations,abilities,roles,identities,groups

but I am unable to access organizations on the result, for example:

(Pdb) zendesk_user = zendesk.users(id=4727211738)
(Pdb) zendesk_user.organizations
*** AttributeError: 'User' object has no attribute 'organizations'
facetoe commented 8 years ago

Hi @rishi-freshbooks, It's a bit funny how Zendesk organized this. Each User is tied to one organization when you query the user API (https://developer.zendesk.com/rest_api/docs/core/users) and can be retrieved via the organization property in Zenpy:

for user in zenpy.users():
    print(user.organization)

There is also an organizations endpoint (https://developer.zendesk.com/rest_api/docs/core/organizations) which takes a User id and returns all organizations associated with that user:

for organization in zenpy.users.organizations(user_id=5386846818):
    print(organization)

If you are looking for all organizations associated with a User then the second one is what you want. Hope this helps!

rishi-freshbooks commented 8 years ago

Thanks @facetoe! Is there a way to link a user to more than 1 organization?

facetoe commented 8 years ago

Zenpy doesn't support this directly however it would be easy to do in your own code with the users.organizations endpoint.

rishi-freshbooks commented 8 years ago

Factoe would you be able to implement this endpoint? https://developer.zendesk.com/rest_api/docs/core/organization_memberships

facetoe commented 8 years ago

Ahh most definitely, that looks really handy. I'll create an issue for it and try to implement it in the next week or so. Good spot!

facetoe commented 8 years ago

Hi @rishi-freshbooks, I've implemented part of the functionality for organization memberships in a9c4ba221848a973629b92710b4bbf840d10b1af. You can query organization memberships:

# To see all organization memberships
for org_memb in zenpy.organization_memberships():
    print(org_memb)

# Or for each user (also works for each organization)
for user in zenpy.users():
    for org_memb in zenpy.users.organization_memberships(user_id=user.id):
        print(org_memb)

And also create and delete them:

obj = OrganizationMembership(user_id=1111, organization_id=222222)
created_orgmemb = zenpy.organization_memberships.create(obj)

zenpy.organization_memberships.delete(created_orgmemb)

There is some functionality I didn't implement as I figured it was unlikely to be used. If you want to use any of the un-implemented functionality let me know and I will have a look at adding it as well.

Let me know if you find any bugs!

rishi-freshbooks commented 8 years ago

Thanks @facetoe! This is great. Do you mind bumping the version and creating a tag, so we can install this new endpoint :smile: ?

facetoe commented 8 years ago

No worries, it's done!

tvallois commented 6 years ago

Hey @facetoe,

Thanks for this. Is it possible to create OrganizationMembership with user and organization objects instead of ids?

Thanks