| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package speech |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"time" |
| 22 |
|
| 23 |
"cloud.google.com/go/longrunning" |
| 24 |
longrunningpb "cloud.google.com/go/longrunning/autogen/longrunningpb" |
| 25 |
speechpb "cloud.google.com/go/speech/apiv1p1beta1/speechpb" |
| 26 |
gax "github.com/googleapis/gax-go/v2" |
| 27 |
"google.golang.org/api/iterator" |
| 28 |
) |
| 29 |
|
| 30 |
|
| 31 |
type LongRunningRecognizeOperation struct { |
| 32 |
lro *longrunning.Operation |
| 33 |
pollPath string |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
func (op *LongRunningRecognizeOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*speechpb.LongRunningRecognizeResponse, error) { |
| 40 |
opts = append([]gax.CallOption{gax.WithPath(op.pollPath)}, opts...) |
| 41 |
var resp speechpb.LongRunningRecognizeResponse |
| 42 |
if err := op.lro.WaitWithInterval(ctx, &resp, time.Minute, opts...); err != nil { |
| 43 |
return nil, err |
| 44 |
} |
| 45 |
return &resp, nil |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
func (op *LongRunningRecognizeOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*speechpb.LongRunningRecognizeResponse, error) { |
| 58 |
opts = append([]gax.CallOption{gax.WithPath(op.pollPath)}, opts...) |
| 59 |
var resp speechpb.LongRunningRecognizeResponse |
| 60 |
if err := op.lro.Poll(ctx, &resp, opts...); err != nil { |
| 61 |
return nil, err |
| 62 |
} |
| 63 |
if !op.Done() { |
| 64 |
return nil, nil |
| 65 |
} |
| 66 |
return &resp, nil |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
func (op *LongRunningRecognizeOperation) Metadata() (*speechpb.LongRunningRecognizeMetadata, error) { |
| 74 |
var meta speechpb.LongRunningRecognizeMetadata |
| 75 |
if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata { |
| 76 |
return nil, nil |
| 77 |
} else if err != nil { |
| 78 |
return nil, err |
| 79 |
} |
| 80 |
return &meta, nil |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
func (op *LongRunningRecognizeOperation) Done() bool { |
| 85 |
return op.lro.Done() |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
func (op *LongRunningRecognizeOperation) Name() string { |
| 91 |
return op.lro.Name() |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
type CustomClassIterator struct { |
| 96 |
items []*speechpb.CustomClass |
| 97 |
pageInfo *iterator.PageInfo |
| 98 |
nextFunc func() error |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
Response interface{} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
InternalFetch func(pageSize int, pageToken string) (results []*speechpb.CustomClass, nextPageToken string, err error) |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
func (it *CustomClassIterator) PageInfo() *iterator.PageInfo { |
| 116 |
return it.pageInfo |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
func (it *CustomClassIterator) Next() (*speechpb.CustomClass, error) { |
| 122 |
var item *speechpb.CustomClass |
| 123 |
if err := it.nextFunc(); err != nil { |
| 124 |
return item, err |
| 125 |
} |
| 126 |
item = it.items[0] |
| 127 |
it.items = it.items[1:] |
| 128 |
return item, nil |
| 129 |
} |
| 130 |
|
| 131 |
func (it *CustomClassIterator) bufLen() int { |
| 132 |
return len(it.items) |
| 133 |
} |
| 134 |
|
| 135 |
func (it *CustomClassIterator) takeBuf() interface{} { |
| 136 |
b := it.items |
| 137 |
it.items = nil |
| 138 |
return b |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
type OperationIterator struct { |
| 143 |
items []*longrunningpb.Operation |
| 144 |
pageInfo *iterator.PageInfo |
| 145 |
nextFunc func() error |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
Response interface{} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
InternalFetch func(pageSize int, pageToken string) (results []*longrunningpb.Operation, nextPageToken string, err error) |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
func (it *OperationIterator) PageInfo() *iterator.PageInfo { |
| 163 |
return it.pageInfo |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
func (it *OperationIterator) Next() (*longrunningpb.Operation, error) { |
| 169 |
var item *longrunningpb.Operation |
| 170 |
if err := it.nextFunc(); err != nil { |
| 171 |
return item, err |
| 172 |
} |
| 173 |
item = it.items[0] |
| 174 |
it.items = it.items[1:] |
| 175 |
return item, nil |
| 176 |
} |
| 177 |
|
| 178 |
func (it *OperationIterator) bufLen() int { |
| 179 |
return len(it.items) |
| 180 |
} |
| 181 |
|
| 182 |
func (it *OperationIterator) takeBuf() interface{} { |
| 183 |
b := it.items |
| 184 |
it.items = nil |
| 185 |
return b |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
type PhraseSetIterator struct { |
| 190 |
items []*speechpb.PhraseSet |
| 191 |
pageInfo *iterator.PageInfo |
| 192 |
nextFunc func() error |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
Response interface{} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
InternalFetch func(pageSize int, pageToken string) (results []*speechpb.PhraseSet, nextPageToken string, err error) |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
func (it *PhraseSetIterator) PageInfo() *iterator.PageInfo { |
| 210 |
return it.pageInfo |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
func (it *PhraseSetIterator) Next() (*speechpb.PhraseSet, error) { |
| 216 |
var item *speechpb.PhraseSet |
| 217 |
if err := it.nextFunc(); err != nil { |
| 218 |
return item, err |
| 219 |
} |
| 220 |
item = it.items[0] |
| 221 |
it.items = it.items[1:] |
| 222 |
return item, nil |
| 223 |
} |
| 224 |
|
| 225 |
func (it *PhraseSetIterator) bufLen() int { |
| 226 |
return len(it.items) |
| 227 |
} |
| 228 |
|
| 229 |
func (it *PhraseSetIterator) takeBuf() interface{} { |
| 230 |
b := it.items |
| 231 |
it.items = nil |
| 232 |
return b |
| 233 |
} |