google

package
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 11, 2023 License: MIT Imports: 9 Imported by: 4

README

easyindex for Google indexing API

Required

  • Google Indexing API
    • Google service account
    • credential json file for Google Indexing API
  • Go +1.18

Install

go get github.com/usk81/easyindex

Example

Publish API
package main

import (
    "fmt"

	"github.com/usk81/easyindex"
    "github.com/usk81/easyindex/google"
    "github.com/usk81/easyindex/logger"
	"google.golang.org/api/indexing/v3"
)

func main() {
    cf := "./credential.json"
    l, err := logger.New("debug")
    if err != nil {
        panic(err)
    }
    publish, err := google.Publish(goole.Config{
		Quota: google.PublishAPIDefaultQuota,
        CredentialsFile: &cf,
        Logger:          l,
        Logging:         true,
    })
    if err != nil {
        panic(err)
    }
    rs := []google.PublishRequest{
        {
            URL:              "http://example.com/foo",
            NotificationType: google.NotificationTypeUpdated,
        },
        {
            URL:              "http://example.com/bar",
            NotificationType: google.NotificationTypeDeleted,
        },
    }

    // precheck: Skip if the web page returns a status code other than 2xx.
	rs, skips, err := publish.Precheck(rs)
	if err != nil {
		panic(err)
	}

	resps := []*indexing.PublishUrlNotificationResponse{}
	for _, r := range rs {
		resp, skip, err := publish.Execute(r)
		if skip {
			skips = append(skips, google.SkipedPublishRequest{
				URL: r.URL,
				NotificationType: r.NotificationType,
				Reason: ErrExceededQuota,
			})
		} else if err != nil {
			panic(err)
		} else {
			resps = append(resps, resp)
		}
	}
    fmt.Printf("response: %#v\n", resps)
    fmt.Printf("skipRequests: %#v\n", skips)
}

milestones

  • publish
  • getMetadata

Documentation

Index

Constants

View Source
const (
	// Unspecified ...
	NotificationTypeUnspecified = NotificationType("URL_NOTIFICATION_TYPE_UNSPECIFIED")
	// Updated means that The given URL (Web document) has been updated.
	NotificationTypeUpdated = NotificationType("URL_UPDATED")
	// Deleted means that The given URL (Web document) has been deleted.
	NotificationTypeDeleted = NotificationType("URL_DELETED")
)

The URL life cycle event that Google is being notified about.

View Source
const (
	PublishAPIDefaultQuota = 200
)

Variables

This section is empty.

Functions

This section is empty.

Types

type APIClient

type APIClient interface {
	Publish(u string, nt NotificationType) (result *indexing.PublishUrlNotificationResponse, err error)
}

APIClient is interface of client to request Google Indexing API

func New

func New(ctx context.Context, authOption option.ClientOption) (c APIClient, err error)

New creates new instance to request Google Indexing API

func NewByWithCredentialsFile

func NewByWithCredentialsFile(ctx context.Context, fp string) (c APIClient, err error)

func NewByWithToken

func NewByWithToken(ctx context.Context, token oauth2.TokenSource) (c APIClient, err error)

type Client

type Client struct {
	Service *indexing.Service
}

Client is defined parameters to request Google Indexing API

func (*Client) Publish

func (c *Client) Publish(u string, nt NotificationType) (result *indexing.PublishUrlNotificationResponse, err error)

Execute Google Indexing API

type Config

type Config struct {
	// Quota is Publish API requests limit per day
	Quota *int

	// Context is used to send Google indexing API request
	Context context.Context

	// CredentialsFile is Google APIs credentials file path.
	//   You must set either CredentialsFile or Token.
	//   If both are set, Token will be given priority.
	CredentialsFile *string

	// CredentialsFile is Google APIs credentials file path.
	//   You must set either CredentialsFile or Token.
	//   If both are set, Token will be given priority.
	Token oauth2.TokenSource

	// Crawler is the instance of the client accessing your web page
	Crawler *http.Client

	// Logger is logger instance
	Logger *zap.Logger

	// Logging is the flag if outputs logs
	Logging bool
}

type NotificationType

type NotificationType string

type PublishRequest

type PublishRequest struct {
	URL              string
	NotificationType NotificationType
}

PublishRequest is defined indexing API request

type Publisher

type Publisher interface {
	Precheck(input []PublishRequest) (requests []PublishRequest, skips []SkipedPublishRequest)
	Publish(request PublishRequest) (responses *indexing.PublishUrlNotificationResponse, skip bool, err error)
}

Publisher ...

func MustPublish

func MustPublish(conf Config) Publisher

func Publish

func Publish(conf Config) (Publisher, error)

func PublishWithClient

func PublishWithClient(conf Config, c APIClient) Publisher

New creates new coordinator instance with API client

type SkipedPublishRequest

type SkipedPublishRequest struct {
	URL              string
	NotificationType NotificationType
	Reason           error
}

SkipedPublishRequest is defined skiped indexing API request

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL