Skip to content

Commit

Permalink
add example
Browse files Browse the repository at this point in the history
  • Loading branch information
StephanHCB committed Mar 27, 2022
1 parent 8bb498f commit 10bc257
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Each of the individual features isn't spectacular, but in combination I've found
- can use multiple instances with different configurations
- context aware, including cancel and shutdown
- support for timeouts both at the httpclient and higher levels
- support for plugging in a circuitbreaker (not included with this library, see [go-autumn-restclient-gobreaker](https://github.com/StephanHCB/go-autumn-restclient-gobreaker))
- support for plugging in a circuitbreaker (not included with this library, see [go-autumn-restclient-circuitbreaker](https://github.com/StephanHCB/go-autumn-restclient-circuitbreaker))
- conditional retry (using a callback so you're flexible about the retry condition)
- support for context aware request logging
- support for pre-request header/request manipulation (using a callback)
Expand All @@ -37,7 +37,7 @@ Each of the individual features isn't spectacular, but in combination I've found

## Usage

TODO
Please see the [examples](https://github.com/StephanHCB/go-autumn-restclient/tree/main/example).

## Logging

Expand Down
37 changes: 37 additions & 0 deletions example/fullstack/fullstack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package examplefullstack

import (
"context"
aulogging "github.com/StephanHCB/go-autumn-logging"
aurestclientapi "github.com/StephanHCB/go-autumn-restclient/api"
auresthttpclient "github.com/StephanHCB/go-autumn-restclient/implementation/httpclient"
aurestlogging "github.com/StephanHCB/go-autumn-restclient/implementation/requestlogging"
aurestretry "github.com/StephanHCB/go-autumn-restclient/implementation/retry"
"net/http"
)

func example() {
// 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()

// set up
httpClient, _ := auresthttpclient.New(0, []byte{}, nil)
requestLoggingClient := aurestlogging.New(httpClient)
retryingClient := aurestretry.New(requestLoggingClient, 2, func(ctx context.Context, response *aurestclientapi.ParsedResponse, err error) bool {
return response.Status >= 500 || err != nil
}, nil)

// now make a request
bodyDto := make(map[string]interface{})

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

// now bodyDto is filled with the response
}

0 comments on commit 10bc257

Please sign in to comment.