batchapi

package
v0.0.0-...-5f8180a Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2024 License: MIT Imports: 13 Imported by: 0

README

Batch API

The Batch API provides an endpoint to perform multiple requests simultaneously. This is particularly useful when you need to make multiple independent requests and want to optimize network usage.

[POST] /now/batch

Sends multiple REST API requests in a single HTTP call. This can significantly improve the performance of your application by reducing the number of HTTP requests.

NOTE: For performance reasons, avoid including long-running requests and requests that retrieve large amounts of data.

Code Example

Try on Playground.

package main

import (
    batchapi "github.com/hdisysteme/servicenow-sdk-go/batch-api"
    core "github.com/hdisysteme/servicenow-sdk-go/core"
    tableapi "github.com/hdisysteme/servicenow-sdk-go/table-api"
    servicenow "github.com/hdisysteme/servicenow-sdk-go"
)

// createTableDeleteRequest creates a delete request for a given table and sysId.
func createTableDeleteRequest(client *servicenow.ServiceNowClient, tableName, sysId string) (*core.RequestInformation, error) {
    pathParameters := map[string]string{
        "baseurl": client.BaseUrl + "/now",
        "table":   tableName,
    }

    requestBuilder := tableapi.NewTableRequestBuilder(client, pathParameters)
    return requestBuilder.ToDeleteRequestInformation2(nil)
}

// buildBatchrequest builds a batch request from a list of request information.
func buildBatchrequest(client *servicenow.ServiceNowClient, requests ...*core.RequestInformation)  (*batchapi.BatchRequest, error) {
    batchRequest := batchapi.NewBatchRequest(client)

    for _, request := range requests {
        if err := batchRequest.AddRequest(request, False); err != nil {
            return nil, err
        }
    }
    return batchRequest, nil
}

func main() {
    cred := credentials.NewUsernamePasswordCredential(username, password)
    client, err := servicenow.NewServiceNowClient2(cred, instance)
    if err != nil {
        fmt.Println("Error creating ServiceNow client:", err)
        return
    }
    
    deleteRequest, err := createTableDeleteRequest(client, "tableName", "sysId")
    if err != nil {
        fmt.Println("Error creating table delete request:", err)
        return
    }

    pathParameters := map[string]string{
        "baseurl": client.BaseUrl + "/now",
    }

    batchRequest, err := buildBatchrequest(client, deleteRequest)
    if err != nil {
        fmt.Println("Error building batch request:", err)
        return
    }

    builder := batchapi.NewBatchRequestBuilder(client, pathParameters)
    response, err := builder.Post(client, batchRequest)
    if err != nil {
        fmt.Println("Error posting batch request:", err)
        return
    }
    // Handle response
}
Code Explanation

The code example above demonstrates how to use the Batch API to send multiple requests in a single HTTP call. Here's a step-by-step explanation:

  1. createTableDeleteRequest: This function creates a delete request for a given table and sysId.
  2. buildBatchrequest: This function builds a batch request from a list of request information.
  3. main: This function is the entry point of the program. It creates a ServiceNow client, builds a delete request, adds it to a batch request, and sends the batch request.
Error Handling

The code example includes error handling. If an error occurs at any point (for example, when creating the ServiceNow client or sending the batch request), the error will be printed to the console and the program will exit. This prevents the program from continuing with invalid data.

See Also

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BatchRequest

type BatchRequest interface {
	AddRequest(internal.RequestInformation, bool) error
}

func NewBatchRequest

func NewBatchRequest(client core.Client2) BatchRequest

type BatchRequestBuilder

type BatchRequestBuilder struct {
	internal.RequestBuilder
}

BatchRequestBuilder constructs batch requests for the specified base URL.

func NewBatchRequestBuilder

func NewBatchRequestBuilder(client core.Client, pathParameters map[string]string) *BatchRequestBuilder

NewBatchRequestBuilder creates a new BatchRequestBuilder. It takes a client (core.Client) and pathParameters (map[string]string).

func (*BatchRequestBuilder) Post

func (rB *BatchRequestBuilder) Post(request BatchRequest) (BatchResponse, error)

Post sends a batch request and returns the BatchResponse.

type BatchRequestItem

type BatchRequestItem interface {
	GetID() *string
	SetID(value *string)
	GetMethod() *string
	SetMethod(value *string)
	GetURL() *string
	SetURL(value *string)
	GetHeaders() core.RequestHeader
	SetHeaders(value core.RequestHeader)
	GetBody() internal.RequestBody
	SetBody(value internal.RequestBody)
	GetExcludeResponseHeaders() bool
	SetExcludeResponseHeaders(bool)
}

func NewBatchItem

func NewBatchItem(excludeResponseHeaders bool) BatchRequestItem

NewBatchItem creates an instance of BatchItem

type BatchResponse

type BatchResponse interface {
	GetID() *string
	GetResponses() []BatchResponseItem[any]
	GetResponse(id string) BatchResponseItem[any]
}

type BatchResponseItem

type BatchResponseItem[T any] interface {
	GetBody() (*T, error)
	GetType() reflect.Type
	GetExecutionTime() time.Duration
	GetHeaders() []batchHeader
	GetID() string
	GetRedirectURL() string
	GetStatusCode() int
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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