@@ -63,6 +63,19 @@ public async Task<bool> DeleteEntityAsync(string entitysetName, string keyExpres
6363 return true ;
6464 }
6565
66+ public async Task < ODataResponse < bool > > DeleteEntityWithResponseAsync ( string entitysetName , string keyExpression )
67+ {
68+ var response = await httpClient . DeleteAsync ( $ "{ entitysetName } ({ keyExpression } )") ;
69+ var headers = CopyResponseHeaders ( response ) ;
70+ var statusCode = response . StatusCode ;
71+ if ( statusCode == System . Net . HttpStatusCode . NotFound )
72+ {
73+ return new ODataResponse < bool > { Data = false , StatusCode = statusCode , ResponseHeaders = headers } ;
74+ }
75+ await ValidateResponseAsync ( response ) ;
76+ return new ODataResponse < bool > { Data = true , StatusCode = statusCode , ResponseHeaders = headers } ;
77+ }
78+
6679 public async Task < T > CreateEntityAsync < T > ( string entitysetName , ODataInputBase input )
6780 {
6881 string json = JsonSerializer . Serialize ( input , jsonOptions ) ;
@@ -82,6 +95,29 @@ public async Task<T> CreateEntityAsync<T>(string entitysetName, ODataInputBase i
8295 return odataResponse ! . Data ! ;
8396 }
8497
98+ public async Task < ODataResponse < T > > CreateEntityWithResponseAsync < T > ( string entitysetName , ODataInputBase input )
99+ {
100+ string json = JsonSerializer . Serialize ( input , jsonOptions ) ;
101+ var content = new StringContent ( json , Encoding . UTF8 , "application/json" ) ;
102+ var response = await httpClient . PostAsync ( $ "{ entitysetName } ", content ) ;
103+
104+ await ValidateResponseAsync ( response ) ;
105+
106+ var headers = CopyResponseHeaders ( response ) ;
107+ var statusCode = response . StatusCode ;
108+ var rawResponse = await response . Content . ReadAsStringAsync ( ) ;
109+ var odataResponse = ProcessQueryResponse < T > ( rawResponse ) ;
110+
111+ if ( odataResponse == null || odataResponse . Data == null )
112+ {
113+ throw new Exception ( "Create entity reported success but the response is null." ) ;
114+ }
115+
116+ odataResponse . StatusCode = statusCode ;
117+ odataResponse . ResponseHeaders = headers ;
118+ return odataResponse ;
119+ }
120+
85121 public async Task InvokeActionAsync ( string actionPath , object ? parameters = null , CancellationToken token = default )
86122 {
87123 string json = parameters != null ? JsonSerializer . Serialize ( parameters , jsonOptions ) : "{}" ;
@@ -96,17 +132,27 @@ public async Task InvokeActionAsync(string actionPath, object? parameters = null
96132 var content = new StringContent ( json , Encoding . UTF8 , "application/json" ) ;
97133 var response = await httpClient . PostAsync ( actionPath , content , token ) ;
98134 await ValidateResponseAsync ( response ) ;
135+ var headers = CopyResponseHeaders ( response ) ;
136+ var statusCode = response . StatusCode ;
99137 var rawResponse = await response . Content . ReadAsStringAsync ( token ) ;
100- return ProcessQueryResponse < T > ( rawResponse ) ;
138+ var result = ProcessQueryResponse < T > ( rawResponse ) ;
139+ result . StatusCode = statusCode ;
140+ result . ResponseHeaders = headers ;
141+ return result ;
101142 }
102143
103144 public async Task < ODataResponse < T > ? > InvokeFunctionAsync < T > ( string functionPath , CancellationToken token = default )
104145 {
105146 using var response = await httpClient . GetAsync ( functionPath , token ) ;
106147 if ( response . StatusCode == System . Net . HttpStatusCode . NotFound ) { return null ; }
107148 await ValidateResponseAsync ( response ) ;
149+ var headers = CopyResponseHeaders ( response ) ;
150+ var statusCode = response . StatusCode ;
108151 var rawResponse = await response . Content . ReadAsStringAsync ( token ) ;
109- return ProcessQueryResponse < T > ( rawResponse ) ;
152+ var result = ProcessQueryResponse < T > ( rawResponse ) ;
153+ result . StatusCode = statusCode ;
154+ result . ResponseHeaders = headers ;
155+ return result ;
110156 }
111157
112158 public async Task < bool > UpdateEntityAsync ( string entitysetName , string keyExpression , ODataInputBase input )
@@ -126,6 +172,27 @@ public async Task<bool> UpdateEntityAsync(string entitysetName, string keyExpres
126172 return true ;
127173 }
128174
175+ public async Task < ODataResponse < bool > > UpdateEntityWithResponseAsync ( string entitysetName , string keyExpression , ODataInputBase input )
176+ {
177+ string json = JsonSerializer . Serialize ( input , jsonOptions ) ;
178+ var request = new HttpRequestMessage ( HttpMethod . Patch , $ "{ entitysetName } ({ keyExpression } )")
179+ {
180+ Content = new StringContent ( json , Encoding . UTF8 , "application/json" )
181+ } ;
182+
183+ var response = await httpClient . SendAsync ( request ) ;
184+ var headers = CopyResponseHeaders ( response ) ;
185+ var statusCode = response . StatusCode ;
186+ if ( statusCode == System . Net . HttpStatusCode . NotFound )
187+ {
188+ return new ODataResponse < bool > { Data = false , StatusCode = statusCode , ResponseHeaders = headers } ;
189+ }
190+
191+ await ValidateResponseAsync ( response ) ;
192+
193+ return new ODataResponse < bool > { Data = true , StatusCode = statusCode , ResponseHeaders = headers } ;
194+ }
195+
129196
130197 public async Task < ODataResponse < List < T > > > QueryEntitySetAsync < T > ( string entitySetName , string ? select , string ? expand = null , string ? filter = null , bool ? count = null , int ? top = null , int ? skip = null , string ? orderby = null , CancellationToken token = default )
131198 {
@@ -138,8 +205,13 @@ public async Task<ODataResponse<List<T>>> QueryEntitySetAsync<T>(string entitySe
138205 await ValidateResponseAsync ( response ) ;
139206 }
140207
208+ var headers = CopyResponseHeaders ( response ) ;
209+ var statusCode = response . StatusCode ;
141210 var rawResponse = await response . Content . ReadAsStringAsync ( token ) ;
142- return ProcessQueryResponse < List < T > > ( rawResponse ) ;
211+ var result = ProcessQueryResponse < List < T > > ( rawResponse ) ;
212+ result . StatusCode = statusCode ;
213+ result . ResponseHeaders = headers ;
214+ return result ;
143215 }
144216
145217
@@ -159,8 +231,13 @@ public async Task<ODataResponse<List<T>>> QueryEntitySetAsync<T>(string entitySe
159231 await ValidateResponseAsync ( response ) ;
160232 }
161233
234+ var headers = CopyResponseHeaders ( response ) ;
235+ var statusCode = response . StatusCode ;
162236 var rawResponse = await response . Content . ReadAsStringAsync ( token ) ;
163- return ProcessQueryResponse < T > ( rawResponse ) ;
237+ var result = ProcessQueryResponse < T > ( rawResponse ) ;
238+ result . StatusCode = statusCode ;
239+ result . ResponseHeaders = headers ;
240+ return result ;
164241 }
165242
166243 private string GenerateUrl ( string entitySetName , string ? keyString = null , string ? select = null , string ? expand = null , string ? filter = null , bool ? count = null , int ? top = null , int ? skip = null , string ? orderby = null )
@@ -392,6 +469,16 @@ private async Task ValidateResponseAsync(HttpResponseMessage response)
392469
393470 throw new ODataRequestException ( $ "OData request failed. Status code:{ ( int ) response . StatusCode } . Error:{ odataError ? . Message } ", response . RequestMessage , odataError ) ;
394471 }
472+
473+ private static IDictionary < string , IEnumerable < string > > CopyResponseHeaders ( HttpResponseMessage response )
474+ {
475+ var headers = new Dictionary < string , IEnumerable < string > > ( StringComparer . OrdinalIgnoreCase ) ;
476+ foreach ( var header in response . Headers )
477+ headers [ header . Key ] = header . Value . ToList ( ) ;
478+ foreach ( var header in response . Content . Headers )
479+ headers [ header . Key ] = header . Value . ToList ( ) ;
480+ return headers ;
481+ }
395482 }
396483
397484}
0 commit comments