Skip to content

Commit

Permalink
videointelligence/apiv2: add client
Browse files Browse the repository at this point in the history
Depends on googleapis/go-genproto#38

Also document that apiv2 is under whitelist.

The package docs of both v1 and v2 previously pointed the users
to a handwritten client which does not exist.
These docs are removed.
The SetGoogleClientInfo methods, previously exported, is now
unexported. They are supposed to be used only by handwritten
clients anyway.

Change-Id: I741461e635c849dc515b2d10c2d04d7d10e70483
Reviewed-on: https://code-review.googlesource.com/16270
Reviewed-by: kokoro <[email protected]>
Reviewed-by: Chris Broadfoot <[email protected]>
Reviewed-by: Jonathan Amsterdam <[email protected]>
  • Loading branch information
pongad committed Sep 7, 2017
1 parent 55e88ef commit 8469772
Show file tree
Hide file tree
Showing 7 changed files with 529 additions and 5 deletions.
2 changes: 0 additions & 2 deletions videointelligence/apiv1beta1/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
// Google Cloud Video Intelligence API.
//
// Google Cloud Video Intelligence API.
//
// Use the client at cloud.google.com/go/videointelligence in preference to this.
package videointelligence // import "cloud.google.com/go/videointelligence/apiv1beta1"

import (
Expand Down
6 changes: 3 additions & 3 deletions videointelligence/apiv1beta1/video_intelligence_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error

client: videointelligencepb.NewVideoIntelligenceServiceClient(conn),
}
c.SetGoogleClientInfo()
c.setGoogleClientInfo()

c.LROClient, err = lroauto.NewOperationsClient(ctx, option.WithGRPCConn(conn))
if err != nil {
Expand All @@ -124,10 +124,10 @@ func (c *Client) Close() error {
return c.conn.Close()
}

// SetGoogleClientInfo sets the name and version of the application in
// setGoogleClientInfo sets the name and version of the application in
// the `x-goog-api-client` header passed on each request. Intended for
// use by Google-written clients.
func (c *Client) SetGoogleClientInfo(keyval ...string) {
func (c *Client) setGoogleClientInfo(keyval ...string) {
kv := append([]string{"gl-go", version.Go()}, keyval...)
kv = append(kv, "gapic", version.Repo, "gax", gax.Version, "grpc", grpc.Version)
c.xGoogHeader = []string{gax.XGoogHeader(kv...)}
Expand Down
40 changes: 40 additions & 0 deletions videointelligence/apiv1beta2/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2017, Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// AUTO-GENERATED CODE. DO NOT EDIT.

// Package videointelligence is an experimental, auto-generated package for the
// Google Cloud Video Intelligence API.
//
// Google Cloud Video Intelligence API.
package videointelligence // import "cloud.google.com/go/videointelligence/apiv1beta2"

import (
"golang.org/x/net/context"
"google.golang.org/grpc/metadata"
)

func insertXGoog(ctx context.Context, val []string) context.Context {
md, _ := metadata.FromOutgoingContext(ctx)
md = md.Copy()
md["x-goog-api-client"] = val
return metadata.NewOutgoingContext(ctx, md)
}

// DefaultAuthScopes reports the default set of authentication scopes to use with this package.
func DefaultAuthScopes() []string {
return []string{
"https://www.googleapis.com/auth/cloud-platform",
}
}
190 changes: 190 additions & 0 deletions videointelligence/apiv1beta2/mock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
// Copyright 2017, Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// AUTO-GENERATED CODE. DO NOT EDIT.

package videointelligence

import (
videointelligencepb "google.golang.org/genproto/googleapis/cloud/videointelligence/v1beta2"
longrunningpb "google.golang.org/genproto/googleapis/longrunning"
)

import (
"flag"
"fmt"
"io"
"log"
"net"
"os"
"strings"
"testing"

"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"golang.org/x/net/context"
"google.golang.org/api/option"
status "google.golang.org/genproto/googleapis/rpc/status"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
gstatus "google.golang.org/grpc/status"
)

var _ = io.EOF
var _ = ptypes.MarshalAny
var _ status.Status

type mockVideoIntelligenceServer struct {
// Embed for forward compatibility.
// Tests will keep working if more methods are added
// in the future.
videointelligencepb.VideoIntelligenceServiceServer

reqs []proto.Message

// If set, all calls return this error.
err error

// responses to return if err == nil
resps []proto.Message
}

func (s *mockVideoIntelligenceServer) AnnotateVideo(ctx context.Context, req *videointelligencepb.AnnotateVideoRequest) (*longrunningpb.Operation, error) {
md, _ := metadata.FromIncomingContext(ctx)
if xg := md["x-goog-api-client"]; len(xg) == 0 || !strings.Contains(xg[0], "gl-go/") {
return nil, fmt.Errorf("x-goog-api-client = %v, expected gl-go key", xg)
}
s.reqs = append(s.reqs, req)
if s.err != nil {
return nil, s.err
}
return s.resps[0].(*longrunningpb.Operation), nil
}

// clientOpt is the option tests should use to connect to the test server.
// It is initialized by TestMain.
var clientOpt option.ClientOption

var (
mockVideoIntelligence mockVideoIntelligenceServer
)

func TestMain(m *testing.M) {
flag.Parse()

serv := grpc.NewServer()
videointelligencepb.RegisterVideoIntelligenceServiceServer(serv, &mockVideoIntelligence)

lis, err := net.Listen("tcp", "localhost:0")
if err != nil {
log.Fatal(err)
}
go serv.Serve(lis)

conn, err := grpc.Dial(lis.Addr().String(), grpc.WithInsecure())
if err != nil {
log.Fatal(err)
}
clientOpt = option.WithGRPCConn(conn)

os.Exit(m.Run())
}

func TestVideoIntelligenceServiceAnnotateVideo(t *testing.T) {
var expectedResponse *videointelligencepb.AnnotateVideoResponse = &videointelligencepb.AnnotateVideoResponse{}

mockVideoIntelligence.err = nil
mockVideoIntelligence.reqs = nil

any, err := ptypes.MarshalAny(expectedResponse)
if err != nil {
t.Fatal(err)
}
mockVideoIntelligence.resps = append(mockVideoIntelligence.resps[:0], &longrunningpb.Operation{
Name: "longrunning-test",
Done: true,
Result: &longrunningpb.Operation_Response{Response: any},
})

var inputUri string = "inputUri1707300727"
var features []videointelligencepb.Feature = nil
var request = &videointelligencepb.AnnotateVideoRequest{
InputUri: inputUri,
Features: features,
}

c, err := NewClient(context.Background(), clientOpt)
if err != nil {
t.Fatal(err)
}

respLRO, err := c.AnnotateVideo(context.Background(), request)
if err != nil {
t.Fatal(err)
}
resp, err := respLRO.Wait(context.Background())

if err != nil {
t.Fatal(err)
}

if want, got := request, mockVideoIntelligence.reqs[0]; !proto.Equal(want, got) {
t.Errorf("wrong request %q, want %q", got, want)
}

if want, got := expectedResponse, resp; !proto.Equal(want, got) {
t.Errorf("wrong response %q, want %q)", got, want)
}
}

func TestVideoIntelligenceServiceAnnotateVideoError(t *testing.T) {
errCode := codes.PermissionDenied
mockVideoIntelligence.err = nil
mockVideoIntelligence.resps = append(mockVideoIntelligence.resps[:0], &longrunningpb.Operation{
Name: "longrunning-test",
Done: true,
Result: &longrunningpb.Operation_Error{
Error: &status.Status{
Code: int32(errCode),
Message: "test error",
},
},
})

var inputUri string = "inputUri1707300727"
var features []videointelligencepb.Feature = nil
var request = &videointelligencepb.AnnotateVideoRequest{
InputUri: inputUri,
Features: features,
}

c, err := NewClient(context.Background(), clientOpt)
if err != nil {
t.Fatal(err)
}

respLRO, err := c.AnnotateVideo(context.Background(), request)
if err != nil {
t.Fatal(err)
}
resp, err := respLRO.Wait(context.Background())

if st, ok := gstatus.FromError(err); !ok {
t.Errorf("got error %v, expected grpc error", err)
} else if c := st.Code(); c != errCode {
t.Errorf("got error code %q, want %q", c, errCode)
}
_ = resp
}
Loading

0 comments on commit 8469772

Please sign in to comment.