]> khamidou — GitHub mirrors - electron.git/commitdiff
Handle the assets uploading in the GitHub API library.
authorCheng Zhao <zcbenz@gmail.com>
Thu, 26 Sep 2013 12:31:17 +0000 (20:31 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Thu, 26 Sep 2013 12:31:17 +0000 (20:31 +0800)
script/lib/github.py

index ba6ea86c0f68400b982f5a161daa4d9327793903..ebb3e3f53aa3e1f5eaba61d7459e8c2f6fe2efa8 100644 (file)
@@ -1,27 +1,39 @@
 #!/usr/bin/env python
 
 import json
+import re
 import requests
 
 GITHUB_URL = 'https://api.github.com'
+GITHUB_UPLOAD_ASSET_URL = 'https://uploads.github.com'
 
 class GitHub:
   def __init__(self, access_token):
     self._authorization = 'token %s' % access_token
 
+    pattern = '^/repos/{0}/{0}/releases/{1}/assets$'.format('[^/]+', '[0-9]+')
+    self._releases_upload_api_pattern = re.compile(pattern)
+
   def __getattr__(self, attr):
     return _Callable(self, '/%s' % attr)
 
   def _http(self, method, path, **kw):
-    url = '%s%s' % (GITHUB_URL, path)
     if not 'headers' in kw:
       kw['headers'] = dict()
     headers = kw['headers']
     headers['Authorization'] = self._authorization
     headers['Accept'] = 'application/vnd.github.manifold-preview'
+
+    # Data are sent in JSON format.
     if 'data' in kw:
       kw['data'] = json.dumps(kw['data'])
 
+    # Switch to a different domain for the releases uploading API.
+    if self._releases_upload_api_pattern.match(path):
+      url = '%s%s' % (GITHUB_UPLOAD_ASSET_URL, path)
+    else:
+      url = '%s%s' % (GITHUB_URL, path)
+
     r = getattr(requests, method)(url, **kw).json()
     if 'message' in r:
       raise Exception(json.dumps(r, indent=2, separators=(',', ': ')))