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 ;
@@ -101,6 +110,8 @@ protected async System.Threading.Tasks.Task<ExitCodes> DownloadAndExtractAsync()
101110 Environment . GetFolderPath ( Environment . SpecialFolder . UserProfile ) ,
102111 ".nanoFramework" ,
103112 _targetName ) ;
113+
114+ Directory . CreateDirectory ( LocationPath ) ;
104115 }
105116 catch
106117 {
@@ -132,9 +143,12 @@ protected async System.Threading.Tasks.Task<ExitCodes> DownloadAndExtractAsync()
132143 Console . Write ( $ "Trying to find { _targetName } in { ( _stable ? "stable" : "developement" ) } repository...") ;
133144 }
134145
135- HttpResponseMessage response = await _bintrayClient . GetAsync ( requestUri ) ;
146+ HttpResponseMessage response = await _cloudsmithClient . GetAsync ( requestUri ) ;
147+
148+ var responseBody = await response . Content . ReadAsStringAsync ( ) ;
136149
137- if ( response . StatusCode == HttpStatusCode . NotFound )
150+ // check for empty array
151+ if ( responseBody == "[]" )
138152 {
139153 if ( Verbosity >= VerbosityLevel . Normal )
140154 {
@@ -143,12 +157,12 @@ protected async System.Threading.Tasks.Task<ExitCodes> DownloadAndExtractAsync()
143157 }
144158
145159 // try with community targets
146- requestUri = $ "{ _bintrayApiPackages } /{ _communityTargetsepo } /{ _targetName } ";
160+ requestUri = $ "{ _cloudsmithPackages } /{ _communityTargetsepo } /?page=1&query= { _targetName } latest ";
147161 repoName = _communityTargetsepo ;
148162
149- response = await _bintrayClient . GetAsync ( requestUri ) ;
163+ response = await _cloudsmithClient . GetAsync ( requestUri ) ;
150164
151- if ( response . StatusCode == HttpStatusCode . NotFound )
165+ if ( responseBody == "[]" )
152166 {
153167 if ( Verbosity >= VerbosityLevel . Normal )
154168 {
@@ -165,24 +179,26 @@ protected async System.Threading.Tasks.Task<ExitCodes> DownloadAndExtractAsync()
165179 Console . WriteLine ( $ "OK") ;
166180 }
167181
168- // read and parse response
169- string responseBody = await response . Content . ReadAsStringAsync ( ) ;
170- BintrayPackageInfo packageInfo = JsonConvert . DeserializeObject < BintrayPackageInfo > ( responseBody ) ;
182+ // parse response
183+ List < CloudsmithPackageInfo > packageInfo = JsonConvert . DeserializeObject < List < CloudsmithPackageInfo > > ( responseBody ) ;
171184
172185 // if no specific version was requested, use latest available
173186 if ( string . IsNullOrEmpty ( _fwVersion ) )
174187 {
175- _fwVersion = packageInfo . LatestVersion ;
188+ _fwVersion = packageInfo . ElementAt ( 0 ) . Version ;
176189 }
177190
191+ // grab download URL
192+ downloadUrl = packageInfo . ElementAt ( 0 ) . DownloadUrl ;
193+
178194 // set exposed property
179195 Version = _fwVersion ;
180196
181197 stepSuccesful = true ;
182198 }
183199 catch
184200 {
185- // 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
186202 }
187203 }
188204
@@ -220,9 +236,7 @@ protected async System.Threading.Tasks.Task<ExitCodes> DownloadAndExtractAsync()
220236 try
221237 {
222238 // setup and perform download request
223- requestUri = $ "https://dl.bintray.com/nfbot/{ repoName } /{ fwFileName } ";
224-
225- using ( var fwFileResponse = await _bintrayClient . GetAsync ( requestUri ) )
239+ using ( var fwFileResponse = await _cloudsmithClient . GetAsync ( downloadUrl ) )
226240 {
227241 if ( fwFileResponse . IsSuccessStatusCode )
228242 {
@@ -251,7 +265,7 @@ protected async System.Threading.Tasks.Task<ExitCodes> DownloadAndExtractAsync()
251265 }
252266 catch
253267 {
254- // 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
255269 }
256270 }
257271 else
0 commit comments