1
1
import { FirmwareOption } from '../constants'
2
2
import { defaultFirmwareOptions } from '../constants'
3
3
import { DeviceService } from './DeviceService' ;
4
- import { Octokit } from '@octokit/rest' ;
4
+ // const { Octokit } = require( '@octokit/rest') ;
5
5
6
6
export class FirmwareService {
7
7
private firmwareString : string | null = null ;
@@ -67,11 +67,24 @@ export class FirmwareService {
67
67
firmwareId = 'm-' + firmwareId . replace ( 'minimal-' , '' ) ;
68
68
}
69
69
70
-
70
+
71
+ // curl method to download based on ID:
72
+ // curl -L \
73
+ // -H "Accept: application/vnd.github+json" \
74
+ // -H "Authorization: Bearer <YOUR-TOKEN>" \
75
+ // -H "X-GitHub-Api-Version: 2022-11-28" \
76
+ // https://api.github.com/repos/OWNER/REPO/releases/assets/ASSET_ID
77
+
78
+ //method 1: use the browser_download_url from the asset object.
71
79
//const firmwareUrl = asset.browser_download_url;
80
+
81
+ //method 2: use the asset ID to formulate a GET request to the GitHub API with octokit.
72
82
// Instead let's use the asset ID to formulate an GitHub API URL.
73
83
// We'll still use the 'URL' var for now but it's actually a formatted GET req
74
- const firmwareUrl = 'GET /repos/sparkfun/micropython/releases/assets/' + asset . id ;
84
+ //const firmwareUrl = 'GET /repos/sparkfun/micropython/releases/assets/' + asset.id;
85
+
86
+ //method 3: use the asset ID to formulate a direct url to fetch the asset from with similar method as curl.
87
+ const firmwareUrl = `https://api.github.com/repos/sparkfun/micropython/releases/assets/${ asset . id } ` ;
75
88
76
89
// log the asset object
77
90
console . log ( 'Asset:' , asset ) ;
@@ -178,6 +191,7 @@ export class FirmwareService {
178
191
179
192
console . log ( "Performing fetch for firmware:" , selectedFirmware . url ) ;
180
193
194
+ // method 1: browser_download_url from the asset object.
181
195
// const result = await fetch(selectedFirmware.url, {
182
196
// mode: 'cors',
183
197
// headers: {
@@ -188,19 +202,38 @@ export class FirmwareService {
188
202
189
203
// console.log('Firmware fetch result:', result);
190
204
191
- const octokit = new Octokit ( { } ) ;
205
+ // method 2: use the asset ID to formulate a GET request to the GitHub API with octokit.
206
+ // const octokit = new Octokit({});
192
207
193
208
// firmware URLs from above: const firmwareUrl = 'GET /repos/sparkfun/micropython/releases/assets/' + asset.id;
194
- const response = await octokit . request ( selectedFirmware . url , {
195
- owner : 'sparkfun' ,
196
- repo : 'micropython' ,
197
- asset_id : selectedFirmware . url . split ( '/' ) . pop ( ) , // Extract the asset ID from the URL
198
- headers : {
199
- 'X-GitHub-Api-Version' : '2022-11-28'
209
+ // const response = await octokit.request(selectedFirmware.url, {
210
+ // owner: 'sparkfun',
211
+ // repo: 'micropython',
212
+ // asset_id: selectedFirmware.url.split('/').pop(), // Extract the asset ID from the URL
213
+ // headers: {
214
+ // 'X-GitHub-Api-Version': '2022-11-28'
215
+ // }
216
+ // });
217
+
218
+ // console.log('Response from GitHub:', response);
219
+ // throw new Error('Purposeful error out during testing.'); // TODO: Remove this line when done testing.
220
+
221
+ // method 3: use the asset ID to formulate a direct url to fetch the asset from with similar method as curl.
222
+ // we can look at the headers in the curl command to see what we need to add to our fetch request.
223
+ // const headers = new Headers({
224
+ // 'Accept': 'application/vnd.github+json',
225
+ // 'X-GitHub-Api-Version': '2022-11-28',
226
+ // });
227
+
228
+ const result = await fetch ( selectedFirmware . url , {
229
+ headers :{
230
+ 'Accept' : 'application/vnd.github+json' ,
231
+ 'X-GitHub-Api-Version' : '2022-11-28' ,
232
+ // 'Authorization': `Bearer ${import.meta.env.VITE_GITHUB_TOKEN}` // Use your GitHub token here.
200
233
}
201
234
} ) ;
202
235
203
- console . log ( 'Response from GitHub :' , response ) ;
236
+ console . log ( 'Result :' , result ) ;
204
237
throw new Error ( 'Purposeful error out during testing.' ) ; // TODO: Remove this line when done testing.
205
238
206
239
// if (!result.ok) {
0 commit comments