Skip to content

Commit cb69b3d

Browse files
author
spoeck
committed
feat: add resetContexts
1 parent d88be80 commit cb69b3d

File tree

4 files changed

+88
-11
lines changed

4 files changed

+88
-11
lines changed

android/src/main/java/de/innfactory/apiai/RNApiAiModule.java

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.facebook.react.bridge.ReactMethod;
1010
import com.google.gson.Gson;
1111
import com.google.gson.reflect.TypeToken;
12+
import com.facebook.react.bridge.Promise;
1213

1314
import java.util.HashMap;
1415
import java.util.Map;
@@ -19,6 +20,7 @@
1920
import ai.api.android.AIConfiguration;
2021
import ai.api.android.AIDataService;
2122
import ai.api.android.AIService;
23+
import ai.api.android.SessionIdStorage;
2224
import ai.api.model.AIError;
2325
import ai.api.model.AIRequest;
2426
import ai.api.model.AIResponse;
@@ -69,6 +71,8 @@ public class RNApiAiModule extends ReactContextBaseJavaModule implements AIListe
6971
private Callback onListeningFinishedCallback;
7072
private Callback onAudioLevelCallback;
7173

74+
private String accessToken;
75+
7276

7377
public RNApiAiModule(ReactApplicationContext reactContext) {
7478
super(reactContext);
@@ -82,25 +86,28 @@ public String getName() {
8286

8387
@ReactMethod
8488
public void setConfiguration(String clientAccessToken, String languageTag) {
89+
this.accessToken = clientAccessToken;
8590
config = new AIConfiguration(clientAccessToken, AIConfiguration.SupportedLanguages.fromLanguageTag(languageTag), AIConfiguration.RecognitionEngine.System);
8691
}
8792

8893
@ReactMethod
8994
public void setContextsAsJson(String contextsAsJson) {
90-
Gson gson= new Gson();
91-
contexts = gson.fromJson(contextsAsJson, new TypeToken<List<Entity>>(){}.getType());
95+
Gson gson = new Gson();
96+
contexts = gson.fromJson(contextsAsJson, new TypeToken<List<Entity>>() {
97+
}.getType());
9298
}
9399

94100

95101
@ReactMethod
96102
public void setEntitiesAsJson(String userEntitiesAsJson) throws AIServiceException {
97-
Gson gson= new Gson();
98-
entities = gson.fromJson(userEntitiesAsJson, new TypeToken<List<Entity>>(){}.getType());
103+
Gson gson = new Gson();
104+
entities = gson.fromJson(userEntitiesAsJson, new TypeToken<List<Entity>>() {
105+
}.getType());
99106
}
100107

101108

102109
@ReactMethod
103-
public void startListening(Callback onResult, Callback onError){//, Callback onListeningStarted, Callback onListeningCanceled, Callback onListeningFinished, Callback onAudioLevel) {
110+
public void startListening(Callback onResult, Callback onError) {//, Callback onListeningStarted, Callback onListeningCanceled, Callback onListeningFinished, Callback onAudioLevel) {
104111

105112
onResultCallback = onResult;
106113
onErrorCallback = onError;
@@ -135,9 +142,9 @@ public void stopListening() {
135142

136143
public void run() {
137144

138-
if (aiService != null) {
139-
aiService.stopListening();
140-
}
145+
if (aiService != null) {
146+
aiService.stopListening();
147+
}
141148
}
142149
});
143150
}
@@ -157,7 +164,6 @@ public void run() {
157164
}
158165

159166

160-
161167
@Override
162168
public void onResult(AIResponse response) {
163169

@@ -267,7 +273,6 @@ public void requestQuery(String query, Callback onResult, Callback onError) {
267273
aiRequest.setQuery(query);
268274

269275

270-
271276
new AsyncTask<AIRequest, Void, AIResponse>() {
272277
@Override
273278
protected AIResponse doInBackground(AIRequest... requests) {
@@ -290,7 +295,7 @@ protected AIResponse doInBackground(AIRequest... requests) {
290295

291296
return response;
292297
} catch (AIServiceException e) {
293-
Gson gson= new Gson();
298+
Gson gson = new Gson();
294299
try {
295300
onErrorCallback.invoke(gson.toJson(e));
296301
} catch (Exception e1) {
@@ -299,6 +304,7 @@ protected AIResponse doInBackground(AIRequest... requests) {
299304
}
300305
return null;
301306
}
307+
302308
@Override
303309
protected void onPostExecute(AIResponse aiResponse) {
304310
if (aiResponse != null) {
@@ -308,6 +314,16 @@ protected void onPostExecute(AIResponse aiResponse) {
308314
}.execute(aiRequest);
309315
}
310316

317+
@ReactMethod
318+
public void getAccessToken(Promise promise) {
319+
promise.resolve(accessToken);
320+
}
321+
322+
@ReactMethod
323+
public void getSessionId(Promise promise) {
324+
promise.resolve(SessionIdStorage.getSessionId(getReactApplicationContext()));
325+
}
326+
311327
@Override
312328
public Map<String, Object> getConstants() {
313329
final Map<String, Object> constants = new HashMap<>();

index.android.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
'use strict';
22

33
import { NativeModules } from 'react-native';
4+
import ResetContextsRequest from './js/ResetContextsRequest';
45

56
let ApiAi = NativeModules.ApiAi;
67

78
ApiAi.setContexts = (contexts) => {
89
ApiAi.setContextsAsJson(JSON.stringify(contexts))
910
};
1011

12+
ApiAi.resetContexts = async (onResult: ()=>{}, onError: ()=>{}) => {
13+
const accessToken = await ApiAi.getAccessToken();
14+
const sessionId = await ApiAi.getSessionId();
15+
let request = new ResetContextsRequest(accessToken, sessionId, null);
16+
request.perform().then(res=>onResult(res)).catch(err=>onError(err));
17+
};
18+
1119
ApiAi.setEntities = (entities) => {
1220
ApiAi.setEntitiesAsJson(JSON.stringify(entities))
1321
};

index.ios.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
import { NativeModules, NativeAppEventEmitter } from 'react-native';
33
import {ApiAiClient} from 'api-ai-javascript';
4+
import ResetContextsRequest from './js/ResetContextsRequest';
45

56
var SpeechToText = NativeModules.RNSpeechToTextIos;
67

@@ -55,6 +56,11 @@ class ApiAi {
5556
this.contexts = contexts;
5657
}
5758

59+
resetContexts(onResult: ()=>{}, onError: ()=>{}) {
60+
let request = new ResetContextsRequest(this.client.getAccessToken(), this.client.getSessionId(), null);
61+
request.perform().then(res=>onResult(res)).catch(err=>onError(err));
62+
};
63+
5864
setEntities(entities) {
5965
this.entities = entities;
6066
}

js/ResetContextsRequest.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Created by toni on 30.08.2017.
3+
*/
4+
import { ApiAiRequestError } from "api-ai-javascript/es6/Errors";
5+
import XhrRequest from 'api-ai-javascript/es6/XhrRequest';
6+
import {ApiAiConstants} from 'api-ai-javascript';
7+
8+
class ResetContextsRequest {
9+
constructor(accessToken, sessionId, contextName) {
10+
11+
if (contextName != null) {
12+
this.uri = ApiAiConstants.DEFAULT_BASE_URL + "contexts/" + contextName + "?sessionId=" + sessionId;
13+
} else {
14+
this.uri = ApiAiConstants.DEFAULT_BASE_URL + "contexts?sessionId=" + sessionId;
15+
}
16+
17+
this.headers = {
18+
Authorization: "Bearer " + accessToken,
19+
Accept: "application/json",
20+
};
21+
}
22+
static handleSuccess(xhr) {
23+
return Promise.resolve(JSON.parse(xhr.responseText));
24+
}
25+
static handleError(xhr) {
26+
let error = new ApiAiRequestError(null);
27+
try {
28+
const serverResponse = JSON.parse(xhr.responseText);
29+
if (serverResponse.status && serverResponse.status.errorDetails) {
30+
error = new ApiAiRequestError(serverResponse.status.errorDetails, serverResponse.status.code);
31+
}
32+
else {
33+
error = new ApiAiRequestError(xhr.statusText, xhr.status);
34+
}
35+
}
36+
catch (e) {
37+
error = new ApiAiRequestError(xhr.statusText, xhr.status);
38+
}
39+
return Promise.reject(error);
40+
}
41+
perform() {
42+
return XhrRequest.delete(this.uri, null, this.headers, {})
43+
.then(ResetContextsRequest.handleSuccess.bind(this))
44+
.catch(ResetContextsRequest.handleError.bind(this));
45+
}
46+
}
47+
export default ResetContextsRequest;

0 commit comments

Comments
 (0)