()
| 71 | } |
| 72 | |
| 73 | func main() { |
| 74 | log.SetLevel(log.InfoLevel) |
| 75 | |
| 76 | ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| 77 | defer cancel() |
| 78 | |
| 79 | core := coreauth.NewManager(nil, nil, nil) |
| 80 | core.RegisterExecutor(EchoExecutor{}) |
| 81 | |
| 82 | auth := &coreauth.Auth{ |
| 83 | ID: "demo-echo", |
| 84 | Provider: providerKey, |
| 85 | Attributes: map[string]string{ |
| 86 | "api_key": "demo-api-key", |
| 87 | }, |
| 88 | } |
| 89 | |
| 90 | // Example 1: Build a prepared request and execute it using your own http.Client. |
| 91 | reqPrepared, errReqPrepared := core.NewHttpRequest( |
| 92 | ctx, |
| 93 | auth, |
| 94 | http.MethodGet, |
| 95 | "https://httpbin.org/anything", |
| 96 | nil, |
| 97 | http.Header{"X-Example": []string{"prepared"}}, |
| 98 | ) |
| 99 | if errReqPrepared != nil { |
| 100 | panic(errReqPrepared) |
| 101 | } |
| 102 | respPrepared, errDoPrepared := http.DefaultClient.Do(reqPrepared) |
| 103 | if errDoPrepared != nil { |
| 104 | panic(errDoPrepared) |
| 105 | } |
| 106 | defer func() { |
| 107 | if errClose := respPrepared.Body.Close(); errClose != nil { |
| 108 | log.Errorf("close response body error: %v", errClose) |
| 109 | } |
| 110 | }() |
| 111 | bodyPrepared, errReadPrepared := io.ReadAll(respPrepared.Body) |
| 112 | if errReadPrepared != nil { |
| 113 | panic(errReadPrepared) |
| 114 | } |
| 115 | fmt.Printf("Prepared request status: %d\n%s\n\n", respPrepared.StatusCode, bodyPrepared) |
| 116 | |
| 117 | // Example 2: Execute a raw request via core.HttpRequest (auto inject + do). |
| 118 | rawBody := []byte(`{"hello":"world"}`) |
| 119 | rawReq, errRawReq := http.NewRequestWithContext(ctx, http.MethodPost, "https://httpbin.org/anything", bytes.NewReader(rawBody)) |
| 120 | if errRawReq != nil { |
| 121 | panic(errRawReq) |
| 122 | } |
| 123 | rawReq.Header.Set("Content-Type", "application/json") |
| 124 | rawReq.Header.Set("X-Example", "executed") |
| 125 | |
| 126 | respExec, errDoExec := core.HttpRequest(ctx, auth, rawReq) |
| 127 | if errDoExec != nil { |
| 128 | panic(errDoExec) |
| 129 | } |
| 130 | defer func() { |
nothing calls this directly
no test coverage detected