Skip to content

Commit

Permalink
Merge pull request #9 from Mercap/server_methods_cleanup
Browse files Browse the repository at this point in the history
Improve server packaging
  • Loading branch information
juliendelplanque authored Jul 4, 2019
2 parents ed5f1c8 + c195aa4 commit 190dc62
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 70 deletions.
13 changes: 2 additions & 11 deletions src/JRPC-Common/JRPCBatchRequestsObject.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ JRPCBatchRequestsObject class >> fromJRPCJSONObject: aJSONObject [
aJSONObject isArray
ifFalse: [ JRPCIncorrectJSON signal ].

^ self new
^ self
requests: (aJSONObject collect: [ :json |
[ (JRPCMessageObject classToInstantiateForJSONObject: json forClient: false)
fromJRPCJSONObject: json
] on: JRPCIncorrectJSON do: [ :error |
error asJRPCResponse ] ]);
yourself
error asJRPCResponse ] ])
]

{ #category : #'instance creation' }
Expand All @@ -38,14 +37,6 @@ JRPCBatchRequestsObject >> asJRPCJSON [
^ self requests collect: #asJRPCJSON
]

{ #category : #'server handling' }
JRPCBatchRequestsObject >> beHandledByServer: aJRPCServer [
^ JRPCBatchResponseObject new
responses: (self requests collect: [ :req |
req beHandledByServer: aJRPCServer ]);
yourself
]

{ #category : #accessing }
JRPCBatchRequestsObject >> requests [
^ requests
Expand Down
6 changes: 0 additions & 6 deletions src/JRPC-Common/JRPCErrorResponseObject.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@ JRPCErrorResponseObject >> asJRPCJSON [
yourself
]

{ #category : #'server handling' }
JRPCErrorResponseObject >> beHandledByServer: aJRPCServer [
"Returns self, this method is executed when a request inside a batch generated an error."
^ self
]

{ #category : #accessing }
JRPCErrorResponseObject >> error [
^ error
Expand Down
19 changes: 0 additions & 19 deletions src/JRPC-Common/JRPCNotificationObject.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,3 @@ JRPCNotificationObject >> asJRPCJSON [
dict removeKey: 'id'.
^ dict
]

{ #category : #'server handling' }
JRPCNotificationObject >> beHandledByServer: aJRPCServer [
| response |
response := super beHandledByServer: aJRPCServer.
"If the response is an error invalid request, it should be returned even if the request was a notification."
(response isError and: [ response isInvalidRequest ])
ifTrue: [ ^ response ].

^ JRPCEmptyResponseObject new
]

{ #category : #'server handling' }
JRPCNotificationObject >> convertErrorToResponse: jrpcError [
jrpcError isIncorrectJSON
ifTrue: [ ^ jrpcError asJRPCResponseWithId: self id ].

^ JRPCEmptyResponseObject new
]
10 changes: 0 additions & 10 deletions src/JRPC-Common/JRPCRequestObject.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,6 @@ JRPCRequestObject >> asJRPCJSON [
^ dict
]

{ #category : #'server handling' }
JRPCRequestObject >> beHandledByServer: aJRPCServer [
^ aJRPCServer handleJRPCRequestObject: self
]

{ #category : #'server handling' }
JRPCRequestObject >> convertErrorToResponse: jrpcError [
^ jrpcError asJRPCResponseWithId: self id
]

{ #category : #accessing }
JRPCRequestObject >> method [
^ method
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Extension { #name : #BlockClosure }

{ #category : #'*JRPC-Common' }
{ #category : #'*JRPC-Server' }
BlockClosure >> jrpcExecuteWithParameters: anArrayOrDictionary [
| arguments |
anArrayOrDictionary ifNil: [
Expand All @@ -15,12 +15,12 @@ BlockClosure >> jrpcExecuteWithParameters: anArrayOrDictionary [
^ self valueWithArguments: arguments
]

{ #category : #'*JRPC-Common' }
{ #category : #'*JRPC-Server' }
BlockClosure >> jrpcParametersCount [
^ self argumentCount
]

{ #category : #'*JRPC-Common' }
{ #category : #'*JRPC-Server' }
BlockClosure >> jrpcParametersNames [
^ self argumentNames asSet
]
9 changes: 9 additions & 0 deletions src/JRPC-Server/JRPCBatchRequestsObject.extension.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Extension { #name : #JRPCBatchRequestsObject }

{ #category : #'*JRPC-Server' }
JRPCBatchRequestsObject >> beHandledByServer: aJRPCServer [
^ JRPCBatchResponseObject new
responses: (self requests collect: [ :req |
req beHandledByServer: aJRPCServer ]);
yourself
]
7 changes: 7 additions & 0 deletions src/JRPC-Server/JRPCErrorResponseObject.extension.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Extension { #name : #JRPCErrorResponseObject }

{ #category : #'*JRPC-Server' }
JRPCErrorResponseObject >> beHandledByServer: aJRPCServer [
"Returns self, this method is executed when a request inside a batch generated an error."
^ self
]
13 changes: 13 additions & 0 deletions src/JRPC-Server/JRPCHTTPServer.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ JRPCHTTPServer class >> defaultPort [
^ 4000
]

{ #category : #accessing }
JRPCHTTPServer >> debugMode [

^ server debugMode
]

{ #category : #accessing }
JRPCHTTPServer >> debugMode: boolean [

server debugMode: boolean
]

{ #category : #defaults }
JRPCHTTPServer >> defaultPort [
^ self class defaultPort
Expand All @@ -38,6 +50,7 @@ JRPCHTTPServer >> initialize [

{ #category : #accessing }
JRPCHTTPServer >> port [

^ server port
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ I am an error raised when the parameters of the method called are invalid.
Class {
#name : #JRPCInvalidParameters,
#superclass : #JRPCError,
#category : #'JRPC-Common-Errors'
#category : #'JRPC-Server'
}

{ #category : #converting }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,10 @@ I am an error raised when the handler required does not exist.
Class {
#name : #JRPCNonExistentHandler,
#superclass : #JRPCError,
#instVars : [
'handlerName'
],
#category : #'JRPC-Common-Errors'
#category : #'JRPC-Server'
}

{ #category : #converting }
JRPCNonExistentHandler >> asJRPCResponseWithId: anInteger [
^ JRPCErrorResponseObject id: anInteger error: JRPCErrorObject methodNotFound
]

{ #category : #accessing }
JRPCNonExistentHandler >> handlerName [
^ handlerName
]

{ #category : #accessing }
JRPCNonExistentHandler >> handlerName: anObject [
handlerName := anObject
]
20 changes: 20 additions & 0 deletions src/JRPC-Server/JRPCNotificationObject.extension.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Extension { #name : #JRPCNotificationObject }

{ #category : #'*JRPC-Server' }
JRPCNotificationObject >> beHandledByServer: aJRPCServer [
| response |
response := super beHandledByServer: aJRPCServer.
"If the response is an error invalid request, it should be returned even if the request was a notification."
(response isError and: [ response isInvalidRequest ])
ifTrue: [ ^ response ].

^ JRPCEmptyResponseObject new
]

{ #category : #'*JRPC-Server' }
JRPCNotificationObject >> convertErrorToResponse: jrpcError [
jrpcError isIncorrectJSON
ifTrue: [ ^ jrpcError asJRPCResponseWithId: self id ].

^ JRPCEmptyResponseObject new
]
11 changes: 11 additions & 0 deletions src/JRPC-Server/JRPCRequestObject.extension.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Extension { #name : #JRPCRequestObject }

{ #category : #'*JRPC-Server' }
JRPCRequestObject >> beHandledByServer: aJRPCServer [
^ aJRPCServer handleJRPCRequestObject: self
]

{ #category : #'*JRPC-Server' }
JRPCRequestObject >> convertErrorToResponse: jrpcError [
^ jrpcError asJRPCResponseWithId: self id
]
55 changes: 49 additions & 6 deletions src/JRPC-Tests/JRPCHttpServerTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ JRPCHttpServerTest >> checkPortAvailability [
do: [ :error | self fail: ( 'Port <1p> is not available' expandMacrosWith: self port ) ]
]

{ #category : #private }
JRPCHttpServerTest >> newJRPCClient [

^ ( JRPCClient http: ( 'http://localhost' asUrl port: self port ) )
ifFail: [ self fail ];
yourself
]

{ #category : #private }
JRPCHttpServerTest >> port [

Expand All @@ -30,7 +38,12 @@ JRPCHttpServerTest >> setUp [
super setUp.
self checkPortAvailability.
server := JRPCServer http.
server port: self port.
server
port: self port;
debugMode: true.
self
assert: server port equals: self port;
assert: server debugMode.
server start
]

Expand All @@ -42,6 +55,32 @@ JRPCHttpServerTest >> tearDown [
super tearDown
]

{ #category : #accessing }
JRPCHttpServerTest >> testNotification [

| notificationCount |

notificationCount := 0.
server addHandlerNamed: 'mail_sent' block: [ notificationCount := notificationCount + 1 ].

self newJRPCClient notifyMethod: 'mail_sent'.

self assert: notificationCount equals: 1
]

{ #category : #accessing }
JRPCHttpServerTest >> testNotificationWithInvalidMethod [

| notificationCount |

notificationCount := 0.
server addHandlerNamed: 'mail_sent' block: [ notificationCount := notificationCount + 1 ].

self newJRPCClient notifyMethod: 'invalid'.

self assert: notificationCount equals: 0
]

{ #category : #accessing }
JRPCHttpServerTest >> testRequestOnInvalidEndpoint [

Expand All @@ -62,13 +101,17 @@ JRPCHttpServerTest >> testRequestOnInvalidEndpoint [
]

{ #category : #accessing }
JRPCHttpServerTest >> testValidRequest [
JRPCHttpServerTest >> testRequestWithoutParameters [

| httpClient |
server addHandlerNamed: 'zero' block: [ 0 ].

server addHandlerNamed: 'sum' block: [ :a :b | a + b ].
self assert: ( self newJRPCClient callMethod: 'zero' withId: 3 ) result equals: 0
]

{ #category : #accessing }
JRPCHttpServerTest >> testValidRequest [

httpClient := JRPCClient http: ( 'http://localhost' asUrl port: self port ).
server addHandlerNamed: 'sum' block: [ :a :b | a + b ].

self assert: ( httpClient callMethod: 'sum' arguments: #(1 3) withId: 1 ) result equals: 4
self assert: ( self newJRPCClient callMethod: 'sum' arguments: #(1 3) withId: 1 ) result equals: 4
]

0 comments on commit 190dc62

Please sign in to comment.