-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: generic api request function (#218)
* format url with url params * make a real request * handle singular outputs only for now * refactor client * fix tests * remove unused stuff * remove more stuff * pr feedback * fix output * fix teams cmd help text
- Loading branch information
1 parent
4ed7125
commit 0141d07
Showing
5 changed files
with
146 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package resources | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"time" | ||
|
||
"ldcli/internal/errors" | ||
) | ||
|
||
type Client interface { | ||
MakeRequest(accessToken, method, path, contentType string, query url.Values, data []byte) ([]byte, error) | ||
} | ||
|
||
type ResourcesClient struct { | ||
cliVersion string | ||
} | ||
|
||
var _ Client = ResourcesClient{} | ||
|
||
func NewClient(cliVersion string) ResourcesClient { | ||
return ResourcesClient{cliVersion: cliVersion} | ||
} | ||
|
||
func (c ResourcesClient) MakeRequest(accessToken, method, path, contentType string, query url.Values, data []byte) ([]byte, error) { | ||
client := http.Client{Timeout: 3 * time.Second} | ||
|
||
req, _ := http.NewRequest(method, path, bytes.NewReader(data)) | ||
req.Header.Add("Authorization", accessToken) | ||
req.Header.Add("Content-type", contentType) | ||
req.Header.Set("User-Agent", fmt.Sprintf("launchdarkly-cli/v%s", c.cliVersion)) | ||
req.URL.RawQuery = query.Encode() | ||
|
||
res, err := client.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
body, err := io.ReadAll(res.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer res.Body.Close() | ||
|
||
if res.StatusCode >= 400 { | ||
return body, errors.NewAPIError(body, nil, nil) | ||
} | ||
|
||
return body, nil | ||
} |