diff --git a/source/Superluminal-Service-Discovery-Examples-Tests/SuperluminalServiceDiscoveryExampleLauncherTest.class.st b/source/Superluminal-Service-Discovery-Examples-Tests/SuperluminalServiceDiscoveryExampleLauncherTest.class.st index c4ece97..45a093c 100644 --- a/source/Superluminal-Service-Discovery-Examples-Tests/SuperluminalServiceDiscoveryExampleLauncherTest.class.st +++ b/source/Superluminal-Service-Discovery-Examples-Tests/SuperluminalServiceDiscoveryExampleLauncherTest.class.st @@ -17,7 +17,7 @@ SuperluminalServiceDiscoveryExampleLauncherTest >> testActivate [ '--consul-agent-location=http://consul:8500' '--message=Hello' '--retry-delay-in-ms=1' ). - self should: [ LaunchpadCommandLineHandler activateWith: commandLine ] raise: NameLookupFailure + self should: [ LaunchpadCommandLineHandler activateWith: commandLine ] raise: HTTPError ] { #category : #tests } diff --git a/source/Superluminal-Service-Discovery/ConsulAgentHttpAPIBasedDiscoveryClient.class.st b/source/Superluminal-Service-Discovery/ConsulAgentHttpAPIBasedDiscoveryClient.class.st index 1c834aa..fba224e 100644 --- a/source/Superluminal-Service-Discovery/ConsulAgentHttpAPIBasedDiscoveryClient.class.st +++ b/source/Superluminal-Service-Discovery/ConsulAgentHttpAPIBasedDiscoveryClient.class.st @@ -8,9 +8,9 @@ Class { #name : #ConsulAgentHttpAPIBasedDiscoveryClient, #superclass : #ServiceDiscoveryClient, #instVars : [ - 'httpClient', 'agentLocation', - 'fallbackClient' + 'fallbackClient', + 'apiClient' ], #category : #'Superluminal-Service-Discovery' } @@ -27,44 +27,44 @@ ConsulAgentHttpAPIBasedDiscoveryClient class >> queryingAgentOn: aConsulAgentLoc ^ self new initializeQueryingAgentOn: aConsulAgentLocation chainedWith: aServiceDiscoveryClient ] -{ #category : #initialization } -ConsulAgentHttpAPIBasedDiscoveryClient >> initializeQueryingAgentOn: aConsulAgentLocation chainedWith: aServiceDiscoveryClient [ - - agentLocation := aConsulAgentLocation. - fallbackClient := aServiceDiscoveryClient. - httpClient := ZnClient new. - httpClient enforceHttpSuccess: true -] - { #category : #private } -ConsulAgentHttpAPIBasedDiscoveryClient >> requestToDiscover: serviceName [ +ConsulAgentHttpAPIBasedDiscoveryClient >> discoverLocationOf: serviceName ifFound: foundBlock otherwise: failBlock [ - ^ HttpRequest get: agentLocation / 'v1' / 'health' / 'service' / serviceName - configuredUsing: [ :request | + | serviceHealthList | + + ^ apiClient + getAt: agentLocation / 'v1' / 'health' / 'service' / serviceName + configuredBy: [ :request | ( request queryString: [ :queryString | queryString fieldNamed: 'filter' pairedTo: 'Checks.Status == passing' ] ) + ( request headers setAcceptTo: ZnMimeType applicationJson ) ] -] - -{ #category : #accessing } -ConsulAgentHttpAPIBasedDiscoveryClient >> withLocationOfService: serviceName do: foundBlock ifUnable: failBlock [ - - ^ Retry - value: [ - | request serviceHealthList | + withSuccessfulResponseDo: [ :responseContents | + serviceHealthList := NeoJSONObject fromString: responseContents. - request := self requestToDiscover: serviceName. - serviceHealthList := NeoJSONObject fromString: ( request applyOn: httpClient ) contents. self withLocationOfServiceOn: serviceHealthList do: foundBlock ifUnable: [ fallbackClient withLocationOfService: serviceName do: foundBlock ifUnable: failBlock ] ] +] + +{ #category : #initialization } +ConsulAgentHttpAPIBasedDiscoveryClient >> initializeQueryingAgentOn: aConsulAgentLocation chainedWith: aServiceDiscoveryClient [ + + agentLocation := aConsulAgentLocation. + fallbackClient := aServiceDiscoveryClient. + apiClient := RESTfulAPIClient cachingOnLocalMemory +] + +{ #category : #accessing } +ConsulAgentHttpAPIBasedDiscoveryClient >> withLocationOfService: serviceName do: foundBlock ifUnable: failBlock [ + + ^ Retry value: [ self discoverLocationOf: serviceName ifFound: foundBlock otherwise: failBlock ] configuredBy: [ :retry | retry upTo: 2; - on: NetworkError , ZnHttpUnsuccessful + on: NetworkError , HTTPError ] ]