Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use API Client instead of a plain ZnClient #50

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ Class {
#name : #ConsulAgentHttpAPIBasedDiscoveryClient,
#superclass : #ServiceDiscoveryClient,
#instVars : [
'httpClient',
'agentLocation',
'fallbackClient'
'fallbackClient',
'apiClient'
],
#category : #'Superluminal-Service-Discovery'
}
Expand All @@ -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
]
]

Expand Down