github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/session/README.md (about) 1 # session package 2 3 This library provides a simple and consistent REST over HTTP library for accessing Akamai Endpoints 4 5 ## Depedencies 6 7 This library is dependent on the `github.com/akamai/AkamaiOPEN-edgegrid-golang/pkg/edgegrid` interface. 8 9 ## Basic Example 10 11 ``` 12 func main() { 13 edgerc := Must(New()) 14 15 s, err := session.New( 16 session.WithConfig(edgerc), 17 ) 18 if err != nil { 19 panic(err) 20 } 21 22 var contracts struct { 23 AccountID string `json:"accountId"` 24 Contracts ContractsItems `json:"contracts"` 25 Items []struct { 26 ContractID string `json:"contractId"` 27 ContractTypeName string `json:"contractTypeName"` 28 } `json:"items"` 29 } 30 31 req, _ := http.NewRequest(http.MethodGet, "/papi/v1/contracts", nil) 32 33 _, err := s.Exec(r, &contracts) 34 if err != nil { 35 panic(err); 36 } 37 38 // do something with contracts 39 } 40 41 ``` 42 43 ## Library Logging 44 The session package supports the structured logging interface from `github.com/apex`. These can be applied globally to the session or to the request context. 45 46 ### Adding a logger to the session 47 48 ``` 49 s, err := session.New( 50 session.WithConfig(edgerc), 51 session.WithLog(log.Log), 52 ) 53 if err != nil { 54 panic(err) 55 } 56 ``` 57 58 ### Request logging 59 The logger can be overidden for a specific request like this 60 61 ``` 62 req, _ := http.NewRequest(http.MethodGet, "/papi/v1/contracts", nil) 63 64 req = req.WithContext( 65 session.ContextWithOptions(request.Context(), 66 session.WithContextLog(otherlog), 67 ) 68 ``` 69 70 ## Custom request headers 71 The context can also be updated to pass special http headers when necessary 72 73 ``` 74 customHeader := make(http.Header) 75 customHeader.Set("X-Custom-Header", "some custom value") 76 77 req = req.WithContext( 78 session.ContextWithOptions(request.Context(), 79 session.WithContextHeaders(customHeader), 80 ) 81 ```