Skip to content

Commit

Permalink
Merge pull request #12 from journy-io/upsert-account-members
Browse files Browse the repository at this point in the history
Correct usage of members for accounts + update tests + update README
  • Loading branch information
ManuDeBuck authored Apr 24, 2021
2 parents 94e6780 + bb837ea commit 7936ceb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ properties["mrr"] = 399
properties["plan"] = "Pro"
properties["registered_at"] = datetime.now()
properties["this_property_will_be_deleted"] = ""
result = client.upsert_account(account, properties, ["memberId1", "memberId2"])

member1 = UserIdentified.by_user_id("memberId1")
member2 = UserIdentified.by_user_id("memberId2")

result = client.upsert_account(account, properties, [member1, member2])
if isinstance(result, Success):
print(result.request_id) # str
print(result.calls_remaining) # int
Expand Down
11 changes: 7 additions & 4 deletions journyio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from .user_identified import UserIdentified
from .account_identified import AccountIdentified


class Properties(dict):

def __init__(self):
Expand Down Expand Up @@ -149,20 +150,22 @@ def upsert_user(self, user: UserIdentified, properties: Properties) -> Success[N
except Exception:
raise JournyException(f"An unknown error has occurred")

def upsert_account(self, account: AccountIdentified, properties: Properties, members: List[str]) -> Success[
None] or Failure:
def upsert_account(self, account: AccountIdentified, properties: Properties, members: List[UserIdentified]) -> \
Success[
None] or Failure:
assert_journy(isinstance(account, AccountIdentified), "Account is not an AccountIdentified object.")
assert_journy(isinstance(properties, Properties), "Properties is not a Properties object.")
for member in members:
assert_journy(isinstance(member, str), f"Member {member} is not a string.")
assert_journy(isinstance(member, UserIdentified), f"Member {member} is not a UserIdentified object.")

try:
request = HttpRequest(self.__create_url("/accounts/upsert"), Method.POST,
self.__get_headers(),
json.dumps({
"identification": account.format_identification(),
"properties": properties.properties,
"members": members
"members": [{"identification": member.format_identification()} for member in
members]
}))
response = self.httpclient.send(request)
calls_remaining = Client.__parse_calls_remaining(response)
Expand Down
7 changes: 5 additions & 2 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,10 @@ def test_client_upsert_account():
properties["haveDog"] = False
properties["name"] = "Journy"

response = client.upsert_account(account, properties, ["hansId", "manuId"])
member1 = UserIdentified.by_user_id("hansId")
member2 = UserIdentified.by_user_id("manuId")

response = client.upsert_account(account, properties, [member1, member2])

assert (isinstance(response, Success))
assert (response.__str__() == "Success(requestId, 4999, None)")
Expand All @@ -163,7 +166,7 @@ def test_client_upsert_account():
assert (response.data is None)

assert (
http_client_testing.received_request.__str__() == 'HttpRequest(https://api.journy.io/accounts/upsert, Method.POST, {"content-type": "application/json", "x-api-key": "api-key"}, {"identification": {"domain": "www.journy.io", "accountId": "account_id"}, "properties": {"havedog": false, "name": "Journy"}, "members": ["hansId", "manuId"]})')
http_client_testing.received_request.__str__() == 'HttpRequest(https://api.journy.io/accounts/upsert, Method.POST, {"content-type": "application/json", "x-api-key": "api-key"}, {"identification": {"domain": "www.journy.io", "accountId": "account_id"}, "properties": {"havedog": false, "name": "Journy"}, "members": [{"identification": {"userId": "hansId"}}, {"identification": {"userId": "manuId"}}]})')


def test_client_link():
Expand Down

0 comments on commit 7936ceb

Please sign in to comment.