|
1 | | -// Github.js 0.6.1 |
| 1 | +// Github.js 0.6.2 |
2 | 2 | // (c) 2012 Michael Aufreiter, Development Seed |
3 | 3 | // Github.js is freely distributable under the MIT license. |
4 | 4 | // For all details and documentation: |
|
13 | 13 | // Util |
14 | 14 | // ======= |
15 | 15 |
|
16 | | - function headers() { |
17 | | - var headers = {} |
18 | | - if (options.auth === 'oauth' && !options.token) return { Accept: 'application/vnd.github.raw' }; |
19 | | - if (options.auth === 'basic' && (!options.username || !options.password)) return { Accept: 'application/vnd.github.raw' }; |
20 | | - return options.auth == 'oauth' |
21 | | - ? { Authorization: 'token '+ options.token, Accept: 'application/vnd.github.raw' } |
22 | | - : { Authorization : 'Basic ' + Base64.encode(options.username + ':' + options.password), Accept: 'application/vnd.github.raw' } |
23 | | - } |
24 | | - |
25 | | - function _request(method, path, data, cb) { |
26 | | - $.ajax({ |
27 | | - type: method, |
28 | | - url: API_URL + path, |
29 | | - data: JSON.stringify(data), |
30 | | - dataType: 'json', |
31 | | - contentType: 'application/x-www-form-urlencoded', |
32 | | - success: function(res) { cb(null, res); }, |
33 | | - error: function(err) { cb(err); }, |
34 | | - headers : headers() |
35 | | - }); |
36 | | - } |
37 | | - |
38 | | - function _raw_request(method, path, data, cb) { |
39 | | - $.ajax({ |
40 | | - type: method, |
41 | | - url: API_URL + path, |
42 | | - data: JSON.stringify(data), |
43 | | - contentType: 'application/x-www-form-urlencoded', |
44 | | - success: function(res) { cb(null, res); }, |
45 | | - error: function(err) { cb(err); }, |
46 | | - headers : headers() |
47 | | - }); |
| 16 | + function _request(method, path, data, cb, raw) { |
| 17 | + var xhr = new XMLHttpRequest(); |
| 18 | + if (!raw) {xhr.dataType = "json"} |
| 19 | + xhr.open(method, API_URL + path); |
| 20 | + xhr.onreadystatechange = function () { |
| 21 | + if (this.readyState == 4) { |
| 22 | + this.status == 200 ? cb(null, JSON.parse(this.responseText)) : cb(this.status); |
| 23 | + } |
| 24 | + } |
| 25 | + xhr.setRequestHeader('Accept','application/vnd.github.raw'); |
| 26 | + if ( |
| 27 | + (options.auth == 'oauth' && options.token) || |
| 28 | + (options.auth == 'basic' && options.username && options.password) |
| 29 | + ) { |
| 30 | + xhr.setRequestHeader('Authorization',options.auth == 'oauth' |
| 31 | + ? 'token '+ options.token |
| 32 | + : 'Basic ' + Base64.encode(options.username + ':' + options.password) |
| 33 | + ); |
| 34 | + } |
| 35 | + data ? xhr.send(JSON.stringify(data)) : xhr.send(); |
48 | 36 | } |
49 | 37 |
|
50 | 38 | // User API |
|
156 | 144 | // ------- |
157 | 145 |
|
158 | 146 | this.getBlob = function(sha, cb) { |
159 | | - _raw_request("GET", repoPath + "/git/blobs/" + sha, null, cb); |
| 147 | + _request("GET", repoPath + "/git/blobs/" + sha, null, cb, 'raw'); |
160 | 148 | }; |
161 | 149 |
|
162 | 150 | // For a given file path, get the corresponding sha (blob for files, tree for dirs) |
|
0 commit comments