Skip to content

Instantly share code, notes, and snippets.

@anish
Last active June 30, 2016 13:23
Show Gist options
  • Save anish/f3363dcf1b5f53475292ad52ddb18398 to your computer and use it in GitHub Desktop.
Save anish/f3363dcf1b5f53475292ad52ddb18398 to your computer and use it in GitHub Desktop.
bug in return code for remove label api
#!/usr/bin/env python
# -*-coding: utf8 -*-
'''
Sample output showing bug in api
Add label, expected return code of 200 : https://developer.github.com/v3/issues/labels/#add-labels-to-an-issue
Method : POST, Endpoint : https://api.github.com/repos/foobar-test/asdf/issues/8/labels, Payload ['bug']
Response Code 200
Remove a label from an issue , expected return code of 204 : https://developer.github.com/v3/issues/labels/#remove-a-label-from-an-issue
Method : DELETE, Endpoint : https://api.github.com/repos/foobar-test/asdf/issues/8/labels/bug, Payload None
Response Code 200
'''
from urllib2 import build_opener, HTTPSHandler, Request
import json
_REPO = '' # eg. 'foobar-test/asdf'
_ISSUE = '' # eg. '8'
_TOKEN = '' # authtoken
_URL = 'https://api.github.com'
_PATH = 'repos/' + _REPO
_ISSUEPATH = 'issues/' + _ISSUE
_METHOD_MAP = dict(
GET=lambda: 'GET',
PUT=lambda: 'PUT',
POST=lambda: 'POST',
PATCH=lambda: 'PATCH',
DELETE=lambda: 'DELETE')
def _encode_json(obj):
'''
Encode object as json str.
'''
def _dump_obj(obj):
if isinstance(obj, dict):
return obj
d = dict()
for k in dir(obj):
if not k.startswith('_'):
d[k] = getattr(obj, k)
return d
return json.dumps(obj, default=_dump_obj)
def _http(_method, url, _authorization, payload):
data = None
params = None
if _method == 'POST':
data = str(_encode_json(payload))
opener = build_opener(HTTPSHandler)
request = Request(url, data=data)
request.get_method = _METHOD_MAP[_method]
print 'Method : {0}, Endpoint : {1}, Payload {2}'.format(_method, url, payload)
if _authorization:
request.add_header('Authorization', _authorization)
if _method == 'POST':
request.add_header('Content-Type', 'application/x-www-form-urlencoded')
try:
response = opener.open(request, timeout=60)
print 'Response Code %s\n' % response.getcode()
except:
raise
print 'Add label, expected return code of 200 : https://developer.github.com/v3/issues/labels/#add-labels-to-an-issue'
_http('POST', '/'.join([_URL, _PATH, _ISSUEPATH, 'labels']), _authorization, ['bug'])
print 'Remove a label from an issue , expected return code of 204 : https://developer.github.com/v3/issues/labels/#remove-a-label-from-an-issue'
_http('DELETE', '/'.join([_URL, _PATH, _ISSUEPATH, 'labels', 'bug']), _authorization, None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment