Skip to content

Commit

Permalink
new methods api_request, use_proxy and Readme correction
Browse files Browse the repository at this point in the history
  • Loading branch information
trico committed Dec 21, 2011
1 parent c83b7ee commit 4667534
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 64 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -64,14 +64,14 @@ There is a key bindings:

From menu items:

* Main menu: Tools -> Gist -> "Create Gist"
* Main menu: Tools -> Gist -> "Get Gist List"

By command called "Gist: Get Gist List".

There is a key bindings:

* Windows and Linux: "ctrl+shift+g"
* OS X: "super+super+g"
* OS X: "super+shift+g"

**The content of your selected gist will be copied into the clipboard**

Expand Down
95 changes: 33 additions & 62 deletions gist.py
Expand Up @@ -9,81 +9,28 @@
_selectedText = ''
_fileName = ''
newlist2 = []
settings = sublime.load_settings('Gist.sublime-settings')
url = 'https://api.github.com/gists'

def create_gist(description):
settings = sublime.load_settings('Gist.sublime-settings')

url = 'https://api.github.com/gists'

data = json.dumps({ 'description': description,
'public': settings.get('create_public', DEFAULT_CREATE_PUBLIC_VALUE),
'files': {
_fileName: {'content': _selectedText}
}})

headers = { 'Authorization': 'Basic ' + base64.urlsafe_b64encode("%s:%s" % (settings.get('username'), settings.get('password'))),
'Accept': 'application/json',
'Content-Type': 'application/json',
'Content-Length': len(data)}

request = urllib2.Request(url, data, headers)

if settings.get('use_proxy') == 'true':
opener = urllib2.build_opener(
urllib2.HTTPHandler(),
urllib2.HTTPSHandler(),
urllib2.ProxyHandler({'https': settings.get('proxy')}))
urllib2.install_opener(opener)

response = urllib2.urlopen(request)
result = api_request(url, data)

result = json.loads(response.read())
sublime.set_clipboard(result['html_url'])

def get_gist(id):
settings = sublime.load_settings('Gist.sublime-settings')
def get_gist(url_gist):
result = api_request(url_gist)

url = id

request = urllib2.Request(url)

request.add_header('Authorization', 'Basic ' + base64.urlsafe_b64encode("%s:%s" % (settings.get('username'), settings.get('password'))))
request.add_header('Accept', 'application/json')
request.add_header('Content-Type', 'application/json')

if settings.get('use_proxy') == 'true':
opener = urllib2.build_opener(
urllib2.HTTPHandler(),
urllib2.HTTPSHandler(),
urllib2.ProxyHandler({'https': settings.get('proxy')}))
urllib2.install_opener(opener)

response = urllib2.urlopen(request)

result = json.loads(response.read())
for x in result['files']:
sublime.set_clipboard(result['files'][x]['content'])

def get_gists():
settings = sublime.load_settings('Gist.sublime-settings')

url = 'https://api.github.com/gists'

request = urllib2.Request(url)
request.add_header('Authorization', 'Basic ' + base64.urlsafe_b64encode("%s:%s" % (settings.get('username'), settings.get('password'))))
request.add_header('Accept', 'application/json')
request.add_header('Content-Type', 'application/json')

if settings.get('use_proxy') == 'true':
opener = urllib2.build_opener(
urllib2.HTTPHandler(),
urllib2.HTTPSHandler(),
urllib2.ProxyHandler({'https': settings.get('proxy')}))
urllib2.install_opener(opener)

response = urllib2.urlopen(request)

result = json.loads(response.read())
def get_gists():
result = api_request(url)

newlist = []
global newlist2
Expand All @@ -97,6 +44,31 @@ def get_gists():

return newlist

def api_request(url_api, data = ''):
request = urllib2.Request(url_api)
request.add_header('Authorization', 'Basic ' + base64.urlsafe_b64encode("%s:%s" % (settings.get('username'), settings.get('password'))))
request.add_header('Accept', 'application/json')
request.add_header('Content-Type', 'application/json')

if len(data)>0:
request.add_data(data)

if settings.get('use_proxy') == 'true':
use_proxy(urllib2)

response = urllib2.urlopen(request)

return json.loads(response.read())

def use_proxy(urllib2):
opener = urllib2.build_opener(
urllib2.HTTPHandler(),
urllib2.HTTPSHandler(),
urllib2.ProxyHandler({'https': settings.get('proxy')})
)

return urllib2.install_opener(opener)


class PromptGistCommand(sublime_plugin.WindowCommand):
def run(self):
Expand All @@ -123,8 +95,7 @@ def run(self, edit):
self.view.window().run_command('prompt_gist')

class GistlistCommand(sublime_plugin.WindowCommand):
def run(self):

def run(self):
self.window.show_quick_panel(get_gists(), self.on_done)

def on_done(self, num):
Expand Down

0 comments on commit 4667534

Please sign in to comment.