3333import com .google .api .core .InternalApi ;
3434import com .google .api .gax .longrunning .OperationSnapshot ;
3535import com .google .api .gax .rpc .StatusCode ;
36+ import com .google .api .gax .rpc .StatusCode .Code ;
37+ import com .google .longrunning .Operation ;
3638
3739/**
3840 * Implementation of OperationSnapshot based on REST transport.
4244@ BetaApi ("The surface for long-running operations is not stable yet and may change in the future." )
4345@ InternalApi
4446public class HttpJsonOperationSnapshot implements OperationSnapshot {
45-
4647 private final String name ;
4748 private final Object metadata ;
4849 private final boolean done ;
4950 private final Object response ;
5051 private final StatusCode errorCode ;
5152 private final String errorMessage ;
5253
53- public HttpJsonOperationSnapshot (
54+ private HttpJsonOperationSnapshot (
5455 String name ,
5556 Object metadata ,
5657 boolean done ,
5758 Object response ,
58- int errorCode ,
59+ StatusCode errorCode ,
5960 String errorMessage ) {
6061 this .name = name ;
6162 this .metadata = metadata ;
6263 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 ;
7167 }
7268
7369 /** {@inheritDoc} */
@@ -106,6 +102,10 @@ public String getErrorMessage() {
106102 return this .errorMessage ;
107103 }
108104
105+ public static HttpJsonOperationSnapshot create (Operation operation ) {
106+ return newBuilder ().setOperation (operation ).build ();
107+ }
108+
109109 public static Builder newBuilder () {
110110 return new HttpJsonOperationSnapshot .Builder ();
111111 }
@@ -115,7 +115,7 @@ public static class Builder {
115115 private Object metadata ;
116116 private boolean done ;
117117 private Object response ;
118- private int errorCode ;
118+ private StatusCode errorCode ;
119119 private String errorMessage ;
120120
121121 public Builder setName (String name ) {
@@ -139,11 +139,24 @@ public Builder setResponse(Object response) {
139139 }
140140
141141 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 );
143145 this .errorMessage = errorMessage ;
144146 return this ;
145147 }
146148
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+
147160 public HttpJsonOperationSnapshot build () {
148161 return new HttpJsonOperationSnapshot (name , metadata , done , response , errorCode , errorMessage );
149162 }
0 commit comments