-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from jhrozek/interface
Use a wrapper interface instead of using go-github directly
- Loading branch information
Showing
8 changed files
with
149 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// | ||
// Copyright 2023 Stacklok, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Package ghrest provides a wrapper around the go-github client that implements the internal REST API | ||
package ghrest | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/google/go-github/v56/github" | ||
) | ||
|
||
// GhRest is the struct that contains the GitHub REST API client | ||
// this struct implements the REST API | ||
type GhRest struct { | ||
client *github.Client | ||
} | ||
|
||
// NewGhRest creates a new instance of GhRest | ||
func NewGhRest(token string) *GhRest { | ||
ghcli := github.NewClient(nil) | ||
|
||
if token != "" { | ||
ghcli = ghcli.WithAuthToken(token) | ||
} | ||
return &GhRest{ | ||
client: ghcli, | ||
} | ||
} | ||
|
||
// NewRequest creates an API request. A relative URL can be provided in urlStr, | ||
// which will be resolved to the BaseURL of the Client. Relative URLS should | ||
// always be specified without a preceding slash. If specified, the value | ||
// pointed to by body is JSON encoded and included as the request body. | ||
func (c *GhRest) NewRequest(method, requestUrl string, body any) (*http.Request, error) { | ||
return c.client.NewRequest(method, requestUrl, body) | ||
} | ||
|
||
// Do sends an API request and returns the API response. | ||
func (c *GhRest) Do(ctx context.Context, req *http.Request) (*http.Response, error) { | ||
var buf bytes.Buffer | ||
|
||
// The GitHub client closes the response body, so we need to capture it | ||
// in a buffer so that we can return it to the caller | ||
resp, err := c.client.Do(ctx, req, &buf) | ||
if err != nil && resp == nil { | ||
return nil, err | ||
} | ||
|
||
if resp.Response != nil { | ||
resp.Response.Body = io.NopCloser(&buf) | ||
} | ||
|
||
return resp.Response, err | ||
} |
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,33 @@ | ||
// | ||
// Copyright 2023 Stacklok, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Package interfaces provides generic interfaces for communicating with remotes | ||
// like github | ||
package interfaces | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
) | ||
|
||
// The REST interface allows to wrap clients to talk to remotes | ||
// When talking to GitHub, wrap a github client to provide this interface | ||
type REST interface { | ||
// NewRequest creates an HTTP request. | ||
NewRequest(method, url string, body any) (*http.Request, error) | ||
|
||
// Do executes an HTTP request. | ||
Do(ctx context.Context, req *http.Request) (*http.Response, error) | ||
} |