from collections import defaultdict
from utils import Enum


class TinyPNGError(Enum):
    """List of error codes returned by TinyPNG API"""
    Unauthorized = 1
    InputMissing = 2
    BadSignature = 3
    DecodeError = 4
    TooManyRequests = 5
    InternalServerError = 6


FATAL_ERRORS = (TinyPNGError.Unauthorized, TinyPNGError.TooManyRequests)


class TinyPNGResponse:
    """Immutable class to represent TinyPNG response"""

    SUCCESS_CODE = 201

    def __init__(self, status, **kwargs):
        self._status = status
        self._response = kwargs
        self._properties = defaultdict(lambda: None)
        self._properties.update(kwargs)

    @property
    def status(self):
        return self._status

    @property
    def success(self):
        return self.status == self.SUCCESS_CODE

    @property
    def failure(self):
        return not self.success

    @property
    def errno(self):
        err = self._properties['error']
        if err:
            return TinyPNGError.from_value(err)
        return None

    @property
    def errmsg(self):
        return self._properties['message']

    @property
    def input_size(self):
        input = self._properties['input']
        if input and 'size' in input:
            return input['size']
        return None

    @property
    def output_size(self):
        output = self._properties['output']
        if output and 'size' in output:
            return output['size']
        return None

    @property
    def output_ratio(self):
        output = self._properties['output']
        if output and 'ratio' in output:
            return output['ratio']
        return None

    @property
    def compressed_image_url(self):
        return self._properties['location']

    @property
    def bytes(self):
        return self._properties['bytes']

@@ -473,22 +473,29 @@
 _(self, 
-status
+response_code
 , **kwar
@@ -517,23 +517,37 @@
 lf._
-status = status
+response_code = response_code
 %0A   
@@ -691,22 +691,29 @@
     def 
-status
+response_code
 (self):%0A
@@ -733,22 +733,29 @@
 n self._
-status
+response_code
 %0A%0A    @p
@@ -809,14 +809,21 @@
 elf.
-status
+response_code
  == 
@@ -1204,16 +1204,17 @@
    input
+_
  = self.
@@ -1254,28 +1254,9 @@
 nput
- and 'size' in input
+_
 :%0A  
@@ -1277,24 +1277,29 @@
 rn input
-%5B
+_.get(
 'size'
-%5D
+)
 %0A       
@@ -1412,29 +1412,8 @@
  if 
-output and 'size' in 
 outp
@@ -1445,16 +1445,20 @@
 tput
-%5B
+.get(
 'size'
-%5D
+)
 %0A   
@@ -1576,30 +1576,8 @@
  if 
-output and 'ratio' in 
 outp
@@ -1609,17 +1609,21 @@
 tput
-%5B
+.get(
 'ratio'
-%5D
+)
 %0A   

change variable names