11using System ;
2- using System . Collections . Generic ;
32using System . Collections . Specialized ;
43using System . IO ;
54using System . Linq ;
65using System . Net ;
76using System . Net . Http ;
8- using System . Net . Http . Headers ;
97using System . Threading . Tasks ;
108using Tomlyn ;
119using Tomlyn . Syntax ;
@@ -14,35 +12,41 @@ namespace Deislabs.Bindle;
1412
1513public class BindleClient
1614{
17- public BindleClient (
18- string baseUri
19- )
20- {
21- _baseUri = new Uri ( SlashSafe ( baseUri ) ) ;
22- _httpClient = new HttpClient ( ) ;
23- }
15+ public BindleClient ( string connectionString ) : this ( new ConnectionInfo ( connectionString ) ) { }
2416
25- public BindleClient (
26- string baseUri ,
27- HttpMessageHandler messageHandler
28- )
17+ public BindleClient ( ConnectionInfo connectionInfo )
2918 {
30- _baseUri = new Uri ( SlashSafe ( baseUri ) ) ;
31- _httpClient = new HttpClient ( messageHandler ) ;
19+ if ( string . IsNullOrEmpty ( connectionInfo . BaseUri ) )
20+ throw new ArgumentException ( "base URI cannot be empty" ) ;
21+
22+ var handler = new HttpClientHandler ( ) ;
23+
24+ if ( connectionInfo . SslMode == SslMode . Disable )
25+ {
26+ handler . ServerCertificateCustomValidationCallback =
27+ ( httpRequestMessage , cert , cetChain , policyErrors ) =>
28+ {
29+ return true ;
30+ } ;
31+ }
32+
33+ _httpClient = new HttpClient ( handler ) { BaseAddress = new Uri ( SlashSafe ( connectionInfo . BaseUri ) ) } ;
34+
35+ if ( ! string . IsNullOrEmpty ( connectionInfo . UserName ) && ! string . IsNullOrEmpty ( connectionInfo . Password ) )
36+ {
37+ _httpClient . DefaultRequestHeaders . Authorization = new System . Net . Http . Headers . AuthenticationHeaderValue ( "Basic" , $ "{ connectionInfo . UserName } :{ connectionInfo . Password } ") ;
38+ }
3239 }
3340
3441 private const string INVOICE_PATH = "_i" ;
3542 private const string QUERY_PATH = "_q" ;
3643 private const string RELATIONSHIP_PATH = "_r" ;
37-
38- private readonly Uri _baseUri ;
3944 private readonly HttpClient _httpClient ;
4045
4146 public async Task < Invoice > GetInvoice ( string invoiceId , GetInvoiceOptions options = GetInvoiceOptions . None )
4247 {
4348 var query = GetInvoiceQueryString ( options ) ;
44- var uri = new Uri ( _baseUri , $ "{ INVOICE_PATH } /{ invoiceId } { query } ") ;
45- var response = await _httpClient . GetAsync ( uri ) ;
49+ var response = await _httpClient . GetAsync ( $ "{ INVOICE_PATH } /{ invoiceId } { query } ") ;
4650 await ExpectResponseCode ( response , HttpStatusCode . OK , HttpStatusCode . Forbidden ) ;
4751
4852 if ( response . StatusCode == HttpStatusCode . Forbidden )
@@ -74,8 +78,7 @@ public async Task<Matches> QueryInvoices(string? queryString = null,
7478 bool ? yanked = null )
7579 {
7680 var query = GetDistinctInvoicesNamesQueryString ( queryString , offset , limit , strict , semVer , yanked ) ;
77- var uri = new Uri ( _baseUri , $ "{ QUERY_PATH } ?{ query } ") ;
78- var response = await _httpClient . GetAsync ( uri ) ;
81+ var response = await _httpClient . GetAsync ( $ "{ QUERY_PATH } ?{ query } ") ;
7982 await ExpectResponseCode ( response , HttpStatusCode . OK , HttpStatusCode . Forbidden ) ;
8083
8184 if ( response . StatusCode == HttpStatusCode . Forbidden )
@@ -99,9 +102,8 @@ public async Task<CreateInvoiceResult> CreateInvoice(Invoice invoice)
99102 ConvertPropertyName = name => TomlNamingHelper . PascalToCamelCase ( name )
100103 } ) ;
101104
102- var uri = new Uri ( _baseUri , INVOICE_PATH ) ;
103105 var requestContent = new StringContent ( invoiceToml , null , "application/toml" ) ;
104- var response = await _httpClient . PostAsync ( uri , requestContent ) ;
106+ var response = await _httpClient . PostAsync ( INVOICE_PATH , requestContent ) ;
105107 await ExpectResponseCode ( response , HttpStatusCode . Created , HttpStatusCode . Accepted ) ;
106108
107109 var content = await response . Content . ReadAsStringAsync ( ) ;
@@ -115,15 +117,13 @@ public async Task<CreateInvoiceResult> CreateInvoice(Invoice invoice)
115117
116118 public async Task YankInvoice ( string invoiceId )
117119 {
118- var uri = new Uri ( _baseUri , $ "{ INVOICE_PATH } /{ invoiceId } ") ;
119- var response = await _httpClient . DeleteAsync ( uri ) ;
120+ var response = await _httpClient . DeleteAsync ( $ "{ INVOICE_PATH } /{ invoiceId } ") ;
120121 await ExpectResponseCode ( response , HttpStatusCode . OK ) ;
121122 }
122123
123124 public async Task < HttpContent > GetParcel ( string invoiceId , string parcelId )
124125 {
125- var uri = new Uri ( _baseUri , $ "{ INVOICE_PATH } /{ invoiceId } @{ parcelId } ") ;
126- var response = await _httpClient . GetAsync ( uri ) ;
126+ var response = await _httpClient . GetAsync ( $ "{ INVOICE_PATH } /{ invoiceId } @{ parcelId } ") ;
127127 await ExpectResponseCode ( response , HttpStatusCode . OK ) ;
128128
129129 return response . Content ;
@@ -146,15 +146,13 @@ public async Task CreateParcel(string invoiceId, string parcelId, byte[] content
146146
147147 public async Task CreateParcel ( string invoiceId , string parcelId , HttpContent content )
148148 {
149- var uri = new Uri ( _baseUri , $ "{ INVOICE_PATH } /{ invoiceId } @{ parcelId } ") ;
150- var response = await _httpClient . PostAsync ( uri , content ) ;
149+ var response = await _httpClient . PostAsync ( $ "{ INVOICE_PATH } /{ invoiceId } @{ parcelId } ", content ) ;
151150 await ExpectResponseCode ( response , HttpStatusCode . OK , HttpStatusCode . Created ) ;
152151 }
153152
154153 public async Task < MissingParcelsResponse > ListMissingParcels ( string invoiceId )
155154 {
156- var uri = new Uri ( _baseUri , $ "{ RELATIONSHIP_PATH } /missing/{ invoiceId } ") ;
157- var response = await _httpClient . GetAsync ( uri ) ;
155+ var response = await _httpClient . GetAsync ( $ "{ RELATIONSHIP_PATH } /missing/{ invoiceId } ") ;
158156 await ExpectResponseCode ( response , HttpStatusCode . OK ) ;
159157
160158 var content = await response . Content . ReadAsStringAsync ( ) ;
0 commit comments