@@ -57,7 +57,7 @@ public virtual Task<GetHomeApiCall> PrepareGetHomeAsync(CancellationToken cancel
5757 UriTemplate template = new UriTemplate ( string . Empty ) ;
5858 Dictionary < string , string > parameters = new Dictionary < string , string > ( ) ;
5959 return GetBaseUriAsync ( cancellationToken )
60- . Then ( PrepareRequestAsyncFunc ( HttpMethod . Get , template , parameters , cancellationToken , "application/json" ) )
60+ . Then ( PrepareRequestAsyncFunc ( HttpMethod . Get , template , parameters , cancellationToken ) )
6161 . Select ( task => new GetHomeApiCall ( CreateJsonApiCall < HomeDocument > ( task . Result ) ) ) ;
6262 }
6363
@@ -79,7 +79,7 @@ public virtual Task<PingApiCall> PreparePingAsync(CancellationToken cancellation
7979 UriTemplate template = new UriTemplate ( "ping" ) ;
8080 Dictionary < string , string > parameters = new Dictionary < string , string > ( ) ;
8181 return GetBaseUriAsync ( cancellationToken )
82- . Then ( PrepareRequestAsyncFunc ( HttpMethod . Get , template , parameters , cancellationToken , "application/json" ) )
82+ . Then ( PrepareRequestAsyncFunc ( HttpMethod . Get , template , parameters , cancellationToken ) )
8383 //.Select(RemoveAcceptHeader) **DLS**
8484 . Select ( task => new PingApiCall ( CreateBasicApiCall ( task . Result ) ) ) ;
8585 }
@@ -174,44 +174,44 @@ public virtual Task<UpdateServiceApiCall> PrepareUpdateServiceAsync(ServiceId se
174174 if ( serviceId == null )
175175 throw new ArgumentNullException ( "serviceId" ) ;
176176
177-
178-
179177 // At this point, the serviceData parameter contains the NEW (AFTER) version
180178 // of the service data. Now, we need to retrieve the current (BEFORE) version,
181179 // then use those to calculate the difference, i.e. the JSON Patch document.
182- CancellationToken cn = new CancellationToken ( ) ;
183- var getServiceTask = ContentDeliveryServiceExtensions . GetServiceAsync ( this , serviceId , cn ) ;
184- getServiceTask . Wait ( ) ;
185- ServiceData originalServiceData = getServiceTask . Result ;
186-
187-
180+ return this . GetServiceAsync ( serviceId , cancellationToken )
181+ . Then ( task => PrepareUpdateServiceFromExistingAsync ( serviceId , updatedServiceData , task . Result , cancellationToken ) ) ;
182+ }
188183
184+ protected virtual Task < UpdateServiceApiCall > PrepareUpdateServiceFromExistingAsync ( ServiceId serviceId , ServiceData updatedServiceData , ServiceData originalServiceData , CancellationToken cancellationToken )
185+ {
189186 // Here's a serious HACK: Because the current ServiceData includes properties
190187 // that are not part of a new instance of ServiceData (such as "Id", "Status" and others)
191188 // we need to create a new instance using the data from the current ServiceData object.
192189 // That way, when we perform the "DIFF" operation to calculate the JSon Patch object,
193190 // the extra properties won't interfere.
194191 ServiceData tempServiceData = new ServiceData ( originalServiceData . Name , originalServiceData . FlavorId , originalServiceData . Domains , originalServiceData . Origins , originalServiceData . CachingRules , originalServiceData . Restrictions ) ;
195192
196-
197-
198193 // Calculate the Json Patch document
199194 var beforeUpdate = JToken . Parse ( JsonConvert . SerializeObject ( tempServiceData ) ) ;
200195 var afterUpdate = JToken . Parse ( JsonConvert . SerializeObject ( updatedServiceData ) ) ;
201196 var patchDoc = new JsonDiffer ( ) . Diff ( beforeUpdate , afterUpdate ) ;
202197
203-
204198 // Another Hack: For some reason (TODO: which needs to be investigated and fixed),
205199 // we need to deserialize the document in order for it to render a proper Json object.
206200 var jsonPatchDocument = JsonConvert . DeserializeObject ( patchDoc . ToString ( ) ) ;
207201
208-
209202 // Now we can call the PATCH method to update the ServiceData on the server.
210203 UriTemplate template = new UriTemplate ( "services/{service_id}" ) ;
211204 Dictionary < string , string > parameters = new Dictionary < string , string > { { "service_id" , serviceId . Value } } ;
205+
212206 return GetBaseUriAsync ( cancellationToken )
213- . Then ( PrepareRequestAsyncFunc ( new HttpMethod ( "PATCH" ) , "application/json-patch+json" , template , parameters , jsonPatchDocument , cancellationToken ) )
214- . Select ( task => new UpdateServiceApiCall ( CreateBasicApiCall ( task . Result ) ) ) ;
207+ . Then ( PrepareRequestAsyncFunc ( new HttpMethod ( "PATCH" ) , template , parameters , jsonPatchDocument , cancellationToken ) )
208+ . Select (
209+ task =>
210+ {
211+ HttpRequestMessage requestMessage = task . Result ;
212+ requestMessage . Content . Headers . ContentType = new MediaTypeHeaderValue ( "application/json-patch+json" ) ;
213+ return new UpdateServiceApiCall ( CreateBasicApiCall ( requestMessage ) ) ;
214+ } ) ;
215215 }
216216
217217 /// <inheritdoc/>
0 commit comments