import json
import hashlib

from wdim.client.storable import Storable


class Blob(Storable):

    HASH_METHOD = 'sha1'

    @classmethod
    def _create(cls, data):
        sha = hashlib(cls.HASH_METHOD, json.dumps(data))
        return cls(sha, data)

    @classmethod
    def _from_document(cls, document):
        return cls(document['data'])

    @property
    def hash(self):
        return self._id

    def __init__(self, data):
        self.data = data

    def to_document(self):
        return {
            '_id': self.hash,
            'data': self.data
        }

import json
import hashlib

from wdim import exceptions
from wdim.client import fields
from wdim.client.storable import Storable


class Blob(Storable):

    HASH_METHOD = 'sha256'

    _id = fields.StringField(unique=True)
    data = fields.DictField()

    @classmethod
    async def create(cls, data):
        sha = hashlib.new(cls.HASH_METHOD, json.dumps(data).encode('utf-8')).hexdigest()
        try:
            # Classmethod supers need arguments for some reason
            return await super(Blob, cls).create(_id=sha, data=data)
        except exceptions.UniqueViolation:
            return await cls.load(sha)

    @property
    def hash(self):
        return self._id

Reimplement Blob, switch to sha256
Reimplement Blob, switch to sha256

  import json
  import hashlib
  
+ from wdim import exceptions
+ from wdim.client import fields
  from wdim.client.storable import Storable
  
  
  class Blob(Storable):
  
-     HASH_METHOD = 'sha1'
+     HASH_METHOD = 'sha256'
+ 
+     _id = fields.StringField(unique=True)
+     data = fields.DictField()
  
      @classmethod
-     def _create(cls, data):
+     async def create(cls, data):
-         sha = hashlib(cls.HASH_METHOD, json.dumps(data))
+         sha = hashlib.new(cls.HASH_METHOD, json.dumps(data).encode('utf-8')).hexdigest()
-         return cls(sha, data)
- 
-     @classmethod
-     def _from_document(cls, document):
-         return cls(document['data'])
+         try:
+             # Classmethod supers need arguments for some reason
+             return await super(Blob, cls).create(_id=sha, data=data)
+         except exceptions.UniqueViolation:
+             return await cls.load(sha)
  
      @property
      def hash(self):
          return self._id
  
-     def __init__(self, data):
-         self.data = data
- 
-     def to_document(self):
-         return {
-             '_id': self.hash,
-             'data': self.data
-         }
- 
Reimplement Blob, switch to sha256
## Code Before:
import json
import hashlib

from wdim.client.storable import Storable


class Blob(Storable):

    HASH_METHOD = 'sha1'

    @classmethod
    def _create(cls, data):
        sha = hashlib(cls.HASH_METHOD, json.dumps(data))
        return cls(sha, data)

    @classmethod
    def _from_document(cls, document):
        return cls(document['data'])

    @property
    def hash(self):
        return self._id

    def __init__(self, data):
        self.data = data

    def to_document(self):
        return {
            '_id': self.hash,
            'data': self.data
        }

## Instruction:
Reimplement Blob, switch to sha256
## Code After:
import json
import hashlib

from wdim import exceptions
from wdim.client import fields
from wdim.client.storable import Storable


class Blob(Storable):

    HASH_METHOD = 'sha256'

    _id = fields.StringField(unique=True)
    data = fields.DictField()

    @classmethod
    async def create(cls, data):
        sha = hashlib.new(cls.HASH_METHOD, json.dumps(data).encode('utf-8')).hexdigest()
        try:
            # Classmethod supers need arguments for some reason
            return await super(Blob, cls).create(_id=sha, data=data)
        except exceptions.UniqueViolation:
            return await cls.load(sha)

    @property
    def hash(self):
        return self._id
