Repository: Shopify/shopify_python_api

Issue:
Attaching Images - Decode required for Python 3 compatibility
When attaching an image from a local file:

```
with open("some_image.jpg","rb") as f:
    img = f.read()
    image1.attach_image(data=img)
```

img will be bytes while the attachment needs to be a string to the JSONified. So in image.py:

```
def attach_image(self, data, filename=None):
    self.attributes["attachment"] = base64.b64encode(data).decode() #decode() for Python 3
    if filename:
        self.attributes["filename"] = filename
```

I am not sure how to keep it Python 2 compatible.


Discussion:

Patch:
diff --git a/shopify/resources/image.py b/shopify/resources/image.py
index a0e82ef..b408261 100644
--- a/shopify/resources/image.py
+++ b/shopify/resources/image.py
@@ -23,7 +23,7 @@ class Image(ShopifyResource):
             return super(Image, self).__getattr__(name)
 
     def attach_image(self, data, filename=None):
-        self.attributes["attachment"] = base64.b64encode(data)
+        self.attributes["attachment"] = base64.b64encode(data).decode()
         if filename:
             self.attributes["filename"] = filename
 


Test patch:
diff --git a/test/image_test.py b/test/image_test.py
index 1234898..bde789e 100644
--- a/test/image_test.py
+++ b/test/image_test.py
@@ -1,5 +1,6 @@
 import shopify
 from test.test_helper import TestCase
+import base64
 
 class ImageTest(TestCase):
 
@@ -13,6 +14,19 @@ class ImageTest(TestCase):
         self.assertEqual('http://cdn.shopify.com/s/files/1/0006/9093/3842/products/ipod-nano.png?v=1389388540', image.src)
         self.assertEqual(850703190, image.id)
 
+    def test_attach_image(self):
+        self.fake("products/632910392/images", method='POST', body=self.load_fixture('image'), headers={'Content-type': 'application/json'})
+        image = shopify.Image({'product_id':632910392})
+        image.position = 1
+        binary_in = base64.b64decode("R0lGODlhbgCMAPf/APbr48VySrxTO7IgKt2qmKQdJeK8lsFjROG5p/nz7Zg3MNmnd7Q1MLNVS9GId71hSJMZIuzTu4UtKbeEeakhKMl8U8WYjfr18YQaIbAf==")
+        image.attach_image(data=binary_in, filename='ipod-nano.png')
+        image.save()
+        binary_out = base64.b64decode(image.attachment)
+
+        self.assertEqual('http://cdn.shopify.com/s/files/1/0006/9093/3842/products/ipod-nano.png?v=1389388540', image.src)
+        self.assertEqual(850703190, image.id)
+        self.assertEqual(binary_in, binary_out)
+
     def test_create_image_then_add_parent_id(self):
         self.fake("products/632910392/images", method='POST', body=self.load_fixture('image'), headers={'Content-type': 'application/json'})
         image = shopify.Image()