sfdcclient

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2021 License: GPL-3.0 Imports: 12 Imported by: 0

README

sfdcclient

GitHub tag (latest SemVer) GitHub go.mod Go version Go Report Card GitHub code size in bytes

sfdcclient is a golang package implementing a pseudo-wrapper of an HTTP client, for making requests to salesforce's REST API through a connected app, making use of the Salesforce OAuth 2.0 JWT Bearer Flow for Server-to-Server authorization flow.

Installation

go get https://github.com/nicheinc/sfdcclient

Example usage

package main

import (
	"context"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"time"

	"github.com/nicheinc/sfdcclient"
)

func main() {
	// Read your private key into memory
	privateKeyBytes, err := ioutil.ReadFile(os.ExpandEnv("/path/to/your/private/key/file.key"))
	if err != nil {
		log.Fatalf("Error creating logger: %s", err)
	}

	client, err := sfdcclient.NewClientWithJWTBearer(
		true, // whether the instance the client connects to, is a sandbox or not
		"https://xx123.salesforce.com",
		"your_connected_app_consumer_key",
		"username_using_the_connected_app@email_provider.com",
		privateKeyBytes,
		3*time.Second, // request timeout for the OAuth new token HTTP request (3 minute max)
		http.Client{ // underlying HTTP client making all HTTP calls
			Timeout: 5 * time.Second,
		},
	)
	if err != nil {
		log.Fatalf("Error initializing connected app salesforce client: %s", err)
	}

	url := "/services/data/v47.0/sobjects/MySObjectName/describe" // note that this is a relative URL to the salesforce instance server URL
	statusCode, resBody, err := client.SendRequest(context.Background(), http.MethodGet, url, nil, nil)
	if err != nil {
		log.Fatalf("Error sending salesforce request: %s", err)
	}

	fmt.Printf("\nResponse status code: %d", statusCode) // -1 if an error is returned by the SendRequest call
	fmt.Printf("\nResponse body: %s", string(resBody))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type APIErr

type APIErr struct {
	Message string   `json:"message"`
	ErrCode string   `json:"errorCode"`
	Fields  []string `json:"fields"`
}

func (*APIErr) Error

func (e *APIErr) Error() string

type APIErrs

type APIErrs []APIErr

APIErrs represents an error response from salesforce REST API endpoints Example: [

    {
			"statusCode": "MALFORMED_ID",
			"message": "SomeSaleforceObject ID: id value of incorrect type: 1234",
			"fields": [
				"Id"
			]
    }

]

func (*APIErrs) Error

func (e *APIErrs) Error() string

type AccessTokenResponse

type AccessTokenResponse struct {
	AccessToken string `json:"access_token"`
	Scope       string `json:"scope"`
	Instance    string `json:"instance_url"`
	ID          string `json:"id"`
	TokenType   string `json:"token_type"`
}

AccessTokenResponse represents a successful response of a requests to salesforce for an OAuth token https://${yourInstance}.salesforce.com/services/oauth2/token

type Client

type Client interface {
	SendRequest(ctx context.Context, method, relURL string, headers http.Header, requestBody []byte) (int, []byte, error)
}

func NewClientWithJWTBearer

func NewClientWithJWTBearer(sandbox bool, instanceURL, consumerKey, username string, privateKey []byte, tokenDuration time.Duration, httpClient http.Client) (Client, error)

type ClientMock

type ClientMock struct {
	SendRequestStub   func(ctx context.Context, method string, relURL string, headers http.Header, requestBody []byte) (int, []byte, error)
	SendRequestCalled int32
}

func (*ClientMock) SendRequest

func (m *ClientMock) SendRequest(ctx context.Context, method string, relURL string, headers http.Header, requestBody []byte) (int, []byte, error)

type OAuthErr

type OAuthErr struct {
	Code        string `json:"error"`
	Description string `json:"error_description"`
}

OAuthErr represents an error that occurs during the OAuth authorization flow https://help.salesforce.com/articleView?id=remoteaccess_oauth_flow_errors.htm&type=5

func (*OAuthErr) Error

func (e *OAuthErr) Error() string

Jump to

Keyboard shortcuts

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