55
66using Newtonsoft . Json ;
77using System ;
8+ using System . Collections . Generic ;
89using System . IO ;
910using System . IO . Compression ;
1011using System . Linq ;
11- using System . Net ;
1212using System . Net . Http ;
1313using System . Text . RegularExpressions ;
14+ using System . Web ;
1415
1516namespace nanoFramework . Tools . FirmwareFlasher
1617{
1718 /// <summary>
18- /// Abstract base class that handles the download and extraction of firmware file from Bintray .
19+ /// Abstract base class that handles the download and extraction of firmware file from Cloudsmith .
1920 /// </summary>
2021 internal abstract class FirmwarePackage : IDisposable
2122 {
2223 // HttpClient is intended to be instantiated once per application, rather than per-use.
23- static HttpClient _bintrayClient = new HttpClient ( ) ;
24+ static HttpClient _cloudsmithClient = new HttpClient ( ) ;
2425
2526 /// <summary>
26- /// Uri of Bintray API
27+ /// Uri of Cloudsmith API
2728 /// </summary>
28- internal const string _bintrayApiPackages = "https://api.bintray.com/ packages/nfbot " ;
29+ internal const string _cloudsmithPackages = "https://api.cloudsmith.io/v1/ packages/net-nanoframework " ;
2930
3031 internal const string _refTargetsDevRepo = "nanoframework-images-dev" ;
3132 internal const string _refTargetsStableRepo = "nanoframework-images" ;
@@ -68,9 +69,17 @@ protected async System.Threading.Tasks.Task<ExitCodes> DownloadAndExtractAsync()
6869 {
6970 string fwFileName = null ;
7071
72+ // query URL
73+ // https://api.cloudsmith.io/v1/packages/net-nanoframework/REPO-NAME-HERE/?page=1&query=/PACKAGE-NAME-HERE latest
74+
75+ // download URL
76+ // https://dl.cloudsmith.io/public/net-nanoframework/REPO-NAME-HERE/raw/names/PACKAGE-NAME-HERE/versions/VERSION-HERE/ST_STM32F429I_DISCOVERY-1.6.2-preview.9.zip
77+
7178 // reference targets
7279 var repoName = _stable ? _refTargetsStableRepo : _refTargetsDevRepo ;
73- string requestUri = $ "{ _bintrayApiPackages } /{ repoName } /{ _targetName } ";
80+ string requestUri = $ "{ _cloudsmithPackages } /{ repoName } /?page=1&query={ _targetName } latest";
81+
82+ string downloadUrl = string . Empty ;
7483
7584 // flag to signal if the work-flow step was successful
7685 bool stepSuccesful = false ;
@@ -134,9 +143,12 @@ protected async System.Threading.Tasks.Task<ExitCodes> DownloadAndExtractAsync()
134143 Console . Write ( $ "Trying to find { _targetName } in { ( _stable ? "stable" : "developement" ) } repository...") ;
135144 }
136145
137- HttpResponseMessage response = await _bintrayClient . GetAsync ( requestUri ) ;
146+ HttpResponseMessage response = await _cloudsmithClient . GetAsync ( requestUri ) ;
138147
139- if ( response . StatusCode == HttpStatusCode . NotFound )
148+ var responseBody = await response . Content . ReadAsStringAsync ( ) ;
149+
150+ // check for empty array
151+ if ( responseBody == "[]" )
140152 {
141153 if ( Verbosity >= VerbosityLevel . Normal )
142154 {
@@ -145,12 +157,12 @@ protected async System.Threading.Tasks.Task<ExitCodes> DownloadAndExtractAsync()
145157 }
146158
147159 // try with community targets
148- requestUri = $ "{ _bintrayApiPackages } /{ _communityTargetsepo } /{ _targetName } ";
160+ requestUri = $ "{ _cloudsmithPackages } /{ _communityTargetsepo } /?page=1&query= { _targetName } latest ";
149161 repoName = _communityTargetsepo ;
150162
151- response = await _bintrayClient . GetAsync ( requestUri ) ;
163+ response = await _cloudsmithClient . GetAsync ( requestUri ) ;
152164
153- if ( response . StatusCode == HttpStatusCode . NotFound )
165+ if ( responseBody == "[]" )
154166 {
155167 if ( Verbosity >= VerbosityLevel . Normal )
156168 {
@@ -167,24 +179,26 @@ protected async System.Threading.Tasks.Task<ExitCodes> DownloadAndExtractAsync()
167179 Console . WriteLine ( $ "OK") ;
168180 }
169181
170- // read and parse response
171- string responseBody = await response . Content . ReadAsStringAsync ( ) ;
172- BintrayPackageInfo packageInfo = JsonConvert . DeserializeObject < BintrayPackageInfo > ( responseBody ) ;
182+ // parse response
183+ List < CloudsmithPackageInfo > packageInfo = JsonConvert . DeserializeObject < List < CloudsmithPackageInfo > > ( responseBody ) ;
173184
174185 // if no specific version was requested, use latest available
175186 if ( string . IsNullOrEmpty ( _fwVersion ) )
176187 {
177- _fwVersion = packageInfo . LatestVersion ;
188+ _fwVersion = packageInfo . ElementAt ( 0 ) . Version ;
178189 }
179190
191+ // grab download URL
192+ downloadUrl = packageInfo . ElementAt ( 0 ) . DownloadUrl ;
193+
180194 // set exposed property
181195 Version = _fwVersion ;
182196
183197 stepSuccesful = true ;
184198 }
185199 catch
186200 {
187- // exception with download, assuming it's something with network connection or Bintray API
201+ // exception with download, assuming it's something with network connection or Cloudsmith API
188202 }
189203 }
190204
@@ -222,9 +236,7 @@ protected async System.Threading.Tasks.Task<ExitCodes> DownloadAndExtractAsync()
222236 try
223237 {
224238 // setup and perform download request
225- requestUri = $ "https://dl.bintray.com/nfbot/{ repoName } /{ fwFileName } ";
226-
227- using ( var fwFileResponse = await _bintrayClient . GetAsync ( requestUri ) )
239+ using ( var fwFileResponse = await _cloudsmithClient . GetAsync ( downloadUrl ) )
228240 {
229241 if ( fwFileResponse . IsSuccessStatusCode )
230242 {
@@ -253,7 +265,7 @@ protected async System.Threading.Tasks.Task<ExitCodes> DownloadAndExtractAsync()
253265 }
254266 catch
255267 {
256- // exception with download, assuming it's something with network connection or Bintray API
268+ // exception with download, assuming it's something with network connection or Cloudsmith API
257269 }
258270 }
259271 else
0 commit comments