|
33 | 33 | import com.google.api.core.InternalApi;
|
34 | 34 | import com.google.api.gax.longrunning.OperationSnapshot;
|
35 | 35 | import com.google.api.gax.rpc.StatusCode;
|
| 36 | +import com.google.api.gax.rpc.StatusCode.Code; |
| 37 | +import com.google.longrunning.Operation; |
36 | 38 |
|
37 | 39 | /**
|
38 | 40 | * Implementation of OperationSnapshot based on REST transport.
|
|
42 | 44 | @BetaApi("The surface for long-running operations is not stable yet and may change in the future.")
|
43 | 45 | @InternalApi
|
44 | 46 | public class HttpJsonOperationSnapshot implements OperationSnapshot {
|
45 |
| - |
46 | 47 | private final String name;
|
47 | 48 | private final Object metadata;
|
48 | 49 | private final boolean done;
|
49 | 50 | private final Object response;
|
50 | 51 | private final StatusCode errorCode;
|
51 | 52 | private final String errorMessage;
|
52 | 53 |
|
53 |
| -public HttpJsonOperationSnapshot( |
| 54 | +private HttpJsonOperationSnapshot( |
54 | 55 | String name,
|
55 | 56 | Object metadata,
|
56 | 57 | boolean done,
|
57 | 58 | Object response,
|
58 |
| -int errorCode, |
| 59 | +StatusCode errorCode, |
59 | 60 | String errorMessage) {
|
60 | 61 | this.name = name;
|
61 | 62 | this.metadata = metadata;
|
62 | 63 | this.done = done;
|
63 |
| -this.response = done ? response : null; |
64 |
| -if (done && errorCode != 0) { |
65 |
| -this.errorCode = HttpJsonStatusCode.of(errorCode, errorMessage); |
66 |
| -this.errorMessage = errorMessage; |
67 |
| -} else { |
68 |
| -this.errorCode = null; |
69 |
| -this.errorMessage = null; |
70 |
| -} |
| 64 | +this.response = response; |
| 65 | +this.errorCode = errorCode; |
| 66 | +this.errorMessage = errorMessage; |
71 | 67 | }
|
72 | 68 |
|
73 | 69 | /** {@inheritDoc} */
|
@@ -106,6 +102,10 @@ public String getErrorMessage() {
|
106 | 102 | return this.errorMessage;
|
107 | 103 | }
|
108 | 104 |
|
| 105 | +public static HttpJsonOperationSnapshot create(Operation operation) { |
| 106 | +return newBuilder().setOperation(operation).build(); |
| 107 | +} |
| 108 | + |
109 | 109 | public static Builder newBuilder() {
|
110 | 110 | return new HttpJsonOperationSnapshot.Builder();
|
111 | 111 | }
|
@@ -115,7 +115,7 @@ public static class Builder {
|
115 | 115 | private Object metadata;
|
116 | 116 | private boolean done;
|
117 | 117 | private Object response;
|
118 |
| -private int errorCode; |
| 118 | +private StatusCode errorCode; |
119 | 119 | private String errorMessage;
|
120 | 120 |
|
121 | 121 | public Builder setName(String name) {
|
@@ -139,11 +139,24 @@ public Builder setResponse(Object response) {
|
139 | 139 | }
|
140 | 140 |
|
141 | 141 | public Builder setError(int errorCode, String errorMessage) {
|
142 |
| -this.errorCode = errorCode; |
| 142 | +this.errorCode = |
| 143 | +HttpJsonStatusCode.of( |
| 144 | +errorCode == 0 ? Code.OK.getHttpStatusCode() : errorCode, errorMessage); |
143 | 145 | this.errorMessage = errorMessage;
|
144 | 146 | return this;
|
145 | 147 | }
|
146 | 148 |
|
| 149 | +private Builder setOperation(Operation operation) { |
| 150 | +this.name = operation.getName(); |
| 151 | +this.done = operation.getDone(); |
| 152 | +this.response = operation.getResponse(); |
| 153 | +this.metadata = operation.getMetadata(); |
| 154 | +this.errorCode = |
| 155 | +HttpJsonStatusCode.of(com.google.rpc.Code.forNumber(operation.getError().getCode())); |
| 156 | +this.errorMessage = operation.getError().getMessage(); |
| 157 | +return this; |
| 158 | +} |
| 159 | + |
147 | 160 | public HttpJsonOperationSnapshot build() {
|
148 | 161 | return new HttpJsonOperationSnapshot(name, metadata, done, response, errorCode, errorMessage);
|
149 | 162 | }
|
|
0 commit comments