@@ -174,40 +174,35 @@ 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- var getServiceTask = ContentDeliveryServiceExtensions . GetServiceAsync ( this , serviceId , cancellationToken ) ;
183- getServiceTask . Wait ( ) ;
184- ServiceData originalServiceData = getServiceTask . Result ;
185-
186-
180+ return this . GetServiceAsync ( serviceId , cancellationToken )
181+ . Then ( task => PrepareUpdateServiceFromExistingAsync ( serviceId , updatedServiceData , task . Result , cancellationToken ) ) ;
182+ }
187183
184+ protected virtual Task < UpdateServiceApiCall > PrepareUpdateServiceFromExistingAsync ( ServiceId serviceId , ServiceData updatedServiceData , ServiceData originalServiceData , CancellationToken cancellationToken )
185+ {
188186 // Here's a serious HACK: Because the current ServiceData includes properties
189187 // that are not part of a new instance of ServiceData (such as "Id", "Status" and others)
190188 // we need to create a new instance using the data from the current ServiceData object.
191189 // That way, when we perform the "DIFF" operation to calculate the JSon Patch object,
192190 // the extra properties won't interfere.
193191 ServiceData tempServiceData = new ServiceData ( originalServiceData . Name , originalServiceData . FlavorId , originalServiceData . Domains , originalServiceData . Origins , originalServiceData . CachingRules , originalServiceData . Restrictions ) ;
194192
195-
196-
197193 // Calculate the Json Patch document
198194 var beforeUpdate = JToken . Parse ( JsonConvert . SerializeObject ( tempServiceData ) ) ;
199195 var afterUpdate = JToken . Parse ( JsonConvert . SerializeObject ( updatedServiceData ) ) ;
200196 var patchDoc = new JsonDiffer ( ) . Diff ( beforeUpdate , afterUpdate ) ;
201197
202-
203198 // Another Hack: For some reason (TODO: which needs to be investigated and fixed),
204199 // we need to deserialize the document in order for it to render a proper Json object.
205200 var jsonPatchDocument = JsonConvert . DeserializeObject ( patchDoc . ToString ( ) ) ;
206201
207-
208202 // Now we can call the PATCH method to update the ServiceData on the server.
209203 UriTemplate template = new UriTemplate ( "services/{service_id}" ) ;
210204 Dictionary < string , string > parameters = new Dictionary < string , string > { { "service_id" , serviceId . Value } } ;
205+
211206 return GetBaseUriAsync ( cancellationToken )
212207 . Then ( PrepareRequestAsyncFunc ( new HttpMethod ( "PATCH" ) , template , parameters , jsonPatchDocument , cancellationToken ) )
213208 . Select (
0 commit comments