Skip to content

Commit

Permalink
Merge pull request #41 from ba-st/delete_improvements
Browse files Browse the repository at this point in the history
Add more flexible options for DELETE
gcotelli authored May 18, 2021
2 parents a81bb24 + adbaafd commit 5bff032
Showing 3 changed files with 72 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -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 [

Original file line number Diff line number Diff line change
@@ -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 [

44 changes: 33 additions & 11 deletions source/Superluminal-RESTfulAPI/RESTfulAPIClient.class.st
Original file line number Diff line number Diff line change
@@ -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 ]
]

0 comments on commit 5bff032

Please sign in to comment.