Skip to content

Commit

Permalink
feat(#14): add example for file upload
Browse files Browse the repository at this point in the history
  • Loading branch information
StephanHCB committed Jan 12, 2023
1 parent 03d3610 commit e732ce7
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions example/fullstack/fullstack.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package examplefullstack

import (
"bytes"
"context"
aulogging "github.com/StephanHCB/go-autumn-logging"
aurestclientapi "github.com/StephanHCB/go-autumn-restclient/api"
Expand All @@ -11,6 +12,8 @@ import (
aurestrecorder "github.com/StephanHCB/go-autumn-restclient/implementation/recorder"
aurestlogging "github.com/StephanHCB/go-autumn-restclient/implementation/requestlogging"
aurestretry "github.com/StephanHCB/go-autumn-restclient/implementation/retry"
"io"
"mime/multipart"
"net/http"
"time"
)
Expand Down Expand Up @@ -112,3 +115,70 @@ func testingExample() {
// also assert on the recording what requests were made
_ = aurestcapture.GetRecording(requestCaptureClient)
}

func fileUploadExample() {
fileContents := &bytes.Buffer{} // here you'd read the file, or open a reader for the file

// construct custom response body

body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, _ := writer.CreateFormFile("file", "filename.txt")
_, _ = io.Copy(part, fileContents)
_ = writer.Close()

request := aurestclientapi.CustomRequestBody{
BodyReader: body,
BodyLength: body.Len(),
ContentType: writer.FormDataContentType(),
}

// assumes you set up logging by importing one of the go-autumn-logging-xxx dependencies
//
// for this example, let's set up a logger that does nothing, so we don't pull in these dependencies here
//
// This of course makes the requestLoggingClient not work.
aulogging.SetupNoLoggerForTesting()

// ----- setup (can be done once during application startup) ----

// 1. set up http client
var timeout time.Duration = 0
var customCACert []byte = nil
var requestManipulator aurestclientapi.RequestManipulatorCallback = nil

httpClient, _ := auresthttpclient.New(timeout, customCACert, requestManipulator)

// 2. recording (for use with 1a)
recorderClient := aurestrecorder.New(httpClient)

// 3. request logging
requestLoggingClient := aurestlogging.New(recorderClient)

// 4. circuit breaker (see https://github.com/StephanHCB/go-autumn-restclient-circuitbreaker)
// not included here because it has extra dependencies

// 5. retry
var repeatCount uint8 = 2 // 0 means only try once (but then why use this at all?)
var condition aurestclientapi.RetryConditionCallback = func(ctx context.Context, response *aurestclientapi.ParsedResponse, err error) bool {
return response.Status == http.StatusRequestTimeout
}
var beforeRetry aurestclientapi.BeforeRetryCallback = nil

retryingClient := aurestretry.New(requestLoggingClient, repeatCount, condition, beforeRetry)

// ----- now make a request -----

bodyDto := make(map[string]interface{})

response := aurestclientapi.ParsedResponse{
Body: &bodyDto,
}
err := retryingClient.Perform(context.Background(), http.MethodGet, "https://some.rest.api", request, &response)
if err != nil {
return
}

// now bodyDto is filled with the response and response.Status and response.Header are also set.

}

0 comments on commit e732ce7

Please sign in to comment.