Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jwt credentials, bulk Job ResultsAll and some minor deprecated and typos #7

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bulk/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func NewFormatter(job *Job, fields []string) (*Formatter, error) {

builder := &strings.Builder{}
writer := csv.NewWriter(builder)
writer.Comma = job.delimiter()
writer.Comma = job.Delimiter()
writer.UseCRLF = job.info.LineEnding == CarriageReturnLinefeed

f := &Formatter{
Expand Down
2 changes: 1 addition & 1 deletion bulk/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func TestNewFormatter(t *testing.T) {
}

if tt.want != nil {
tt.want.sb.WriteString(strings.Join(tt.want.fields, string(tt.want.job.delimiter())))
tt.want.sb.WriteString(strings.Join(tt.want.fields, string(tt.want.job.Delimiter())))
tt.want.sb.WriteString("\n")

assert.Equalf(t, tt.want.job, got.job, "NewFormatter().job = %v, want %v", got.job, tt.want.job)
Expand Down
53 changes: 45 additions & 8 deletions bulk/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,8 +488,8 @@ func (j *Job) Wait(ctx context.Context) (Info, error) {
}
}

// Results returns a page of results from a Query job.
func (j *Job) Results(ctx context.Context, locator string, maxRecords int) (*ResultsPage, error) {
// results returns a page of results from a Query job.
func (j *Job) results(ctx context.Context, locator string, maxRecords int) (*http.Response, error) {
if j.endpoint != V2QueryEndpoint {
return nil, errors.New("job error: results only available for query jobs")
}
Expand All @@ -515,15 +515,26 @@ func (j *Job) Results(ctx context.Context, locator string, maxRecords int) (*Res
if err != nil {
return nil, err
}
defer response.Body.Close()

if response.StatusCode != http.StatusOK {
return nil, sfdc.HandleError(response)
}

return response, nil
}

// Results returns a page of results from a Query job.
func (j *Job) Results(ctx context.Context, locator string, maxRecords int) (*ResultsPage, error) {

response, err := j.results(ctx, locator, maxRecords)
if err != nil {
return nil, err
}
defer response.Body.Close()

reader := csv.NewReader(response.Body)
reader.LazyQuotes = true
reader.Comma = j.delimiter()
reader.Comma = j.Delimiter()

count, _ := strconv.Atoi(response.Header.Get(HeaderRecordCount))
result := ResultsPage{
Expand All @@ -549,6 +560,32 @@ func (j *Job) Results(ctx context.Context, locator string, maxRecords int) (*Res
return &result, nil
}

// Results returns a page of results from a Query job.
// return n is total records
func (j *Job) ResultsAll(ctx context.Context, writer io.Writer, maxRecords int) (int64, error) {
var n int64 = 0
for locator := ""; locator != "null"; {
response, err := j.results(ctx, locator, maxRecords)
if err != nil {
return 0, err
}
defer response.Body.Close()
_, err = io.Copy(writer, response.Body)
if err != nil {
return 0, err
}

count, err := strconv.Atoi(response.Header.Get(HeaderRecordCount))
if err != nil {
return n, err
}
n += int64(count)
locator = response.Header.Get(HeaderLocator)
}

return n, nil
}

// SuccessfulRecords returns the successful records for the job.
func (j *Job) SuccessfulRecords(ctx context.Context) ([]SuccessfulRecord, error) {
url := j.session.DataServiceURL() + string(j.endpoint) + "/" + j.info.ID + "/successfulResults/"
Expand All @@ -571,7 +608,7 @@ func (j *Job) SuccessfulRecords(ctx context.Context) ([]SuccessfulRecord, error)

reader := csv.NewReader(response.Body)
reader.LazyQuotes = true
reader.Comma = j.delimiter()
reader.Comma = j.Delimiter()

var records []SuccessfulRecord
fields, err := reader.Read()
Expand Down Expand Up @@ -622,7 +659,7 @@ func (j *Job) FailedRecords(ctx context.Context) ([]FailedRecord, error) {

reader := csv.NewReader(response.Body)
reader.LazyQuotes = true
reader.Comma = j.delimiter()
reader.Comma = j.Delimiter()

var records []FailedRecord
fields, err := reader.Read()
Expand Down Expand Up @@ -669,7 +706,7 @@ func (j *Job) UnprocessedRecords(ctx context.Context) ([]UnprocessedRecord, erro

reader := csv.NewReader(response.Body)
reader.LazyQuotes = true
reader.Comma = j.delimiter()
reader.Comma = j.Delimiter()

var records []UnprocessedRecord
fields, err := reader.Read()
Expand Down Expand Up @@ -715,7 +752,7 @@ func (j *Job) record(fields, values []string) map[string]interface{} {
return record
}

func (j *Job) delimiter() rune {
func (j *Job) Delimiter() rune {
switch ColumnDelimiter(j.info.ColumnDelimiter) {
case Tab:
return '\t'
Expand Down
2 changes: 1 addition & 1 deletion bulk/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func TestJob_delimiter(t *testing.T) {
session: tt.fields.session,
info: tt.fields.info,
}
if got := j.delimiter(); got != tt.want {
if got := j.Delimiter(); got != tt.want {
t.Errorf("Job.delimiter() = %v, want %v", got, tt.want)
}
})
Expand Down
3 changes: 1 addition & 2 deletions bulk/v1/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"

Expand Down Expand Up @@ -205,7 +204,7 @@ func (b *Batch) Results(ctx context.Context) (BatchResult, error) {
return result, sfdc.HandleError(response)
}

body, err := ioutil.ReadAll(response.Body)
body, err := io.ReadAll(response.Body)
if err != nil {
return result, err
}
Expand Down
14 changes: 7 additions & 7 deletions bulk/v1/batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package bulkv1

import (
"context"
"io/ioutil"
"io"
"net/http"
"strings"
"testing"
Expand Down Expand Up @@ -47,7 +47,7 @@ func TestBatch_response(t *testing.T) {
return &http.Response{
StatusCode: http.StatusOK,
Status: "OK",
Body: ioutil.NopCloser(strings.NewReader(resp)),
Body: io.NopCloser(strings.NewReader(resp)),
Header: make(http.Header),
}
},
Expand Down Expand Up @@ -81,7 +81,7 @@ func TestBatch_response(t *testing.T) {
return &http.Response{
StatusCode: http.StatusBadRequest,
Status: "Bad",
Body: ioutil.NopCloser(strings.NewReader(resp)),
Body: io.NopCloser(strings.NewReader(resp)),
Header: make(http.Header),
}
}),
Expand Down Expand Up @@ -147,7 +147,7 @@ func TestBatch_fetchInfo(t *testing.T) {
return &http.Response{
StatusCode: http.StatusOK,
Status: "OK",
Body: ioutil.NopCloser(strings.NewReader(resp)),
Body: io.NopCloser(strings.NewReader(resp)),
Header: make(http.Header),
}
},
Expand Down Expand Up @@ -224,7 +224,7 @@ func TestBatch_create(t *testing.T) {
return &http.Response{
StatusCode: http.StatusCreated,
Status: "OK",
Body: ioutil.NopCloser(strings.NewReader(resp)),
Body: io.NopCloser(strings.NewReader(resp)),
Header: make(http.Header),
}
},
Expand Down Expand Up @@ -300,7 +300,7 @@ func TestBatch_Results(t *testing.T) {
return &http.Response{
StatusCode: http.StatusOK,
Status: "OK",
Body: ioutil.NopCloser(strings.NewReader(resp)),
Body: io.NopCloser(strings.NewReader(resp)),
Header: make(http.Header),
}
},
Expand Down Expand Up @@ -363,7 +363,7 @@ func TestBatch_Results(t *testing.T) {
return &http.Response{
StatusCode: http.StatusOK,
Status: "OK",
Body: ioutil.NopCloser(strings.NewReader(resp)),
Body: io.NopCloser(strings.NewReader(resp)),
Header: make(http.Header),
}
},
Expand Down
10 changes: 5 additions & 5 deletions bulk/v1/bulk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package bulkv1

import (
"context"
"io/ioutil"
"io"
"net/http"
"reflect"
"strings"
Expand Down Expand Up @@ -76,7 +76,7 @@ func TestResource_CreateJob(t *testing.T) {
return &http.Response{
StatusCode: 500,
Status: "Invalid URL",
Body: ioutil.NopCloser(strings.NewReader(req.URL.String())),
Body: io.NopCloser(strings.NewReader(req.URL.String())),
Header: make(http.Header),
}
}
Expand All @@ -101,7 +101,7 @@ func TestResource_CreateJob(t *testing.T) {
return &http.Response{
StatusCode: http.StatusOK,
Status: "Good",
Body: ioutil.NopCloser(strings.NewReader(resp)),
Body: io.NopCloser(strings.NewReader(resp)),
Header: make(http.Header),
}

Expand Down Expand Up @@ -151,7 +151,7 @@ func TestResource_GetJob(t *testing.T) {
return &http.Response{
StatusCode: 500,
Status: "Invalid URL",
Body: ioutil.NopCloser(strings.NewReader(req.URL.String())),
Body: io.NopCloser(strings.NewReader(req.URL.String())),
Header: make(http.Header),
}
}
Expand All @@ -176,7 +176,7 @@ func TestResource_GetJob(t *testing.T) {
return &http.Response{
StatusCode: http.StatusOK,
Status: "Good",
Body: ioutil.NopCloser(strings.NewReader(resp)),
Body: io.NopCloser(strings.NewReader(resp)),
Header: make(http.Header),
}

Expand Down
Loading