EdupageAPI / edupage-api

A python library for accessing your Edupage account
https://edupageapi.github.io/edupage-api/
GNU General Public License v3.0
66 stars 13 forks source link

[Feature request] REST API #55

Closed dxwil closed 1 year ago

dxwil commented 1 year ago

Hi, would it be possible to process this info to something that's accessible though swift? Perhaps rest api?

ivanhrabcak commented 1 year ago

I've created edupage-rest, a small rest API that you can host yourself. (Please read the warning in README.md before using)

I don't know how much you need, maybe it is enough for you. Even if it's not, I think it will push you in the correct direction and you can use it as a starting point to create something that is closer to what you need.

Feel free to ask any other questions. If this solved your issue, please close it.

ivanhrabcak commented 1 year ago

I've added authentication, I hope it suits your needs.

It should now be fully usable and safe for multiple users.

dxwil commented 1 year ago

Hi, thank you so much! All I really need is Homework data, marking as done would be nice but I read that that's not currently possible.

Edit - I just set it up (really easy), but I'm not seeing homework data, is that possible to get?

ivanhrabcak commented 1 year ago

I've fixed some issues, please pull my changes to your cloned repository, or clone it again.

You would do something like this:

  1. POST to /authenticate with the user's edupage credentials as the request body (json):

    {
    "username":  "<username>",
    "password": "<password>",
    "subdomain": "<subdomain>"
    }

    The response will look like this:

    {
    "response": "c42ed375-9d42-4d36-953d-091f9cb7c62a"
    }

    The long string of numbers and characters is your token (you have to use it in the next requests).

  2. To get the notifications you have to do a GET to /timeline with these request parameters: items_per_page (int) and page (int). Send your token from the previous request in a header (Token).

Because there are a lot of notifications returned by edupage, I implemented pagination (you only get items_per_page notification in one request). If you want to get all homework you would need to get items in bulks of 10 or 20 (it's up to you). Example pseudocode (in python):

token = "c42ed375-9d42-4d36-953d-091f9cb7c62a"
items_per_page = 20
page = 0 # the first page is page 0!

homework = []

# get the first page
request = requests.get(f"http://localhost:8000/timeline?items_per_page={items_per_page}&page={page}", headers={
    "token": token
})

# do this while the server has more notifications for us
# (the servers returns the status code 204 - No Content if the page you requested doesn't exist)
while request.status != 204: 
    # notificartions will be an array of:
    # {
    #    "event_id": int,
    #    "timestamp": string (date),
    #    "text": string,
    #    "author": string,
    #    "recipient": string,
    #    "event_type": string,
    #    "additional_data": object (depends on the event_type)
    #}
    notifications = request.json()
    for notification in notifications:
        if notification["event_type"] == "homework":
            homework.append(notification)

    # we request the next page
    page += 1
    request = requests.get(f"http://localhost:8000/timeline?items_per_page={items_per_page}&page={page}", headers={
        "token": token
    })

print(homework)
ivanhrabcak commented 1 year ago

Did you figure everything out? Do you need more help? Are there any features from edupage-rest you are missing?

In case this resolved your problem, please close this issue.

ivanhrabcak commented 1 year ago

Closing because of inactivity.