Skip to content
This repository has been archived by the owner on Aug 27, 2020. It is now read-only.

Commit

Permalink
add recipes api interface
Browse files Browse the repository at this point in the history
  • Loading branch information
SebRut committed May 26, 2019
1 parent 9ec3f33 commit 31d09c8
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 4 deletions.
31 changes: 29 additions & 2 deletions qbo/qbo.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from typing import List
from urllib.parse import urljoin
from uuid import UUID

import requests
import urllib3
Expand Down Expand Up @@ -77,14 +79,33 @@ def rinsing_status(self):
return self._rinsing_status


class Recipe(object):
def __init__(self, parsed_json):
self._name = parsed_json['name']
self._uid = UUID(parsed_json['uid'])
self._uses_hot_milk = parse_int(parsed_json['usesHotMilk']) == 1

@property
def name(self) -> str:
return self._name

@property
def uid(self) -> UUID:
return self._uid

@property
def uses_hot_milk(self) -> bool:
return self._uses_hot_milk


class Qbo(object):
def __init__(self, qbo_url: str):
self._qbo_url = qbo_url
self._headers = {
"accept": "application/json"
}

def machine_info(self):
def machine_info(self) -> MachineInfo:
req_url = urljoin(self._qbo_url, "machineInfo")
resp = requests.get(req_url, headers=self._headers, verify=False)
parsed_json = resp.json()
Expand All @@ -96,8 +117,14 @@ def maintenance_status(self) -> MaintenanceStatus:
parsed_json = resp.json()
return MaintenanceStatus(parsed_json)

def name(self):
def name(self) -> str:
req_url = urljoin(self._qbo_url, "settings/name")
resp = requests.get(req_url, headers=self._headers, verify=False)
parsed_json = resp.json()
return parsed_json['name']

def recipes(self) -> List[Recipe]:
req_url = urljoin(self._qbo_url, "recipes")
resp = requests.get(req_url, headers=self._headers, verify=False)
parsed_json = resp.json()
return [Recipe(parsed_json[index]) for index in parsed_json]
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="python-qbo",
version="0.2.0",
version="0.3.0",
author="Sebastian Rutofski",
author_email="kontakt@sebastian-rutofski.de",
description="",
Expand Down
111 changes: 110 additions & 1 deletion test/test_qbo.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,123 @@
from unittest import TestCase

import responses

from qbo import Qbo


class TestQbo(TestCase):
def setUp(self):
self.qbo = Qbo("https://test.test/")

@responses.activate
def test_recipes(self):
response_json = {
"index_0": {
"uid": "00000000-0000-0000-0000-000000000000",
"userId": "00000000-0000-0000-0000-000000000aaa",
"creatorId": "00000000-0000-0000-0000-000000000aaa",
"userName": "Testman",
"name": "Milkcoffee",
"coffeeAmount": 160,
"foamAmount": 0,
"milkAmount": 120,
"cupSize": 280,
"creationTimestamp": 1555994946,
"defaultRecipe": 0,
"usesHotMilk": 1,
"sequence": {
"index_0": 1,
"index_1": 3,
"index_2": 0,
"index_3": 0,
"index_4": 0
},
"favouriteCapsuleId": -1
},
"index_1": {
"uid": "00000000-0000-0000-0000-000000000001",
"userId": "00000000-0000-0000-0000-000000000aaa",
"creatorId": "00000000-0000-0000-0000-000000000aaa",
"userName": "Testman",
"name": "Coffee",
"coffeeAmount": 75,
"foamAmount": 80,
"milkAmount": 120,
"cupSize": 280,
"creationTimestamp": 1499801868,
"defaultRecipe": 0,
"usesHotMilk": 0,
"sequence": {
"index_0": 3,
"index_1": 4,
"index_2": 5,
"index_3": 2,
"index_4": 0
},
"favouriteCapsuleId": -1
},
"index_2": {
"uid": "00000000-0000-0000-0000-000000000002",
"userId": "00000000-0000-0000-0000-000000000aaa",
"creatorId": "00000000-0000-0000-0000-000000000aaa",
"userName": "Testman",
"name": "Cappuccino",
"coffeeAmount": 70,
"foamAmount": 50,
"milkAmount": 70,
"cupSize": 200,
"creationTimestamp": 1555479179,
"defaultRecipe": 0,
"usesHotMilk": 1,
"sequence": {
"index_0": 3,
"index_1": 4,
"index_2": 2,
"index_3": 0,
"index_4": 0
},
"favouriteCapsuleId": -1
}
}

responses.add(responses.GET, 'https://test.test/recipes', json=response_json, status=200)

recipes = self.qbo.recipes()

assert recipes is not None
assert len(recipes) == 3

from qbo.qbo import Recipe
from uuid import UUID

first_recipe = recipes[0]

assert isinstance(first_recipe, Recipe)
assert first_recipe.name == "Milkcoffee"

assert isinstance(first_recipe.uid, UUID)
assert first_recipe.uid == UUID("00000000-0000-0000-0000-000000000000")

assert first_recipe.uses_hot_milk is True

second_recipe = recipes[1]
assert isinstance(second_recipe, Recipe)
assert second_recipe.name == "Coffee"

assert isinstance(second_recipe.uid, UUID)
assert second_recipe.uid == UUID("00000000-0000-0000-0000-000000000001")

assert second_recipe.uses_hot_milk is False

third_recipe = recipes[2]
assert isinstance(third_recipe, Recipe)
assert third_recipe.name == "Cappuccino"

assert isinstance(third_recipe.uid, UUID)
assert third_recipe.uid == UUID("00000000-0000-0000-0000-000000000002")

assert third_recipe.uses_hot_milk is True

@responses.activate
def test_name(self):
response_json = {
Expand Down Expand Up @@ -47,7 +157,6 @@ def test_maintenance_status(self):
assert maintenance_status.machine_clean_status == 1
assert maintenance_status.rinsing_status == 3


@responses.activate
def test_machine_info(self):
response_json = {
Expand Down

0 comments on commit 31d09c8

Please sign in to comment.