diff --git a/source/Superluminal-RESTfulAPI-Tests/RESTfulAPIClientIntegrationTest.class.st b/source/Superluminal-RESTfulAPI-Tests/RESTfulAPIClientIntegrationTest.class.st index 5ecbb24..4f00e0f 100644 --- a/source/Superluminal-RESTfulAPI-Tests/RESTfulAPIClientIntegrationTest.class.st +++ b/source/Superluminal-RESTfulAPI-Tests/RESTfulAPIClientIntegrationTest.class.st @@ -79,6 +79,14 @@ RESTfulAPIClientIntegrationTest >> testDeleteNotFound [ raise: HTTPClientError notFound ] +{ #category : #tests } +RESTfulAPIClientIntegrationTest >> testDeletePreconditionFailed [ + + self + should: [ apiClient deleteAt: self httpbinStatusLocation / '412' ] + raise: HTTPClientError preconditionFailed +] + { #category : #tests } RESTfulAPIClientIntegrationTest >> testGetAcceptingWithSuccessfulResponseDo [ diff --git a/source/Superluminal-RESTfulAPI-Tests/RESTfulAPIClientTest.class.st b/source/Superluminal-RESTfulAPI-Tests/RESTfulAPIClientTest.class.st index fb28d91..fbf53b6 100644 --- a/source/Superluminal-RESTfulAPI-Tests/RESTfulAPIClientTest.class.st +++ b/source/Superluminal-RESTfulAPI-Tests/RESTfulAPIClientTest.class.st @@ -42,7 +42,7 @@ RESTfulAPIClientTest >> notFoundResponse [ RESTfulAPIClientTest >> setUp [ super setUp. - apiClient := self apiClient. "( ExpiringCache onDistributedMemoryAt: {'127.0.0.1:11211'} )" + apiClient := self apiClient. resourceIdentifier := UUID new asString ] @@ -68,6 +68,36 @@ RESTfulAPIClientTest >> testDeleteAcceptingWithSuccessfulResponseDo [ self assert: wasSuccessfull ] +{ #category : #tests } +RESTfulAPIClientTest >> testDeleteAtSuccess [ + + self configureHttpClientToRespondWith: ZnResponse noContent. + self shouldnt: [ apiClient deleteAt: self location ] raise: HTTPClientError +] + +{ #category : #tests } +RESTfulAPIClientTest >> testDeleteAtSuccessWhenCached [ + + | etagIsSet | + + etagIsSet := false. + + self + configureHttpClientToRespondWith: + ( ( self jsonOkResponseWith: #(1 2 3) ) + addCachingDirective: 'Max-Age=60'; + setEntityTag: '"1"' asETag; + yourself ). + + self httpClient whenSend: #setIfMatchTo: evaluate: [ :etag | etagIsSet := etag = '"1"' ]. + + apiClient get: self location withSuccessfulResponseDo: [ :contents | ]. + + self configureHttpClientToRespondWith: ZnResponse noContent. + self shouldnt: [ apiClient deleteAt: self location ] raise: HTTPClientError. + self assert: etagIsSet +] + { #category : #tests } RESTfulAPIClientTest >> testDeleteNotFound [ diff --git a/source/Superluminal-RESTfulAPI/RESTfulAPIClient.class.st b/source/Superluminal-RESTfulAPI/RESTfulAPIClient.class.st index 5c3386b..17a2629 100644 --- a/source/Superluminal-RESTfulAPI/RESTfulAPIClient.class.st +++ b/source/Superluminal-RESTfulAPI/RESTfulAPIClient.class.st @@ -61,28 +61,50 @@ RESTfulAPIClient >> clientPoolFor: aLocation [ ] ] +{ #category : #invoking } +RESTfulAPIClient >> deleteAt: aLocation [ + + ^ self + deleteAt: aLocation + configuredBy: [ :request | + self + withCachedETagAt: aLocation + do: [ :entityTag | request headers setIfMatchTo: entityTag asString ] + ] + withSuccessfulResponseDo: [ ] +] + { #category : #invoking } RESTfulAPIClient >> deleteAt: aLocation accepting: aMediaType withSuccessfulResponseDo: aBlock [ + ^ self + deleteAt: aLocation + configuredBy: [ :request | + | command | + + command := request headers setAcceptTo: aMediaType. + self + withCachedETagAt: aLocation + do: [ :entityTag | command := command + ( request headers setIfMatchTo: entityTag asString ) ]. + command + ] + withSuccessfulResponseDo: aBlock +] + +{ #category : #invoking } +RESTfulAPIClient >> deleteAt: aLocation configuredBy: aRequestBuildingBlock withSuccessfulResponseDo: aBlock [ + ^ self handleExceptionsDuring: [ | httpRequest response | - httpRequest := HttpRequest - delete: aLocation - configuredUsing: [ :request | - | command | - command := request headers setAcceptTo: aMediaType. - self - withCachedETagAt: aLocation - do: [ :entityTag | command := request headers setIfMatchTo: entityTag asString ]. - command - ]. + + httpRequest := HttpRequest delete: aLocation configuredUsing: aRequestBuildingBlock. self withHttpClientFor: aLocation do: [ :httpClient | response := httpRequest applyOn: httpClient ]. ( response isSuccess or: [ response isNoContent ] ) ifTrue: [ expiringCache clearResourceAt: aLocation. - aBlock value: ( self tryToCacheContentsOf: response basedOn: aLocation ) + aBlock cull: ( self tryToCacheContentsOf: response basedOn: aLocation ) ] ifFalse: [ self signalCannotCompleteDeleteErrorBasedOn: response ] ]