File: example_auth.py

package info (click to toggle)
python-mypermobil 0.1.8-1.1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 168 kB
  • sloc: python: 1,172; makefile: 3
file content (70 lines) | stat: -rw-r--r-- 1,895 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""Example of how to authenticate a MyPermobil instance"""

import asyncio
import mypermobil


async def main():
    """Example of how to authenticate a MyPermobil instance"""

    # Create a session
    # mypermobil uses aiohttp.ClientSession for the requests
    session = await mypermobil.create_session()
    p_api = mypermobil.MyPermobil(
        session=session,
        application="example-application",
    )

    # get email
    email = input("Email: ")
    p_api.set_email(email)
    is_internal = email.endswith("@permobil.com")
    # request regions
    region_names = await p_api.request_region_names(include_internal=is_internal)
    print("Regions:")
    print(", ".join([region for region in region_names]))
    region_name = input("Select one: ")
    while region_name not in region_names:
        region_name = input("Select one: ")
    region = region_names[region_name]
    p_api.set_region(region)

    # send code request
    await p_api.request_application_code()
    print("Check your email for a code")
    code = input("Code: ")
    while True:
        try:
            p_api.set_code(code)
            break
        except mypermobil.MyPermobilClientException as err:
            print(err)
            code = input("Code: ")

    # get token
    try:
        token, ttl = await p_api.request_application_token()
        # íf your instance is linked to a single account set the values
        p_api.set_token(token)
        p_api.set_expiration_date(ttl)
        print("Token:", token)
        print("Expiration:", ttl)
    except mypermobil.MyPermobilException as err:
        print(err)

    # mark as authenticated
    p_api.self_authenticate()

    #################
    #               #
    # Do stuff here #
    #               #
    #################

    # close session

    await p_api.close_session()


if __name__ == "__main__":
    asyncio.run(main())