Skip to content
This repository has been archived by the owner on Apr 23, 2024. It is now read-only.

Bug: Error if product group is not set #215

Closed
andreheuer opened this issue Jan 17, 2022 · 3 comments
Closed

Bug: Error if product group is not set #215

andreheuer opened this issue Jan 17, 2022 · 3 comments
Labels
bug Something isn't working

Comments

@andreheuer
Copy link
Contributor

When requesting a product for which the product group is not set, an exception is raised by pygrocy.
Following example from the demo instance:

from pygrocy import Grocy
grocy = Grocy('https://demo.grocy.info','',port=443)
product = grocy.product(27)

This raises the following exception:

Traceback (most recent call last):
  File "/mnt/d/Code/grocy-openfoodfacts/test.py", line 5, in <module>
    product = grocy.product(27)
  File "/home/andreheuer/.local/lib/python3.8/site-packages/pygrocy/grocy.py", line 96, in product
    resp = self._api_client.get_product(product_id)
  File "/home/andreheuer/.local/lib/python3.8/site-packages/pygrocy/grocy_api_client.py", line 349, in get_product
    return ProductDetailsResponse(**parsed_json)
  File "pydantic/main.py", line 406, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validation error for ProductDetailsResponse
product -> product_group_id
  value is not a valid integer (type=type_error.integer)
@SebRut
Copy link
Owner

SebRut commented Jan 17, 2022

I guess this is related to this grocy/grocy#1683.
The API response was changed back to string and so the pygrocy code has to be fixed too.

@blue-hound
Copy link

blue-hound commented Mar 16, 2022

Hi,
I'd do a pull request for this but my Python isn't great and I don't know if this is the preferred way of fixing this.

I fixed the product_group_id problem by importing validator from pydantic and adding the second code block to the end of the ProductData definition. Not sure why the definition doesn't work that already exists but as I said, my Python is terrible.

from pydantic import BaseModel, Field, validator

validator('product_group_id', pre=True)            
def blank_string(value, field):                     
   if value == "":                                 
      return None                                  
   return value

@andreheuer
Copy link
Contributor Author

fixed with #234

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants