@@ -40,14 +40,20 @@ public async Task<Invoice> GetInvoice(string invoiceId, GetInvoiceOptions option
4040 var query = GetInvoiceQueryString ( options ) ;
4141 var uri = new Uri ( _baseUri , $ "{ INVOICE_PATH } /{ invoiceId } { query } ") ;
4242 var response = await _httpClient . GetAsync ( uri ) ;
43- if ( response == null )
44- {
45- throw new Exception ( "No response from Bindle server" ) ;
46- }
47- if ( response . StatusCode != HttpStatusCode . OK )
43+ ExpectResponseCode ( response , HttpStatusCode . OK , HttpStatusCode . Forbidden ) ;
44+
45+ if ( response . StatusCode == HttpStatusCode . Forbidden )
4846 {
49- throw new System . Net . WebException ( $ "Bindle server returned status code { response . StatusCode } ") ;
47+ if ( ( options & GetInvoiceOptions . IncludeYanked ) == 0 )
48+ {
49+ throw new BindleYankedException ( ) ;
50+ }
51+ else
52+ {
53+ throw new BindleProtocolException ( $ "Bindle server returned status code { response . StatusCode } ", response ) ;
54+ }
5055 }
56+
5157 var toml = await ReadResponseToml ( response ) ;
5258 return Parser . ParseInvoice ( toml ) ;
5359 }
@@ -56,49 +62,32 @@ public async Task<CreateInvoiceResult> CreateInvoice(Invoice invoice)
5662 {
5763 var invoiceToml = InvoiceWriter . Write ( invoice ) ;
5864
59- if ( invoiceToml == null )
60- {
61- throw new Exception ( "Error serialising invoice to TOML" ) ;
62- }
63-
6465 var uri = new Uri ( _baseUri , INVOICE_PATH ) ;
6566 var requestContent = new StringContent ( invoiceToml , null , "application/toml" ) ;
6667 if ( requestContent . Headers . ContentType != null )
6768 {
6869 requestContent . Headers . ContentType . CharSet = null ; // The Bindle server is VERY strict about the contents of the Content-Type header
6970 }
7071 var response = await _httpClient . PostAsync ( uri , requestContent ) ;
72+ ExpectResponseCode ( response , HttpStatusCode . Created , HttpStatusCode . Accepted ) ;
7173
72- if ( response == null )
73- {
74- throw new Exception ( "No response from Bindle server" ) ;
75- }
76- if ( response . StatusCode != HttpStatusCode . Accepted && response . StatusCode != HttpStatusCode . Created )
77- {
78- throw new System . Net . WebException ( $ "Bindle server returned status code { response . StatusCode } ") ;
79- }
8074 var toml = await ReadResponseToml ( response ) ;
8175 return Parser . ParseCreateInvoiceResult ( toml ) ;
8276 }
8377
8478 public async Task YankInvoice ( string invoiceId )
8579 {
8680 var uri = new Uri ( _baseUri , $ "{ INVOICE_PATH } /{ invoiceId } ") ;
87- await _httpClient . DeleteAsync ( uri ) ;
81+ var response = await _httpClient . DeleteAsync ( uri ) ;
82+ ExpectResponseCode ( response , HttpStatusCode . OK ) ;
8883 }
8984
9085 public async Task < HttpContent > GetParcel ( string invoiceId , string parcelId )
9186 {
9287 var uri = new Uri ( _baseUri , $ "{ INVOICE_PATH } /{ invoiceId } @{ parcelId } ") ;
9388 var response = await _httpClient . GetAsync ( uri ) ;
94- if ( response == null )
95- {
96- throw new Exception ( "No response from Bindle server" ) ;
97- }
98- if ( response . StatusCode != HttpStatusCode . OK )
99- {
100- throw new System . Net . WebException ( $ "Bindle server returned status code { response . StatusCode } ") ;
101- }
89+ ExpectResponseCode ( response , HttpStatusCode . OK ) ;
90+
10291 return response . Content ;
10392 }
10493
@@ -120,21 +109,16 @@ public async Task CreateParcel(string invoiceId, string parcelId, byte[] content
120109 public async Task CreateParcel ( string invoiceId , string parcelId , HttpContent content )
121110 {
122111 var uri = new Uri ( _baseUri , $ "{ INVOICE_PATH } /{ invoiceId } @{ parcelId } ") ;
123- await _httpClient . PostAsync ( uri , content ) ;
112+ var response = await _httpClient . PostAsync ( uri , content ) ;
113+ ExpectResponseCode ( response , HttpStatusCode . OK , HttpStatusCode . Created ) ;
124114 }
125115
126116 public async Task < IEnumerable < Label > > ListMissingParcels ( string invoiceId )
127117 {
128118 var uri = new Uri ( _baseUri , $ "{ RELATIONSHIP_PATH } /missing/{ invoiceId } ") ;
129119 var response = await _httpClient . GetAsync ( uri ) ;
130- if ( response == null )
131- {
132- throw new Exception ( "No response from Bindle server" ) ;
133- }
134- if ( response . StatusCode != HttpStatusCode . OK )
135- {
136- throw new System . Net . WebException ( $ "Bindle server returned status code { response . StatusCode } ") ;
137- }
120+ ExpectResponseCode ( response , HttpStatusCode . OK ) ;
121+
138122 var toml = await ReadResponseToml ( response ) ;
139123 return Parser . ParseMissingLabels ( toml ) ;
140124 }
@@ -148,18 +132,30 @@ private static string SlashSafe(string uri)
148132 return uri + '/' ;
149133 }
150134
135+ private static void ExpectResponseCode ( HttpResponseMessage response , params HttpStatusCode [ ] codes )
136+ {
137+ if ( response == null )
138+ {
139+ throw new NoResponseException ( ) ;
140+ }
141+ if ( ! codes . Contains ( response . StatusCode ) )
142+ {
143+ throw new BindleProtocolException ( $ "Bindle server returned status code { response . StatusCode } ", response ) ;
144+ }
145+ }
146+
151147 private async static Task < TomlTable > ReadResponseToml ( HttpResponseMessage response )
152148 {
153149 var responseText = await response . Content . ReadAsStringAsync ( ) ;
154150 var responseToml = Tomlyn . Toml . Parse ( responseText ) ;
155151 if ( responseToml == null )
156152 {
157- throw new Exception ( "Empty response from Bindle server" ) ;
153+ throw new ResponseContentException ( "Empty response from Bindle server" ) ;
158154 }
159155 if ( responseToml . HasErrors )
160156 {
161157 var errors = String . Join ( ", " , responseToml . Diagnostics . Select ( d => d . Message ) ) ;
162- throw new Exception ( $ "Invalid response from Bindle server: { errors } ") ;
158+ throw new ResponseContentException ( $ "Invalid response from Bindle server: { errors } ") ;
163159 }
164160 return Tomlyn . Toml . ToModel ( responseToml ) ;
165161 }
0 commit comments