-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathVideoMetadataCommand.java
85 lines (67 loc) · 3.02 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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.lab.gateway.loadbalancer.DiscoveryAndLoadBalancer;
import io.reactivex.netty.protocol.http.client.HttpClientRequest;
import io.reactivex.netty.protocol.http.sse.ServerSentEvent;
import netflix.ocelli.LoadBalancer;
import netflix.ocelli.rxnetty.HttpClientHolder;
import rx.Observable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class VideoMetadataCommand extends HystrixObservableCommand<VideoMetadata> {
private final List<Video> videos;
private static final LoadBalancer<HttpClientHolder<ByteBuf, ServerSentEvent>> loadBalancer =
DiscoveryAndLoadBalancer.getFactory().forVip("reactive-lab-vms-service");
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> construct() {
HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/metadata?" + UrlGenerator.generate("videoId",
videos));
return loadBalancer.choose()
.map(holder -> holder.getClient())
.<VideoMetadata>flatMap(client -> client.submit(request)
.flatMap(r -> r.getContent()
.map((ServerSentEvent sse) -> VideoMetadata.fromJson(sse.contentAsString()))))
.retry(1);
}
@Override
protected Observable<VideoMetadata> resumeWithFallback() {
Map<String, Object> video = new HashMap<>();
video.put("videoId", videos.get(0).getId());
video.put("title", "Fallback Video Title");
video.put("other_data", "goes_here");
return Observable.just(new VideoMetadata(video));
}
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;
}
}
}