def update_product_features(self, brand, model, variant, features: dict): # TODO: tests url = f"/v2/products/{brand}/{model}/{variant}/features" self.patch(url, json.dumps(features)) if self.response.status_code == 200 or self.response.status_code == 204: return True elif self.response.status_code == 400: raise Errors.ValidationError("Impossible to update feature/s") elif self.response.status_code == 404: raise Errors.ProductNotFoundError(f"Product doesn't exist")
def update_item_features(self, code: str, features: dict): # TODO: add the update feature for the products, 2 functions? """ Send updated features to the database (this is the PATCH endpoint) """ self.patch(['/v2/items/', self.urlencode(code), '/features'], json.dumps(features)) if self.response.status_code == 200 or self.response.status_code == 204: return True elif self.response.status_code == 400: raise Errors.ValidationError("Impossible to update feature/s") elif self.response.status_code == 404: raise Errors.ItemNotFoundError(f"Item {code} doesn't exist")
def get_codes_by_feature(self, feature: str, value: str): url = f"/v2/features/{self.urlencode(feature)}/{self.urlencode(value)}" items = self.get(url) if items.status_code == 200: return items.json() elif items.status_code == 400: exception = items.json() raise Errors.ValidationError(exception.get('message', 'No message from the server')) else: raise RuntimeError("Unexpected return code")
def move(self, code, location): """ Move an item to another location """ move_status = self.put( ['v2/items/', self.urlencode(code), '/parent'], json.dumps(location)).status_code if move_status == 204 or move_status == 201: return True elif move_status == 400: raise Errors.ValidationError(f"Cannot move {code} into {location}") elif move_status == 404: response_json = json.loads(self.response.content) if 'item' not in response_json: raise Errors.ServerError("Server didn't find an item, but isn't telling us which one") if response_json['item'] == location: raise Errors.LocationNotFoundError else: raise Errors.ItemNotFoundError(f"Item {response_json['item']} doesn't exist") else: raise RuntimeError(f"Move failed with {move_status}")
def get_item(self, code, depth_limit=None): """This method returns an Item instance received from the server""" url = '/v2/items/' + self.urlencode(code) + '?separate' # try an Item without product6 if depth_limit is not None: url += '?depth=' + str(int(depth_limit)) self.get(url) if self.response.status_code == 200: item = Item(json.loads(self.response.content)) return item elif self.response.status_code == 404: raise Errors.ItemNotFoundError(f"Item {code} doesn't exist")
def get_product(self, brand, model, variant): """Retrieve a product from the server Args: self Returns: Product """ url = '/v2/products/' + brand + '/' + model + '/' + variant self.get(url) if self.response.status_code == 200: res = json.loads(self.response.content) p = Product(res) return p elif self.response.status_code == 404: raise Errors.ProductNotFoundError("Product doesn't exists.")
def get_product_list(self, brand, model): """returns an list of Product retrieved from the server Args: self, brand: str, model: str, variant: str Returns: list of Products """ url = '/v2/products/' + brand + '/' + model self.get(url) if self.response.status_code == 200: res = json.loads(self.response.content) product_list = [] for p in res: product_list.append(Product(p)) return product_list elif self.response.status_code == 404: raise Errors.ProductNotFoundError("Product doesn't exists.")
def get_history(self, code, limit: Optional[int] = None): url = f'/v2/items/{self.urlencode(code)}/history' if limit is not None: url += '?length=' + str(int(limit)) history = self.get(url) if history.status_code == 200: result = [] for entry in history.json(): try: change = AuditChanges(entry["change"]) except ValueError: change = AuditChanges.Unknown result.append(AuditEntry(entry["user"], change, float(entry["time"]), entry["other"])) return result elif history.status_code == 404: raise Errors.ItemNotFoundError(f"Item {code} doesn\'t exist") else: raise RuntimeError("Unexpected return code")