Skip to content

Commit 6d728b9

Browse files
author
Adriano Santos
committed
Fixes & Refactory
1 parent cb8d5dd commit 6d728b9

13 files changed

Lines changed: 363 additions & 269 deletions

src/main/java/io/notifye/botengine/client/Engine.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
public final class Engine {
3535
private static final Logger log = LoggerFactory.getLogger(Engine.class);
3636

37-
private static final String STORIES_RESOURCE = "/stories";
37+
private static final String STORIES_RESOURCE = "%s/stories";
38+
private static final String STORY_RESOURCE = "%s/stories/%s";
3839
private static final String WELCOME_RESOURCE = "%s/stories/%s/interactions/welcome";
3940
private static final String INTERACTIONS_RESOURCE = "%s/stories/%s/interactions";
4041
private static final String FALLBACK_RESOURCE = "%s/stories/%s/interactions/fallback";
@@ -43,10 +44,9 @@ public final class Engine {
4344
private static RestTemplate client;
4445

4546
// Stories
46-
4747
public static Story getStory(String id, Token token) throws BotException{
4848
Story story = null;
49-
String uri = Bot.API_URL + STORIES_RESOURCE + "/" + id;
49+
String uri = getStoryUriResourceById(id);
5050
try{
5151
HttpHeaders headers = getDevHeaders(token);
5252

@@ -73,7 +73,7 @@ public static Story getStory(String id, Token token) throws BotException{
7373
}
7474

7575
public static Story createStory(Story story, Token token) throws BotException{
76-
String uri = Bot.API_URL + STORIES_RESOURCE;
76+
String uri = getRootStoryUriResource();
7777
log.info("Create Story with type -> {}", story);
7878
HttpHeaders headers = getDevHeaders(token);
7979
HttpEntity<Story> request = new HttpEntity<>(story, headers);
@@ -89,7 +89,7 @@ public static Story createStory(Story story, Token token) throws BotException{
8989
node = getObjectMapper().readValue(body, JsonNode.class);
9090
JsonNode idNode = node.get("id");
9191

92-
//TODO: Validade npe exception
92+
//TODO: Validate npe exception
9393
String id = idNode.asText();
9494
story.setId(id);
9595
log.info("Create Story result -> {}", story);
@@ -101,7 +101,7 @@ public static Story createStory(Story story, Token token) throws BotException{
101101
}
102102

103103
public static void deleteStory(String id, Token token) {
104-
String uri = Bot.API_URL + STORIES_RESOURCE + "/" + id;
104+
String uri = getStoryUriResourceById(id);
105105
log.debug("Delete Story by Id -> {}", id);
106106
HttpHeaders headers = getDevHeaders(token);
107107
HttpEntity<Story> request = new HttpEntity<>(headers);
@@ -172,7 +172,6 @@ public static Interaction createOrUpdateFalbackInteraction(Story story, Interact
172172
return interaction;
173173
}
174174

175-
176175
//Entities
177176
public static Entity createEntity(Entity entity, Token token){
178177
final String ENTITY_URI_RESOURCE = String.format("%s/entities", Bot.API_URL);
@@ -222,19 +221,29 @@ public static String getSession(){
222221
return String.valueOf(number);
223222
}
224223

224+
private static String getRootStoryUriResource(){
225+
return String.format(STORIES_RESOURCE, Bot.API_URL);
226+
}
227+
228+
private static String getStoryUriResourceById(String id){
229+
return String.format(STORY_RESOURCE, Bot.API_URL, id);
230+
}
231+
225232
private static QueryResponse q(Query query, Token token){
226233
final String ENTITY_URI_RESOURCE = String.format(QUERY_RESOURCE, Bot.API_URL);
227234
HttpHeaders headers = getDevHeaders(token);
228235
HttpEntity<Query> request = new HttpEntity<>(query, headers);
229236

230-
ResponseEntity<String> entityResponse = getClient().exchange(ENTITY_URI_RESOURCE, HttpMethod.POST, request, String.class);
237+
ResponseEntity<QueryResponse> entityResponse = getClient().exchange(ENTITY_URI_RESOURCE, HttpMethod.POST, request, QueryResponse.class);
238+
//ResponseEntity<String> entityResponse = getClient().exchange(ENTITY_URI_RESOURCE, HttpMethod.POST, request, String.class);
231239
log.info("Entity response -> {}", entityResponse);
232240

233241
QueryResponse response = null;
234242
if(entityResponse.getStatusCode().is2xxSuccessful()){
235243
log.info("Query executed suscessfull");
236-
response = QueryResponse.builder()
237-
.build();
244+
/*response = QueryResponse.builder()
245+
.build();*/
246+
response = entityResponse.getBody();
238247
}
239248
return response;
240249

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
package io.notifye.botengine.client;
2-
3-
4-
import io.notifye.botengine.client.BotEngine;
5-
import io.notifye.botengine.client.BotEngine.TokenType;
6-
import lombok.Builder;
7-
import lombok.Data;
8-
9-
@Builder
10-
public @Data class Token {
11-
12-
private String token;
13-
private TokenType tokenType;
14-
15-
}
1+
package io.notifye.botengine.client;
2+
3+
4+
import io.notifye.botengine.client.BotEngine.TokenType;
5+
import lombok.Builder;
6+
import lombok.Data;
7+
import lombok.ToString;
8+
9+
@Builder
10+
@ToString
11+
public @Data class Token {
12+
private String token;
13+
private TokenType tokenType;
14+
}
Lines changed: 92 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,93 @@
1-
package io.notifye.botengine.client.action.controller;
2-
3-
import java.util.ArrayList;
4-
import java.util.List;
5-
import java.util.concurrent.CopyOnWriteArrayList;
6-
7-
import io.notifye.botengine.client.Engine;
8-
import io.notifye.botengine.client.Token;
9-
import io.notifye.botengine.client.action.InteractionAction;
10-
import io.notifye.botengine.client.bots.Bot;
11-
import io.notifye.botengine.client.model.Interaction;
12-
import io.notifye.botengine.client.model.enums.InteractionType;
13-
import lombok.extern.slf4j.Slf4j;
14-
15-
@Slf4j
16-
public final class InteractionsActionController implements InteractionAction {
17-
18-
private static final long serialVersionUID = 1L;
19-
20-
private final String ACTION_NAME = InteractionAction.class.getSimpleName();
21-
22-
private final Bot bot;
23-
private final Token token;
24-
private List<Interaction> interactions;
25-
26-
public InteractionsActionController(final Bot bot, final Token token) {
27-
super();
28-
this.bot = bot;
29-
this.token = token;
30-
}
31-
32-
public List<Interaction> getInteractions(){
33-
return this.interactions;
34-
}
35-
36-
@Override
37-
public InteractionAction add() {
38-
this.interactions = new CopyOnWriteArrayList<Interaction>(new ArrayList<Interaction>());
39-
return this;
40-
}
41-
42-
@Override
43-
public InteractionAction welcome(Interaction welcomeInteraction) {
44-
welcomeInteraction.setType(InteractionType.welcome);
45-
this.interactions.add(welcomeInteraction);
46-
return this;
47-
}
48-
49-
@Override
50-
public InteractionAction fallback(Interaction fallbackInteraction) {
51-
fallbackInteraction.setType(InteractionType.fallback);
52-
this.interactions.add(fallbackInteraction);
53-
return this;
54-
}
55-
56-
@Override
57-
public InteractionAction interaction(Interaction userInteraction) {
58-
userInteraction.setType(InteractionType.user);
59-
this.interactions.add(userInteraction);
60-
return this;
61-
}
62-
63-
@Override
64-
public Bot build() {
65-
66-
this.interactions.stream()
67-
.forEach(interaction -> {
68-
log.info("Creating Entities...");
69-
70-
if(interaction.getEntities() != null && interaction.getEntities().size() > 0){
71-
interaction.getEntities().forEach(entity -> {
72-
Engine.createEntity(entity, this.token);
73-
});
74-
}
75-
//CREATE INTERACTION
76-
log.info("Creating Interaction -> {}", interaction);
77-
Engine.creatInteraction(this.bot.getStory(), interaction, this.token);
78-
});
79-
80-
return this.bot;
81-
}
82-
83-
@Override
84-
public String getActionName() {
85-
return ACTION_NAME;
86-
}
87-
88-
@Override
89-
public int getOrderExecution() {
90-
return 0;
91-
}
92-
93-
public Token getToken() {
94-
return token;
95-
}
96-
97-
98-
1+
package io.notifye.botengine.client.action.controller;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Objects;
6+
import java.util.concurrent.CopyOnWriteArrayList;
7+
8+
import io.notifye.botengine.client.Engine;
9+
import io.notifye.botengine.client.Token;
10+
import io.notifye.botengine.client.action.InteractionAction;
11+
import io.notifye.botengine.client.bots.Bot;
12+
import io.notifye.botengine.client.model.Interaction;
13+
import io.notifye.botengine.client.model.enums.InteractionType;
14+
import lombok.Data;
15+
import lombok.RequiredArgsConstructor;
16+
import lombok.extern.slf4j.Slf4j;
17+
18+
@Slf4j
19+
@RequiredArgsConstructor
20+
public final @Data class InteractionsActionController implements InteractionAction {
21+
private static final long serialVersionUID = 1L;
22+
private final String ACTION_NAME = InteractionAction.class.getSimpleName();
23+
24+
private final Bot bot;
25+
private final Token token;
26+
private List<Interaction> interactions;
27+
28+
public List<Interaction> getInteractions(){
29+
return this.interactions;
30+
}
31+
32+
@Override
33+
public InteractionAction add() {
34+
this.interactions = new CopyOnWriteArrayList<Interaction>(new ArrayList<Interaction>());
35+
return this;
36+
}
37+
38+
@Override
39+
public InteractionAction welcome(Interaction welcomeInteraction) {
40+
welcomeInteraction.setType(InteractionType.welcome);
41+
this.interactions.add(welcomeInteraction);
42+
return this;
43+
}
44+
45+
@Override
46+
public InteractionAction fallback(Interaction fallbackInteraction) {
47+
fallbackInteraction.setType(InteractionType.fallback);
48+
this.interactions.add(fallbackInteraction);
49+
return this;
50+
}
51+
52+
@Override
53+
public InteractionAction interaction(Interaction userInteraction) {
54+
userInteraction.setType(InteractionType.user);
55+
this.interactions.add(userInteraction);
56+
return this;
57+
}
58+
59+
@Override
60+
public Bot build() {
61+
Objects.requireNonNull(this.interactions, "Please necessary add interactions");
62+
this.interactions.stream()
63+
.forEach(interaction -> {
64+
65+
log.info("Creating Entities...");
66+
if(interaction.getEntities() != null && interaction.getEntities().size() > 0){
67+
interaction.getEntities().forEach(entity -> {
68+
Engine.createEntity(entity, this.token);
69+
});
70+
}
71+
//CREATE INTERACTION
72+
log.info("Creating Interaction -> {}", interaction);
73+
Engine.creatInteraction(this.bot.getStory(), interaction, this.token);
74+
});
75+
76+
return this.bot;
77+
}
78+
79+
@Override
80+
public String getActionName() {
81+
return ACTION_NAME;
82+
}
83+
84+
@Override
85+
public int getOrderExecution() {
86+
return 0;
87+
}
88+
89+
public Token getToken() {
90+
return token;
91+
}
92+
9993
}

src/main/java/io/notifye/botengine/client/action/controller/QueryController.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
import io.notifye.botengine.client.model.Query;
77
import io.notifye.botengine.client.model.QueryResponse;
88
import io.notifye.botengine.client.model.Story;
9-
import lombok.AllArgsConstructor;
109
import lombok.Data;
10+
import lombok.RequiredArgsConstructor;
1111

12-
@AllArgsConstructor
13-
public @Data class QueryController implements QueryAction {
12+
@RequiredArgsConstructor
13+
public final @Data class QueryController implements QueryAction {
1414

15-
private Story story;
16-
private Token token;
17-
private String session;
15+
private final Story story;
16+
private final Token token;
17+
private final String session;
1818

1919
@Override
2020
public QueryResponse q(String query) {
@@ -23,9 +23,6 @@ public QueryResponse q(String query) {
2323

2424
@Override
2525
public QueryResponse q(Story story, String query) {
26-
if(this.story == null){
27-
this.story = story;
28-
}
2926
return Engine.query(story, query, token, session);
3027
}
3128

@@ -36,9 +33,6 @@ public QueryResponse q(Query query) {
3633

3734
@Override
3835
public QueryResponse q(Story story, Query query) {
39-
if(this.story == null){
40-
this.story = story;
41-
}
4236
return Engine.query(story, query, token, session);
4337
}
4438

0 commit comments

Comments
 (0)