# -*- coding: utf-8 -*-

import requests, json

class Gist:
	BASE_URL = 'https://api.github.com'

	def __init__ (self, token):
		response = requests.get(self.BASE_URL + '/user?access_token=' + token)
		if response.status_code != 200:
			raise Exception('The entered token is invalid')
		else:
			self.__userData = response.json()
			self.__accessToken = token
			self.__headers = {
				'X-Github-Username': self.__userData.get('login'),
				'Content-Type': 'application/json',
				'Authorization': 'token %s' %token
			}
			self.__defaults = {
				'public': False,
				'description': 'Sublime Text Configuration',
				'files': {}
			}

	def create (self, gistData):
		_data = json.dumps(dict(self.__defaults, **gistData))

		response = requests.post(
			self.BASE_URL + '/gists',
			data = _data,
			headers = self.__headers
		)

		if response.status_code == 201:
			return response.json()

		raise Exception(Gist.__getResponseError('Gist can\'t created', response))

	def edit (self, gistId, gistData):
		gistData = json.dumps(dict(self.__defaults, **gistData))
		response = requests.patch(
			self.BASE_URL + '/gists/%s' %gistId,
			data=gistData,
			headers=self.__headers
		)

		if response.status_code == 200:
			return response.json()

		raise Exception(Gist.__getResponseError('Can\'t edit the gist', response))

	def list (self):
		listUrl = (
			self.BASE_URL + '/users/',
			self.__userData.get('login') + '/gists'
		)

		response = requests.get(''.join(listUrl))
		if response.status_code == 200:
			return response.json()

		raise Exception(Gist.__getResponseError('It is not possible to list files', reponse))

	def delete (self, gistId):
		response = requests.delete(
			self.BASE_URL + '/gists/' + gistId,
			headers=self.__headers
		)

		if response.status_code == 204:
			return True

		raise Exception(Gist.__getResponseError('The Gist can be deleted', response))

	def get (self, gistId):
		response = requests.get(self.BASE_URL + '/gists/' + gistId)
		if response.status_code == 200:
			return response.json()

		raise Exception(Gist.__getResponseError('The gist not exist', response))

	@staticmethod
	def __getResponseError (message, response):
		rjson = response.json()
		errorDescription = "Code " + str(response.status_code) + " - " + rjson.get('message')
		return {
			'app_message': message,
			'error_description': errorDescription
		}

	@staticmethod
	def getCurrentRelease ():
		response = requests.get(Gist.BASE_URL + '/repos/mfuentesg/SyncSettings/releases/latest')
		if response.status_code == 200:
			return response.json()
		raise Exception(Gist.__getResponseError('Repository troubles', response))

@@ -41,16 +41,113 @@
 , json%0A%0A
+class GistException (Exception):%0A%09def toJSON (self):%0A%09%09return json.loads(json.dumps(e.args%5B0%5D))%0A%0A
 class Gi
@@ -150,16 +150,16 @@
 s Gist:%0A
-
 %09BASE_UR
@@ -325,32 +325,36 @@
 = 200:%0A%09%09%09raise 
+Gist
 Exception('The e
@@ -347,16 +347,40 @@
 ception(
+Gist.__getResponseError(
 'The ent
@@ -401,16 +401,27 @@
 invalid'
+, response)
 )%0A%09%09else
@@ -1020,32 +1020,36 @@
 json()%0A%0A%09%09raise 
+Gist
 Exception(Gist._
@@ -1375,32 +1375,36 @@
 json()%0A%0A%09%09raise 
+Gist
 Exception(Gist._
@@ -1672,32 +1672,36 @@
 json()%0A%0A%09%09raise 
+Gist
 Exception(Gist._
@@ -1943,32 +1943,36 @@
 n True%0A%0A%09%09raise 
+Gist
 Exception(Gist._
@@ -2176,32 +2176,36 @@
 json()%0A%0A%09%09raise 
+Gist
 Exception(Gist._
@@ -2707,24 +2707,24 @@
 .json()%0A
-
 %09%09raise 
 Exceptio
@@ -2715,16 +2715,20 @@
 %09%09raise 
+Gist
 Exceptio

add custom Exception to allow get the error on json format