1 package io.getunleash.repository;
2
3 import io.getunleash.FeatureToggle;
4 import io.getunleash.UnleashException;
5 import io.getunleash.event.UnleashEvent;
6 import io.getunleash.event.UnleashSubscriber;
7 import io.getunleash.lang.Nullable;
8 import java.util.Collections;
9 import java.util.List;
10
11 public class FeatureToggleResponse implements UnleashEvent {
12
13 public enum Status {
14 NOT_CHANGED,
15 CHANGED,
16 UNAVAILABLE,
17 }
18
19 private final Status status;
20 private final int httpStatusCode;
21 private final ToggleCollection toggleCollection;
22 @Nullable private String location;
23
24 public FeatureToggleResponse(Status status, ToggleCollection toggleCollection) {
25 this.status = status;
26 this.httpStatusCode = 200;
27 this.toggleCollection = toggleCollection;
28 }
29
30 public FeatureToggleResponse(Status status, int httpStatusCode) {
31 this.status = status;
32 this.httpStatusCode = httpStatusCode;
33 List<FeatureToggle> emptyList = Collections.emptyList();
34 this.toggleCollection = new ToggleCollection(emptyList);
35 }
36
37 public FeatureToggleResponse(Status status, int httpStatusCode, @Nullable String location) {
38 this(status, httpStatusCode);
39 this.location = location;
40 }
41
42 public Status getStatus() {
43 return status;
44 }
45
46 public ToggleCollection getToggleCollection() {
47 return toggleCollection;
48 }
49
50 public int getHttpStatusCode() {
51 return httpStatusCode;
52 }
53
54 public @Nullable String getLocation() {
55 return location;
56 }
57
58 @Override
59 public String toString() {
60 return "FeatureToggleResponse:"
61 + " status="
62 + status
63 + " httpStatus="
64 + httpStatusCode
65 + " location="
66 + location;
67 }
68
69 @Override
70 public void publishTo(UnleashSubscriber unleashSubscriber) {
71 if (status == FeatureToggleResponse.Status.UNAVAILABLE) {
72 String msg =
73 "Error fetching toggles from Unleash API - StatusCode: " + getHttpStatusCode();
74 if (location != null) {
75 msg += ", Location: " + location;
76 }
77 unleashSubscriber.onError(new UnleashException(msg, null));
78 }
79
80 unleashSubscriber.togglesFetched(this);
81 }
82 }
83