Skip to content

Commit

Permalink
Merge pull request #140 from PiBrewing/brewfatherAPIV2Test
Browse files Browse the repository at this point in the history
Brewfather apiv2 test
  • Loading branch information
avollkopf authored Jun 18, 2024
2 parents eb1365d + 31aa275 commit 0f7308b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 22 deletions.
2 changes: 1 addition & 1 deletion cbpi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__version__ = "4.4.1"
__version__ = "4.4.2.a3"
__codename__ = "Yeast Starter"

59 changes: 38 additions & 21 deletions cbpi/controller/upload_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import re
import base64


class UploadController:

def __init__(self, cbpi):
Expand Down Expand Up @@ -70,9 +69,12 @@ async def get_json_recipes(self):
return []

async def get_brewfather_recipes(self,offset=0):
limit = 50
#loop=0
repeat = True
brewfather = True
result=[]
self.url="https://api.brewfather.app/v1/recipes"
self.url="https://api.brewfather.app/v2/recipes"
brewfather_user_id = self.cbpi.config.get("brewfather_user_id", None)
if brewfather_user_id == "" or brewfather_user_id is None:
brewfather = False
Expand All @@ -84,25 +86,40 @@ async def get_brewfather_recipes(self,offset=0):
if brewfather == True:
encodedData = base64.b64encode(bytes(f"{brewfather_user_id}:{brewfather_api_key}", "ISO-8859-1")).decode("ascii")
headers={"Authorization": "Basic %s" % encodedData}
parameters={"limit": 50, 'offset': offset}
async with aiohttp.ClientSession(headers=headers) as bf_session:
async with bf_session.get(self.url, params=parameters) as r:
bf_recipe_list = await r.json()
await bf_session.close()

if bf_recipe_list:
for row in bf_recipe_list:
recipe_id = row['_id']
name = row['name']
element = {'value': recipe_id, 'label': name}
result.append(element)
return result
else:
return []

else:
return []
parameters={"limit": limit}
while repeat == True:
try:
async with aiohttp.ClientSession(headers=headers) as bf_session:
async with bf_session.get(self.url, params=parameters) as r:
bf_recipe_list = await r.json()
await bf_session.close()
except Exception as e:
logging.error(e)

if bf_recipe_list:
#loop +=1
for row in bf_recipe_list:
recipe_id = row['_id']
name = row['name']
element = {'value': recipe_id, 'label': name}
result.append(element)
else:
repeat = False

if len(bf_recipe_list) != limit:
#logging.info(loop)
repeat = False
else:
parameters={"limit": limit, 'start_after': recipe_id}

#logging.info(len(result))
try:
newlist = sorted(result, key=lambda d: d['label'])
#logging.info(newlist)
return newlist
except:
return result


def get_creation_path(self):
creation_path = self.cbpi.config.get("RECIPE_CREATION_PATH", "upload")
Expand Down Expand Up @@ -738,7 +755,7 @@ async def bf_recipe_creation(self, Recipe_ID):

brewfather = True
result=[]
self.bf_url="https://api.brewfather.app/v1/recipes/" + Recipe_ID
self.bf_url="https://api.brewfather.app/v2/recipes/" + Recipe_ID
brewfather_user_id = self.cbpi.config.get("brewfather_user_id", None)
if brewfather_user_id == "" or brewfather_user_id is None:
brewfather = False
Expand Down
16 changes: 16 additions & 0 deletions cbpi/extension/ConfigUpdate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ async def run(self):
AddMashIn = self.cbpi.config.get("AddMashInStep", None)
bfuserid = self.cbpi.config.get("brewfather_user_id", None)
bfapikey = self.cbpi.config.get("brewfather_api_key", None)
bflistlength = self.cbpi.config.get("brewfather_list_length", None)
RecipeCreationPath = self.cbpi.config.get("RECIPE_CREATION_PATH", None)
BoilKettle = self.cbpi.config.get("BoilKettle", None)
CONFIG_STATUS = self.cbpi.config.get("CONFIG_STATUS", None)
Expand Down Expand Up @@ -244,6 +245,21 @@ async def run(self):
await self.cbpi.config.add("brewfather_api_key", "", type=ConfigType.STRING, description="Brewfather API Key", source="craftbeerpi")
except:
logger.warning('Unable to update config')

## Check if Brewfather API Key is in config

if bflistlength is None:
logger.info("INIT Brewfather Recipe List Length")
try:
await self.cbpi.config.add("brewfather_list_length", 50, type=ConfigType.SELECT, description="Brewfather Recipe List length",
source="craftbeerpi",
options= [{"label": "5", "value": 5},
{"label": "10", "value": 10},
{"label": "25", "value": 25},
{"label": "50", "value": 50},
{"label": "100", "value": 100}])
except:
logger.warning('Unable to update config')

## Check if Brewfather API Key is in config

Expand Down

0 comments on commit 0f7308b

Please sign in to comment.