# AUTOGENERATED! DO NOT EDIT! File to edit: pynamodbAttributes.ipynb (unless otherwise specified).

__all__ = ['SchemaAttribute']

# Cell
from pynamodb.attributes import Attribute
import pynamodb

# Cell
from typing import Any, Optional, Type, TypeVar
from enum import Enum
import requests, dpath.util, yaml, jsonschema


class SchemaAttribute(Attribute):
  attr_type = pynamodb.constants.STRING
  def __init__(self, schemaUrl:str, path:str = '/', isYaml=True, headers={'Cache-Control': 'no-cache'}, **kwargs: Any) -> None:
      """
      :yaml::Bool:: whether the schema is in yaml or json
      :path::str:: the path of the object of interest in schema, if the schema is at root then '/'
      """
      super().__init__(**kwargs)
      if isYaml: # yaml schema
        schema:dict = yaml.load(requests.get(schemaUrl, headers=headers).text, Loader = yaml.FullLoader)
      else: # probably json
        schema:dict = requests.get(schemaUrl, headers).json()

      self.schema = dpath.util.get(schema, path) # get to the path in schema
      print(self.schema)

  def deserialize(self, value: str) -> dict:
    return json.loads(value)

  def serialize(self, value:dict) -> str:
    res = jsonschema.validate(value,self.schema)
    print(res)
    return json.dumps(value)