mirror of
https://github.com/kennethreitz/gistapi.py.git
synced 2026-06-05 23:10:17 +00:00
Merge branch 'feature/requests' into develop
This commit is contained in:
+8
-14
@@ -35,7 +35,7 @@ u'Great Stuff.'
|
||||
import cStringIO
|
||||
import os.path
|
||||
|
||||
import urllib2
|
||||
import requests
|
||||
from datetime import datetime
|
||||
|
||||
try:
|
||||
@@ -97,7 +97,7 @@ class Gist(object):
|
||||
else:
|
||||
# Fetch Gist metadata
|
||||
_meta_url = GIST_JSON % self.id
|
||||
_meta = json.load(urllib2.urlopen(_meta_url))['gists'][0]
|
||||
_meta = json.loads(requests.get(_meta_url).content)['gists'][0]
|
||||
|
||||
for key, value in _meta.iteritems():
|
||||
|
||||
@@ -111,7 +111,7 @@ class Gist(object):
|
||||
setattr(self, key, value)
|
||||
elif key == 'created_at':
|
||||
# Attach datetime
|
||||
datetime.strptime(value[:-6], '%Y/%m/%d %H:%M:%S')
|
||||
setattr(self, 'created_at', datetime.strptime(value[:-6], '%Y/%m/%d %H:%M:%S'))
|
||||
|
||||
elif key == 'comments':
|
||||
_comments = []
|
||||
@@ -127,14 +127,9 @@ class Gist(object):
|
||||
|
||||
def _post(self, params, headers={}):
|
||||
"""POST to the web form (internal method)."""
|
||||
request = urllib2.Request(self.post_url,
|
||||
urllib.urlencode(params),
|
||||
headers)
|
||||
try:
|
||||
response = urllib2.urlopen(request)
|
||||
except IOError, exc:
|
||||
response = exc
|
||||
return response.code, response.msg
|
||||
r = requests.post(self.post_url, params, headers=headers)
|
||||
|
||||
return r.status_code, r.content
|
||||
|
||||
def reset(self):
|
||||
"""Clear the local cache."""
|
||||
@@ -204,9 +199,8 @@ class Gist(object):
|
||||
for fn in self._meta['files']:
|
||||
# Grab file contents
|
||||
_file_url = GIST_BASE % 'raw/%s/%s' % (self.id, urllib2.quote(fn))
|
||||
# _files[fn] = unicode(urllib2.urlopen(_file_url).read())
|
||||
_files[fn] = cStringIO.StringIO()
|
||||
_files[fn].write(urllib2.urlopen(_file_url).read())
|
||||
_files[fn].write(requests.get(_file_url).content)
|
||||
|
||||
return _files
|
||||
|
||||
@@ -228,7 +222,7 @@ class Gists(object):
|
||||
|
||||
# Return a list of Gist objects
|
||||
return [Gist(json=g)
|
||||
for g in json.load(urllib2.urlopen(_url))['gists']]
|
||||
for g in json.loads(requests.get(_url).content)['gists']]
|
||||
|
||||
|
||||
class GistComment(object):
|
||||
|
||||
@@ -9,36 +9,39 @@ from distutils.core import setup
|
||||
|
||||
|
||||
def publish():
|
||||
|
||||
"""Publish to PyPi"""
|
||||
os.system("python setup.py sdist upload")
|
||||
|
||||
|
||||
if sys.argv[-1] == "publish":
|
||||
publish()
|
||||
sys.exit()
|
||||
|
||||
|
||||
required = []
|
||||
required = ['requests']
|
||||
|
||||
|
||||
if sys.version_info[:2] < (2,6):
|
||||
required.append('simplejson')
|
||||
|
||||
setup(name='gistapi',
|
||||
version=gistapi.__version__,
|
||||
description='Python wrapper for Gist API',
|
||||
long_description=open('README.rst').read() + '\n\n' +
|
||||
open('HISTORY.rst').read(),
|
||||
author='Kenneth Reitz',
|
||||
author_email='me@kennethreitz.com',
|
||||
url='http://github.com/kennethreitz/gistapi.py',
|
||||
packages=['gistapi'],
|
||||
install_requires=required,
|
||||
license='MIT',
|
||||
classifiers=(
|
||||
|
||||
setup(
|
||||
name='gistapi',
|
||||
version=gistapi.__version__,
|
||||
description='Python wrapper for Gist API',
|
||||
long_description=open('README.rst').read() + '\n\n' + open('HISTORY.rst').read(),
|
||||
author='Kenneth Reitz',
|
||||
author_email='me@kennethreitz.com',
|
||||
url='http://github.com/kennethreitz/gistapi.py',
|
||||
packages=['gistapi'],
|
||||
install_requires=required,
|
||||
license='MIT',
|
||||
classifiers=(
|
||||
"Development Status :: 4 - Beta",
|
||||
"License :: OSI Approved :: MIT License",
|
||||
"Programming Language :: Python",
|
||||
"Programming Language :: Python :: 2.5",
|
||||
"Programming Language :: Python :: 2.6",
|
||||
"Programming Language :: Python :: 2.7",
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import unittest
|
||||
|
||||
import gistapi
|
||||
from gistapi import Gist, Gists
|
||||
|
||||
|
||||
class RequestsTestSuite(unittest.TestCase):
|
||||
"""Requests test cases."""
|
||||
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
def tearDown(self):
|
||||
"""Teardown."""
|
||||
pass
|
||||
|
||||
def test_repo_fetch(self):
|
||||
r1 = Gist('d4507e882a07ac6f9f92').repo
|
||||
r2 = u'd4507e882a07ac6f9f92'
|
||||
|
||||
self.assertEqual(r1, r2)
|
||||
|
||||
def test_owner_fetch(self):
|
||||
r1 = Gist('d4507e882a07ac6f9f92').owner
|
||||
r2 = u'kennethreitz'
|
||||
|
||||
self.assertEqual(r1, r2)
|
||||
|
||||
def test_created_at_fetch(self):
|
||||
r1 = Gist('d4507e882a07ac6f9f92').created_at.isoformat()
|
||||
r2 = '2010-05-16T10:51:15'
|
||||
|
||||
self.assertEqual(r1, r2)
|
||||
|
||||
def test_public_fetch(self):
|
||||
r1 = Gist('d4507e882a07ac6f9f92').public
|
||||
r2 = False
|
||||
|
||||
self.assertEqual(r1, r2)
|
||||
|
||||
def test_fetch_filesnames(self):
|
||||
r1 = Gist('d4507e882a07ac6f9f92').filenames
|
||||
r2 = ['exampleEmptyFile', 'exampleFile']
|
||||
|
||||
self.assertEqual(r1, r2)
|
||||
|
||||
def test_gist_search(self):
|
||||
r1 = Gists.fetch_by_user('kennethreitz')[-1].description
|
||||
r2 = u'My .bashrc configuration'
|
||||
|
||||
self.assertEqual(r1, r2)
|
||||
|
||||
def test_gist_comments(self):
|
||||
r1 = Gist(885658).comments[0].body
|
||||
r2 = u'Great stuff.'
|
||||
|
||||
self.assertEqual(r1, r2)
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -0,0 +1,17 @@
|
||||
[tox]
|
||||
envlist = py25,py26,py27,py3
|
||||
|
||||
[testenv]
|
||||
commands=py.test --junitxml=junit-{envname}.xml
|
||||
deps = pytest
|
||||
|
||||
[testenv:py25]
|
||||
simplejson = pytest simplejson
|
||||
|
||||
[testenv:pypy]
|
||||
basepython=/usr/bin/pypy-c
|
||||
simplejson = pytest simplejson
|
||||
|
||||
[testenv:py3]
|
||||
basepython=/usr/bin/python3
|
||||
simplejson = pytest
|
||||
Reference in New Issue
Block a user