Skip to content
This repository was archived by the owner on Sep 28, 2022. It is now read-only.

Commit 278345d

Browse files
Merge pull request #148 from kuzzleio/3.0.1-proposal
Release 3.0.1
2 parents ee647f9 + 9264d49 commit 278345d

38 files changed

+468
-183
lines changed

README.md

Lines changed: 90 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ A backend software, self-hostable and ready to use to power modern apps.
1111

1212
You can access the Kuzzle repository on [Github](https://github.com/kuzzleio/kuzzle)
1313

14-
1514
## SDK Documentation
1615

1716
The complete SDK documentation is available [here](http://docs.kuzzle.io/sdk-reference)
@@ -24,7 +23,7 @@ https://github.com/kuzzleio/kuzzle-sdk/issues
2423

2524
## Installation
2625

27-
You can configure your android project to get the Kuzzle's android SDK from jcenter in your build.gradle:
26+
You can configure your Android project to get Kuzzle's Android SDK from jcenter in your build.gradle:
2827

2928
buildscript {
3029
repositories {
@@ -40,6 +39,94 @@ You can configure your android project to get the Kuzzle's android SDK from jcen
4039
compile 'io.kuzzle:sdk-android:2.2.0'
4140
}
4241

42+
## Basic usage
43+
44+
```java
45+
Kuzzle kuzzle = new Kuzzle("host", new ResponseListener<Void>() {
46+
@Override
47+
public void onSuccess(Void object) {
48+
// Handle success
49+
KuzzleDocument doc = new KuzzleDocument(dataCollection);
50+
doc.setContent("foo", "bar").save();
51+
}
52+
53+
@Override
54+
public void onError(JSONObject error) {
55+
// Handle error
56+
}
57+
});
58+
```
59+
60+
## KuzzleDocument
61+
62+
KuzzleDocument is an encapsulation of a JSONObject.
63+
64+
```java
65+
KuzzleDataCollection myCollection = new KuzzleDataCollection(kuzzle, "myNewCollection");
66+
KuzzleDocument myDocument = new KuzzleDocument(myCollection);
67+
// Add properties to the body
68+
myDocument.setContent("foo", "bar");
69+
// Persist the document
70+
myDocument.save();
71+
// Send it on real time (not persistent)
72+
myDocument.publish();
73+
```
74+
75+
## Adding metadata
76+
77+
As stated [here](http://kuzzle.io/api-reference/#sending-metadata) you can add metadata to a subscription.
78+
79+
```java
80+
KuzzleOptions options = new KuzzleOptions();
81+
JSONObject metadata = new JSONObject();
82+
metadata.put("foo", "bar");
83+
options.setMetadata(metadata);
84+
myCollection.subscribe(options);
85+
```
86+
87+
# Login
88+
89+
## Prerequisite
90+
91+
To login using kuzzle you need at least one authentication plugin. You can refer [here](https://github.com/kuzzleio/kuzzle-plugin-auth-passport-local) for a local authentication plugin
92+
or [here](https://github.com/kuzzleio/kuzzle-plugin-auth-passport-oauth) to refer to our OAuth2 plugin.
93+
94+
To know more about how to log in with a Kuzzle SDK, please refer to our [documentation](http://docs.kuzzle.io/sdk-reference/kuzzle/login/)
95+
96+
If you have the kuzzle-plugin-auth-passport-local installed you can login using either the Kuzzle's constructor or the login method.
97+
98+
### Login with an OAuth strategy
99+
100+
If you have an OAUTH plugin like kuzzle-plugin-auth-passport-oauth, you may use the KuzzleWebViewClient class to handle the second authentication phase:
101+
102+
```java
103+
Handler handler = new Handler();
104+
WebView webView = (WebView) findViewById(R.id.webView);
105+
webView.setWebViewClient(kuzzle.getKuzzleWebViewClient());
106+
kuzzle.login("github", new KuzzleResponseListener<JSONObject>() {
107+
@Override
108+
public void onSuccess(final JSONObject object) {
109+
handler.post(new Runnable() {
110+
@Override
111+
public void run() {
112+
try {
113+
if (object.has("headers")) {
114+
webView.loadUrl(object.getJSONObject("headers").getString("Location"));
115+
}
116+
} catch (JSONException e) {
117+
e.printStackTrace();
118+
}
119+
}
120+
});
121+
}
122+
123+
@Override
124+
public void onError(JSONObject error) {
125+
Log.e("error", error.toString());
126+
}
127+
});
128+
```
129+
43130
## License
44131

45-
[Apache 2](LICENSE.md)
132+
[Apache 2](LICENSE)

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
22
apply plugin: 'jacoco-android'
33
apply plugin: 'com.jfrog.bintray'
44
apply plugin: 'com.github.dcendents.android-maven'
5-
version = "3.0.0"
5+
version = "3.0.1"
66
buildscript {
77
repositories {
88
jcenter()

src/main/java/io/kuzzle/sdk/core/Collection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public void scroll(String scrollId, final Options options, final JSONObject filt
176176
options.setScrollId(scrollId);
177177

178178
try {
179-
this.kuzzle.query(makeQueryArgs("document", "scroll"), request, options, new OnQueryDoneListener() {
179+
this.kuzzle.query(this.kuzzle.buildQueryArgs("document", "scroll"), request, options, new OnQueryDoneListener() {
180180
@Override
181181
public void onSuccess(JSONObject object) {
182182
try {

src/main/java/io/kuzzle/sdk/core/Document.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public void onError(JSONObject error) {
170170
/**
171171
* {@link #exists(Options, ResponseListener)}
172172
*/
173-
public void exists(@NonNull final ResponseListener<JSONObject> listener) {
173+
public void exists(@NonNull final ResponseListener<Boolean> listener) {
174174
this.exists(null, listener);
175175
}
176176

@@ -180,7 +180,7 @@ public void exists(@NonNull final ResponseListener<JSONObject> listener) {
180180
* @param options - Request options
181181
* @param listener - Response callback listener
182182
*/
183-
public void exists(final Options options, @NonNull final ResponseListener<JSONObject> listener) {
183+
public void exists(final Options options, @NonNull final ResponseListener<Boolean> listener) {
184184
if (this.id == null) {
185185
throw new IllegalStateException("Document.exists: cannot check if the document exists if no id has been provided");
186186
}
@@ -193,8 +193,11 @@ public void exists(final Options options, @NonNull final ResponseListener<JSONOb
193193
this.kuzzle.query(this.dataCollection.makeQueryArgs("document", "exists"), this.serialize(), options, new OnQueryDoneListener() {
194194
@Override
195195
public void onSuccess(JSONObject object) {
196-
listener.onSuccess(object);
197-
;
196+
try {
197+
listener.onSuccess(object.getBoolean("result"));
198+
} catch (JSONException e) {
199+
throw new RuntimeException(e);
200+
}
198201
}
199202

200203
@Override

src/main/java/io/kuzzle/sdk/core/Kuzzle.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,7 @@ public void login(@NonNull final String strategy, final JSONObject credentials,
912912
}
913913

914914
if (expiresIn >= 0) {
915-
body.put("expiresIn", expiresIn);
915+
query.put("expiresIn", expiresIn);
916916
}
917917

918918
query.put("strategy", strategy);
@@ -1303,7 +1303,7 @@ public Kuzzle removeListener(Event event, EventListener listener) {
13031303

13041304
/**
13051305
* Renew all registered subscriptions. Usually called after:
1306-
* - a connection, if subscriptions occured before
1306+
* - a connection, if subscriptions occurred before
13071307
* - a reconnection
13081308
* - after a successful login attempt, to subscribe with the new credentials
13091309
*/
@@ -1430,7 +1430,7 @@ private void reconnect() {
14301430
*/
14311431
protected Socket createSocket() throws URISyntaxException {
14321432
IO.Options opt = new IO.Options();
1433-
opt.forceNew = true;
1433+
opt.forceNew = false;
14341434
opt.reconnection = this.autoReconnect;
14351435
opt.reconnectionDelay = this.reconnectionDelay;
14361436
return IO.socket("http://" + host + ":" + this.port, opt);
@@ -2152,7 +2152,7 @@ public void whoAmI(@NonNull final ResponseListener<User> listener) {
21522152
public void onSuccess(JSONObject response) {
21532153
try {
21542154
JSONObject result = response.getJSONObject("result");
2155-
listener.onSuccess(new User(Kuzzle.this, result.getString("_id"), result.getJSONObject("_source")));
2155+
listener.onSuccess(new User(Kuzzle.this, result.getString("_id"), result.getJSONObject("_source"), result.getJSONObject("_meta")));
21562156
} catch (JSONException e) {
21572157
throw new RuntimeException(e);
21582158
}

src/main/java/io/kuzzle/sdk/enums/Scope.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ public enum Scope {
44
IN,
55
OUT,
66
ALL,
7-
NONE
7+
NONE,
8+
UNKNOWN
89
}

src/main/java/io/kuzzle/sdk/enums/Users.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package io.kuzzle.sdk.enums;
22

3-
/**
4-
* Created by kblondel on 11/12/15.
5-
*/
63
public enum Users {
74
IN,
85
OUT,

src/main/java/io/kuzzle/sdk/responses/NotificationResponse.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,12 @@ public NotificationResponse(final Kuzzle kuzzle, final JSONObject object) {
4444
this.scope = (object.isNull("scope") ? null : Scope.valueOf(object.getString("scope").toUpperCase()));
4545
this.users = (object.isNull("user") ? null : Users.valueOf(object.getString("user").toUpperCase()));
4646
if (!object.getJSONObject("result").isNull("_source")) {
47-
this.document = new Document(new Collection(kuzzle, this.collection, this.index), object.getJSONObject("result"));
48-
this.document.setId(this.result.getString("_id"));
47+
JSONObject content = object.getJSONObject("result");
48+
String id = content.getString("_id");
49+
JSONObject meta = content.isNull("_meta") ? new JSONObject() : content.getJSONObject("_meta");
50+
content.remove("_id");
51+
content.remove("_meta");
52+
this.document = new Document(new Collection(kuzzle, this.collection, this.index), id, content, meta);
4953
}
5054
} catch (JSONException e) {
5155
throw new RuntimeException(e);

src/main/java/io/kuzzle/sdk/responses/SearchResult.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.support.annotation.NonNull;
44

5+
import org.json.JSONArray;
56
import org.json.JSONException;
67
import org.json.JSONObject;
78

@@ -99,16 +100,19 @@ public JSONObject getFilters() {
99100
public void fetchNext(ResponseListener<SearchResult> listener) {
100101
JSONObject filters;
101102
Options options;
103+
102104
try {
103105
options = new Options(this.options);
104106
} catch (JSONException e) {
105107
throw new RuntimeException(e);
106108
}
107109
options.setPrevious(this);
108110

111+
// retrieve next results with scroll if original search use it
109112
if (options.getScrollId() != null) {
110-
if(this.fetchedDocument >= this.getTotal()) {
113+
if (this.fetchedDocument >= this.getTotal()) {
111114
listener.onSuccess(null);
115+
112116
return;
113117
}
114118

@@ -125,6 +129,37 @@ public void fetchNext(ResponseListener<SearchResult> listener) {
125129
return;
126130
}
127131

132+
// retrieve next results using ES's search_after
133+
if (options.getSize() != null && this.filters.has("sort")) {
134+
if (this.fetchedDocument >= this.getTotal()) {
135+
listener.onSuccess(null);
136+
137+
return;
138+
}
139+
140+
if (options.getFrom() != null) {
141+
options.setFrom(null);
142+
}
143+
144+
try {
145+
JSONArray searchAfter = new JSONArray();
146+
147+
for (int i = 0; i < this.filters.getJSONArray("sort").length(); i++) {
148+
Document doc = this.getDocuments().get(this.getDocuments().size() - 1);
149+
searchAfter.put(doc.getContent().get(this.filters.getJSONArray("sort").getJSONObject(i).keys().next()));
150+
}
151+
152+
this.filters.put("search_after", searchAfter);
153+
} catch (JSONException e) {
154+
throw new RuntimeException(e);
155+
}
156+
157+
this.collection.search(this.filters, options, listener);
158+
159+
return;
160+
}
161+
162+
// retrieve next results with from/size if original search use it
128163
if (options.getFrom() != null && options.getSize() != null) {
129164
try {
130165
filters = new JSONObject(this.filters.toString());

src/main/java/io/kuzzle/sdk/security/AbstractSecurityDocument.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class AbstractSecurityDocument {
2020
protected String updateActionName;
2121
public final String id;
2222
public JSONObject content;
23+
public JSONObject meta;
2324

2425
/**
2526
* Instantiates a new Abstract kuzzle security document.
@@ -29,7 +30,7 @@ public class AbstractSecurityDocument {
2930
* @param content Security document content
3031
* @throws JSONException
3132
*/
32-
public AbstractSecurityDocument(final Kuzzle kuzzle, @NonNull final String id, final JSONObject content) throws JSONException {
33+
public AbstractSecurityDocument(final Kuzzle kuzzle, @NonNull final String id, final JSONObject content, final JSONObject meta) throws JSONException {
3334
if (id == null) {
3435
throw new IllegalArgumentException("Cannot initialize with a null ID");
3536
}
@@ -43,6 +44,12 @@ public AbstractSecurityDocument(final Kuzzle kuzzle, @NonNull final String id, f
4344
} else {
4445
this.content = new JSONObject();
4546
}
47+
48+
if (meta != null) {
49+
setMeta(meta);
50+
} else {
51+
this.meta = new JSONObject();
52+
}
4653
}
4754

4855
/**
@@ -62,6 +69,23 @@ public AbstractSecurityDocument setContent(@NonNull final JSONObject content) th
6269
return this;
6370
}
6471

72+
/**
73+
* Sets the metadata of this object
74+
*
75+
* @param meta New metadata
76+
* @return this
77+
* @throws JSONException
78+
*/
79+
public AbstractSecurityDocument setMeta(@NonNull final JSONObject meta) throws JSONException {
80+
if (meta == null) {
81+
throw new IllegalArgumentException("AbstractSecurityDocument.setMeta: cannot set null metadata");
82+
}
83+
84+
this.meta = new JSONObject(meta.toString());
85+
86+
return this;
87+
}
88+
6589
/**
6690
* Serializes this object to a plain-old JSON object
6791
*
@@ -149,6 +173,15 @@ public JSONObject getContent() {
149173
return this.content;
150174
}
151175

176+
/**
177+
* Getter for the "meta" property
178+
*
179+
* @return the document metadata
180+
*/
181+
public JSONObject getMeta() {
182+
return this.meta;
183+
}
184+
152185
/**
153186
* {@link #update(JSONObject, Options, ResponseListener)}
154187
*/

0 commit comments

Comments
 (0)