-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathVideoMetadataCommand.java
69 lines (53 loc) · 2.16 KB
/
VideoMetadataCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package io.reactivex.lab.gateway.clients;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixObservableCommand;
import io.netty.buffer.ByteBuf;
import io.reactivex.lab.gateway.clients.PersonalizedCatalogCommand.Video;
import io.reactivex.lab.gateway.clients.VideoMetadataCommand.VideoMetadata;
import io.reactivex.lab.gateway.common.SimpleJson;
import io.reactivex.netty.RxNetty;
import io.reactivex.netty.pipeline.PipelineConfigurators;
import io.reactivex.netty.protocol.http.client.HttpClientRequest;
import rx.Observable;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
public class VideoMetadataCommand extends HystrixObservableCommand<VideoMetadata> {
private final List<Video> videos;
public VideoMetadataCommand(Video video) {
this(Arrays.asList(video));
// replace with HystrixCollapser
}
public VideoMetadataCommand(List<Video> videos) {
super(HystrixCommandGroupKey.Factory.asKey("VideoMetadata"));
this.videos = videos;
}
@Override
protected Observable<VideoMetadata> run() {
return RxNetty.createHttpClient("localhost", 9196, PipelineConfigurators.<ByteBuf> clientSseConfigurator())
.submit(HttpClientRequest.createGet("/metadata?" + UrlGenerator.generate("videoId", videos)))
.flatMap(r -> r.getContent().map(sse -> {
String videometa = sse.contentAsString();
System.out.println("videometa = " + videometa);
return VideoMetadata.fromJson(videometa);
}));
}
public static class VideoMetadata {
private final Map<String, Object> data;
public VideoMetadata(Map<String, Object> data) {
this.data = data;
}
public static VideoMetadata fromJson(String json) {
return new VideoMetadata(SimpleJson.jsonToMap(json));
}
public int getId() {
return (int) data.get("videoId");
}
public String getTitle() {
return (String) data.get("title");
}
public Map<String, Object> getDataAsMap() {
return data;
}
}
}