|
| 1 | +// Copyright 2019 Google Inc. All rights reserved |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#ifndef GAPIC_GENERATOR_CPP_GAX_OPERATION_H_ |
| 16 | +#define GAPIC_GENERATOR_CPP_GAX_OPERATION_H_ |
| 17 | + |
| 18 | +#include "google/longrunning/operations.pb.h" |
| 19 | +#include "gax/call_context.h" |
| 20 | +#include "gax/operations_stub.h" |
| 21 | +#include "gax/status.h" |
| 22 | +#include "gax/status_or.h" |
| 23 | +#include <memory> |
| 24 | + |
| 25 | +namespace google { |
| 26 | +namespace gax { |
| 27 | + |
| 28 | +/** |
| 29 | + * Operation is a user-visible primitive used to support Long Running Operations |
| 30 | + * (LROs). |
| 31 | + * |
| 32 | + * LROs are service methods that may take a 'long time' (anywhere from |
| 33 | + * seconds to days), and instead of blocking for that long return an Operation |
| 34 | + * object or an error status. The Operation may contain a result and always |
| 35 | + * contains metadata. |
| 36 | + * |
| 37 | + * A service method that returns `google.longrunning.Operation` and that has |
| 38 | + * the correct annotations for its result and metadata types will cause the |
| 39 | + * generator to define a client method that returns |
| 40 | + * gax::StatusOr<gax::Operation<ResponseT, MetadataT>>. |
| 41 | + * |
| 42 | + * Example usage: |
| 43 | + * |
| 44 | + * @code |
| 45 | + * gax::StatusOr<gax::Operation<Foo, FooMeta>> res = |
| 46 | + * client.GetBigFoo(getBigFooRequest); |
| 47 | + * if(!res) { |
| 48 | + * ... |
| 49 | + * } |
| 50 | + * |
| 51 | + * operationsClient = client.OperationsClient(); |
| 52 | + * Operation<Foo, FooMeta> op = *std::move(res); |
| 53 | + * while(!op.Done()) { |
| 54 | + * gax::Status stat = operationsClient.Update(op); |
| 55 | + * if(isPermanentFailure(stat)) { |
| 56 | + * ... |
| 57 | + * } |
| 58 | + * std::cout << "Foo request " << getBigFooRequest().name() << " is " << |
| 59 | + * op.Metadata().percent() << "% done."; |
| 60 | + * std::this_thread::sleep(sleep_period()); |
| 61 | + * } |
| 62 | + * |
| 63 | + * gax::StatusOr<Result> terminus = op.Result(); |
| 64 | + * if(!terminus) { |
| 65 | + * ... |
| 66 | + * } |
| 67 | + * Result r = *std::move(terminus); |
| 68 | + * @endcode |
| 69 | + */ |
| 70 | +template < |
| 71 | + typename ResponseT, typename MetadataT, |
| 72 | + typename std::enable_if< |
| 73 | + std::is_base_of<protobuf::Message, ResponseT>::value, int>::type = 0, |
| 74 | + typename std::enable_if< |
| 75 | + std::is_base_of<protobuf::Message, MetadataT>::value, int>::type = 0> |
| 76 | +class Operation final { |
| 77 | + public: |
| 78 | + // Note: the constructor is intended to be used by GAPIC generated code, not |
| 79 | + // users. |
| 80 | + explicit Operation(google::longrunning::Operation op) : op_(std::move(op)) {} |
| 81 | + |
| 82 | + /** |
| 83 | + * @brief Return the service-provided name of the underlying |
| 84 | + * google.longrunning.Operation |
| 85 | + */ |
| 86 | + std::string const& Name() const { return op_.name(); } |
| 87 | + |
| 88 | + /** |
| 89 | + * @brief If Operation::Done(), return the underlying Response, or an error |
| 90 | + * code if a problem occurred. Otherwise return an error indicating that the |
| 91 | + * Operation has not completed. |
| 92 | + */ |
| 93 | + gax::StatusOr<ResponseT> Result() const { |
| 94 | + if (!Done()) { |
| 95 | + return gax::Status{gax::StatusCode::kUnknown, |
| 96 | + "operation has not completed=" + Name()}; |
| 97 | + } else if (op_.has_error()) { |
| 98 | + return gax::Status{static_cast<gax::StatusCode>(op_.error().code()), |
| 99 | + op_.error().message()}; |
| 100 | + } else { |
| 101 | + auto const& any = op_.response(); |
| 102 | + if (!any.Is<ResponseT>()) { |
| 103 | + return gax::Status{gax::StatusCode::kUnknown, |
| 104 | + "invalid result in operation=" + Name()}; |
| 105 | + } |
| 106 | + |
| 107 | + ResponseT result; |
| 108 | + any.UnpackTo(&result); |
| 109 | + return std::move(result); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * @brief Return the most recent metadata received from the service. |
| 115 | + * |
| 116 | + * The metadata type is application specific. Manipulating it is left to the |
| 117 | + * user. |
| 118 | + * |
| 119 | + * @return the most recent metadata. |
| 120 | + */ |
| 121 | + MetadataT Metadata() const { |
| 122 | + MetadataT m; |
| 123 | + op_.metadata().UnpackTo(&m); |
| 124 | + return m; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * @brief Indicate whether the operation has completed. If true, the Operation |
| 129 | + * now contains a result or an error status. |
| 130 | + */ |
| 131 | + bool Done() const { return op_.done(); } |
| 132 | + |
| 133 | + private: |
| 134 | + google::longrunning::Operation op_; |
| 135 | +}; |
| 136 | + |
| 137 | +} // namespace gax |
| 138 | +} // namespace google |
| 139 | + |
| 140 | +#endif // GAPIC_GENERATOR_CPP_GAX_OPERATION_H_ |
0 commit comments