11import { FirmwareOption } from '../constants'
22import { defaultFirmwareOptions } from '../constants'
33import { DeviceService } from './DeviceService' ;
4- import { Octokit } from '@octokit/rest' ;
4+ // const { Octokit } = require( '@octokit/rest') ;
55
66export class FirmwareService {
77 private firmwareString : string | null = null ;
@@ -67,11 +67,24 @@ export class FirmwareService {
6767 firmwareId = 'm-' + firmwareId . replace ( 'minimal-' , '' ) ;
6868 }
6969
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.
7179 //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.
7282 // Instead let's use the asset ID to formulate an GitHub API URL.
7383 // 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 } ` ;
7588
7689 // log the asset object
7790 console . log ( 'Asset:' , asset ) ;
@@ -178,6 +191,7 @@ export class FirmwareService {
178191
179192 console . log ( "Performing fetch for firmware:" , selectedFirmware . url ) ;
180193
194+ // method 1: browser_download_url from the asset object.
181195 // const result = await fetch(selectedFirmware.url, {
182196 // mode: 'cors',
183197 // headers: {
@@ -188,19 +202,38 @@ export class FirmwareService {
188202
189203 // console.log('Firmware fetch result:', result);
190204
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({});
192207
193208 // 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.
200233 }
201234 } ) ;
202235
203- console . log ( 'Response from GitHub :' , response ) ;
236+ console . log ( 'Result :' , result ) ;
204237 throw new Error ( 'Purposeful error out during testing.' ) ; // TODO: Remove this line when done testing.
205238
206239 // if (!result.ok) {
0 commit comments