LibreBooking / app

Repository for the last open source version of Booked Scheduler. The "develop" branch contains the most current working code of the project and should be considered beta. The "master" branch is the most current stable release of BookedScheduler. Please read doc/README.md for further details.
GNU General Public License v3.0
373 stars 220 forks source link

Difficulty when trying to consume the Librebooking API #285

Open tiagojulianoferreira opened 7 months ago

tiagojulianoferreira commented 7 months ago

hello!

I'm working on an application that implements a panel for an Indoor TV and that would consume LibreBooking room scheduling data from the API, but I'm having difficulty understanding the steps to authenticate to the API and consume the Scheduler route.

Apart from the documentation, is there any example of querying the LibreBooking API that I can use as inspiration to try to identify why the following code doesn't work?

import requests

class APIWrapper:
    def __init__(self, base_url):
        self.base_url = base_url
        self.session = requests.Session()
        self.token = None  # Store the session token

    def make_another_request(self, route, data=None):
        url = f"{self.base_url}/{route}"

        # Check if the session token is available or renew
        if not self.token:
            login_data = {
                "username": "admin",
                "password": "password"
            }
            login_url = f"{self.base_url}/Authentication/Authenticate"
            response = self.session.post(login_url, json=login_data, verify=False)

            if response.status_code == 200:
                response_json = response.json()
                if 'sessionToken' in response_json:
                    self.token = response_json['sessionToken']
            else:
                return None  # Return None for a failed login

        # Include the token in the request headers
        headers = {'Authorization': f'Bearer {self.token}'}
        response = self.session.get(url, json=data, headers=headers, verify=False)

        if response.status_code == 200:
            return response.json()  # Return the JSON response for a successful request
        else:
            return None  # Return None for a failed request

# API URL
api_url = "http://librebooking.teste/Web/Services"

# Creating an instance of the APIWrapper class
api_wrapper = APIWrapper(api_url)

# Making another request to a different route
route_destination = "Schedules"
request_data = {
    "schedules": 
        [ { 
                "daysVisible": 5,
                "id": 123, "isDefault": "true", 
                "name": "schedule name", 
                "timezone": "timezone_name", 
                "weekdayStart": 0, 
                "availabilityBegin": "2023-01-18T21:09:54-0500", 
                "availabilityEnd": "2023-02-07T21:09:54-0500", 
                "maxResourcesPerReservation": 10, 
                "totalConcurrentReservationsAllowed": 0, 
                "links": [], 
                "message": "null" 
        } 
        ], 
    "links": [], "message": "null"
}

response_data = api_wrapper.make_another_request(route_destination, request_data)

# Process response_data as needed
print(response_data)
colisee commented 7 months ago

Hi @tiagojulianoferreira ,

could you please confirm that you are able to access the api portal page at http:///Web/Services.

You should get a page like below. Please SCROLL DOWN to check that you have no errors

Capture d'écran 2023-12-23 195919

tiagojulianoferreira commented 7 months ago

Thanks @colisee ,

I was forgetting to enter the X-Booked-SessionToken and X-Booked-UserId header as instructed in the documentation.

I also understood that in my case I need to consume the "Reservations" route, which contains the data I need to display on the TV (reservation time, title, firstName, lastName)

Now I need to find a way to access only the day's reservations, which is what's important to show on TV.

JohnVillalovos commented 2 months ago

@tiagojulianoferreira can this issue be closed? If so, can you close it?

tiagojulianoferreira commented 2 months ago

I think it can be closed. From what I understand, the API does not offer a way to access the current day's reservations. Maybe it's worth opening an issue requesting this feature specifically?

I may try to implement it in the future, even. However, I will need initial guidance on how to do it.

JohnVillalovos commented 2 months ago

I could see opening a new issue for a feature enhancement and closing this issue.