Skip to content

Commit

Permalink
Improve DX for Query method (#66)
Browse files Browse the repository at this point in the history
# What does this PR do ?

Improves the DX of the Query method
- Add a RawJson object that designate a string that should represent a Json Object and should not be escaped, RawJson can be given to `sdk.query` or be put anywhere in a map or object
- Add RequestPayload class, mostly used in Kotlin to instantiate new query without having to use `map.put` multiples times in row
- The serializer is now capable of serializing Java Objects directly
  • Loading branch information
Shiranuit authored Mar 11, 2022
1 parent b80d428 commit 0860d9f
Show file tree
Hide file tree
Showing 46 changed files with 461 additions and 135 deletions.
1 change: 1 addition & 0 deletions .ci/doc/templates/default.tpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import java.util.concurrent.ExecutionException
import io.kuzzle.sdk.coreClasses.responses.Response
import io.kuzzle.sdk.coreClasses.SearchResult
import io.kuzzle.sdk.coreClasses.lang.Lang
import java.util.*;

fun main() {
val ws = WebSocket("kuzzle")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: java-auth#checkRights
description: Checks if an API action can be executed by the current user
hooks:
before: curl -X POST kuzzle:7512/users/foo/_create -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo
before: curl -X POST kuzzle:7512/users/foo/_create?refresh=wait_for -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo?refresh=wait_for
template: print-result
expected: true
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: kotlin-auth#checkRights
description: Checks if an API action can be executed by the current user
hooks:
before: curl -X POST kuzzle:7512/users/foo/_create -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo
before: curl -X POST kuzzle:7512/users/foo/_create?refresh=wait_for -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo?refresh=wait_for
template: print-result
expected: true
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: java-Auth#CheckToken
description: Checks an authentication token's validity.
hooks:
before: curl -X POST kuzzle:7512/users/foo/_create -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo
before: curl -X POST kuzzle:7512/users/foo/_create?refresh=wait_for -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo?refresh=wait_for
template: default
expected: Success
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: kotlin-Auth#CheckToken
description: Checks an authentication token's validity.
hooks:
before: curl -X POST kuzzle:7512/users/foo/_create -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo
before: curl -X POST kuzzle:7512/users/foo/_create?refresh=wait_for -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo?refresh=wait_for
template: default
expected: Success
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: java-Auth#CreateMyCredentials
description: Creates the current user's credentials for the specified strategy.
hooks:
before: curl -X POST kuzzle:7512/users/foo/_create -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo
before: curl -X POST kuzzle:7512/users/foo/_create?refresh=wait_for -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo?refresh=wait_for
template: catch
expected: io.kuzzle.sdk.coreClasses.exceptions.ApiErrorException:\ Unknown\ authentication\ strategy\ "other"
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: kotlin-Auth#CreateMyCredentials
description: Creates the current user's credentials for the specified strategy.
hooks:
before: curl -X POST kuzzle:7512/users/foo/_create -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo
before: curl -X POST kuzzle:7512/users/foo/_create?refresh=wait_for -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo?refresh=wait_for
template: catch
expected: io.kuzzle.sdk.coreClasses.exceptions.ApiErrorException:\ Unknown\ authentication\ strategy\ "other"
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: java-Auth#CredentialsExist
description: Checks that the current user has credentials for the specified strategy.
hooks:
before: curl -X POST kuzzle:7512/users/foo/_create -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo
before: curl -X POST kuzzle:7512/users/foo/_create?refresh=wait_for -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo?refresh=wait_for
template: print-result
expected: "true"
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: kotlin-Auth#CredentialsExist
description: Checks that the current user has credentials for the specified strategy.
hooks:
before: curl -X POST kuzzle:7512/users/foo/_create -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo
before: curl -X POST kuzzle:7512/users/foo/_create?refresh=wait_for -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo?refresh=wait_for
template: print-result
expected: "true"
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: java-Auth#DeleteMyCredentials
description: Deletes the current user's credentials for the specified.
hooks:
before: curl -X POST kuzzle:7512/users/foo/_create -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo
before: curl -X POST kuzzle:7512/users/foo/_create?refresh=wait_for -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo?refresh=wait_for
template: default
expected: Success
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: kotlin-Auth#DeleteMyCredentials
description: Deletes the current user's credentials for the specified.
hooks:
before: curl -X POST kuzzle:7512/users/foo/_create -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo
before: curl -X POST kuzzle:7512/users/foo/_create?refresh=wait_for -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo?refresh=wait_for
template: default
expected: Success
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: java-Auth#GetCurrentUser
description: Returns the profile object for the user linked to the `JSON Web Token`.
hooks:
before: curl -X POST kuzzle:7512/users/foo/_create -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo
before: curl -X POST kuzzle:7512/users/foo/_create?refresh=wait_for -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo?refresh=wait_for
template: print-result
expected: ^{strategies=\[local\],\ _source={profileIds=\[default\],\ _kuzzle_info={.*}},\ _id=foo}$
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: kotlin-Auth#GetCurrentUser
description: Returns the profile object for the user linked to the `JSON Web Token`.
hooks:
before: curl -X POST kuzzle:7512/users/foo/_create -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo
before: curl -X POST kuzzle:7512/users/foo/_create?refresh=wait_for -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo?refresh=wait_for
template: print-result
expected: ^{strategies=\[local\],\ _source={profileIds=\[default\],\ _kuzzle_info={.*}},\ _id=foo}$
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name: java-Auth#GetMyCredentials
description: Returns the current user's credential information for the specified strategy.
hooks:
before: curl -X POST kuzzle:7512/users/foo/_create -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo
before: curl -X POST kuzzle:7512/users/foo/_create?refresh=wait_for -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo?refresh=wait_for
template: print-result
expected:
- {kuid=foo,\ username=foo}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name: kotlin-Auth#GetMyCredentials
description: Returns the current user's credential information for the specified strategy.
hooks:
before: curl -X POST kuzzle:7512/users/foo/_create -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo
before: curl -X POST kuzzle:7512/users/foo/_create?refresh=wait_for -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo?refresh=wait_for
template: print-result
expected:
- {kuid=foo,\ username=foo}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: java-Auth#GetMyRights
description: Returns the rights for the user linked to the `JSON Web Token`.
hooks:
before: curl -X POST kuzzle:7512/users/foo/_create -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo
before: curl -X POST kuzzle:7512/users/foo/_create?refresh=wait_for -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo?refresh=wait_for
template: print-result
expected: [{controller=*,\ action=*,\ index=*,\ collection=*,\ value=allowed}]
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: kotlin-Auth#GetMyRights
description: Returns the rights for the user linked to the `JSON Web Token`.
hooks:
before: curl -X POST kuzzle:7512/users/foo/_create -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo
before: curl -X POST kuzzle:7512/users/foo/_create?refresh=wait_for -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo?refresh=wait_for
template: print-result
expected: [{controller=*,\ action=*,\ index=*,\ collection=*,\ value=allowed}]
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: java-Auth#GetStrategies
description: Returns the rights for the user linked to the `JSON Web Token`.
hooks:
before: curl -X POST kuzzle:7512/users/foo/_create -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo
before: curl -X POST kuzzle:7512/users/foo/_create?refresh=wait_for -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo?refresh=wait_for
template: print-result
expected: [local]
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: kotlin-Auth#GetStrategies
description: Returns the rights for the user linked to the `JSON Web Token`.
hooks:
before: curl -X POST kuzzle:7512/users/foo/_create -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo
before: curl -X POST kuzzle:7512/users/foo/_create?refresh=wait_for -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo?refresh=wait_for
template: print-result
expected: [local]
4 changes: 2 additions & 2 deletions doc/1/controllers/auth/login/snippets/login-java.test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name: java-Auth#Login
description: Authenticates a user.
hooks:
before: curl -X POST kuzzle:7512/users/foo/_create -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo
before: curl -X POST kuzzle:7512/users/foo/_create?refresh=wait_for -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo?refresh=wait_for
template: print-result
expected: ^{jwt=.*,\ _id=foo,\ ttl=3600000,\ expiresAt=[0-9]+}$
4 changes: 2 additions & 2 deletions doc/1/controllers/auth/login/snippets/login-kotlin.test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name: kotlin-Auth#Login
description: Authenticates a user.
hooks:
before: curl -X POST kuzzle:7512/users/foo/_create -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo
before: curl -X POST kuzzle:7512/users/foo/_create?refresh=wait_for -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo?refresh=wait_for
template: print-result
expected: ^{jwt=.*,\ _id=foo,\ ttl=3600000,\ expiresAt=[0-9]+}$
4 changes: 2 additions & 2 deletions doc/1/controllers/auth/logout/snippets/logout-java.test.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: java-Auth#Logout
description: Revokes the user's token & unsubscribe them from registered rooms.
hooks:
before: curl -X POST kuzzle:7512/users/foo/_create -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo
before: curl -X POST kuzzle:7512/users/foo/_create?refresh=wait_for -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo?refresh=wait_for
template: default
expected: Success
4 changes: 2 additions & 2 deletions doc/1/controllers/auth/logout/snippets/logout-kotlin.test.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: kotlin-Auth#Logout
description: Revokes the user's token & unsubscribe them from registered rooms.
hooks:
before: curl -X POST kuzzle:7512/users/foo/_create -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo
before: curl -X POST kuzzle:7512/users/foo/_create?refresh=wait_for -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo?refresh=wait_for
template: default
expected: Success
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: java-Auth#RefreshToken
description: Refreshes an authentication token.
hooks:
before: curl -X POST kuzzle:7512/users/foo/_create -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo
before: curl -X POST kuzzle:7512/users/foo/_create?refresh=wait_for -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo?refresh=wait_for
template: print-result
expected: ^{jwt=.*,\ _id=foo,\ ttl=3600000,\ expiresAt=[0-9]+}$
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: kotlin-Auth#RefreshToken
description: Refreshes an authentication token.
hooks:
before: curl -X POST kuzzle:7512/users/foo/_create -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo
before: curl -X POST kuzzle:7512/users/foo/_create?refresh=wait_for -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo?refresh=wait_for
template: print-result
expected: ^{jwt=.*,\ _id=foo,\ ttl=3600000,\ expiresAt=[0-9]+}$
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@
query.put("action", "createApiKey");
query.put("userId", "jared.doe");
query.put("refresh", "wait_for");
query.put("_id", "lora-key");
query.put("body", description);

kuzzle.query(query).get();

description.put("description", "Sigfox API key");
query.put("_id", "sigfox-key");
query.put("body", description);
kuzzle.query(query).get();

description.put("description", "LoRa 6 month API key");
query.put("_id", "lora-temp-key");
query.put("body", description);
query.put("expiresIn", 36000);
kuzzle.query(query).get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ hooks:
}
}' "kuzzle:7512/users/jared.doe/_create?refresh=wait_for"
after:
curl -XDELETE kuzzle:7512/users/jared.doe
curl -XDELETE kuzzle:7512/users/jared.doe?refresh=wait_for
template: default
expected:
- Found 2 API keys matching 'LoRa'
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@ val query: HashMap<String?, Any?> = HashMap<String?, Any?>().apply {
put("action", "createApiKey")
put("userId", "jared.doe")
put("refresh", "wait_for")
put("_id", "lora-key")
put("body", description)
}

kuzzle.query(query).get()


description.put("description", "Sigfox API key")
query.put("_id", "sigfox-key")
query.put("body", description);
kuzzle.query(query).get()

description.put("description", "LoRa 6 month API key")
query.put("_id", "lora-temp-key")
query.put("body", description);
query.put("expiresIn", 36000);
kuzzle.query(query).get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ hooks:
}
}' "kuzzle:7512/users/jared.doe/_create?refresh=wait_for"
after:
curl -XDELETE kuzzle:7512/users/jared.doe
curl -XDELETE kuzzle:7512/users/jared.doe?refresh=wait_for
template: default
expected:
- Found 2 API keys matching 'LoRa'
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@
query.put("action", "createApiKey");
query.put("userId", "jared.doe");
query.put("refresh", "wait_for");
query.put("_id", "lora-key");
query.put("body", description);

kuzzle.query(query).get();

description.put("description", "Sigfox API key");
query.put("_id", "sigfox-key");
query.put("body", description);
kuzzle.query(query).get();

description.put("description", "LoRa 6 month API key");
query.put("_id", "lora-temp-key");
query.put("body", description);
query.put("expiresIn", 36000);
kuzzle.query(query).get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ hooks:
}
}' "kuzzle:7512/users/jared.doe/_create?refresh=wait_for"
after:
curl -XDELETE kuzzle:7512/users/jared.doe
curl -XDELETE kuzzle:7512/users/jared.doe?refresh=wait_for
template: default
expected:
- Found 1 API key
Loading

0 comments on commit 0860d9f

Please sign in to comment.