Skip to content

Commit

Permalink
feat: add ID getter methods
Browse files Browse the repository at this point in the history
Without this, it is not possible to know which app or installation a given transport is
for without tracking it separately.

These are getter functions that return copies so they are backwards compatible and cannot
cause any issues or break a transport.
  • Loading branch information
carsonoid committed Jul 11, 2023
1 parent 7e0d5e7 commit a87c06c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
5 changes: 5 additions & 0 deletions appsTransport.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ func (t *AppsTransport) RoundTrip(req *http.Request) (*http.Response, error) {
return resp, err
}

// AppID returns the appID of the transport
func (t *AppsTransport) AppID() int64 {
return t.appID
}

type AppsTransportOption func(*AppsTransport)

// WithSigner configures the AppsTransport to use the given Signer for generating JWT tokens.
Expand Down
4 changes: 4 additions & 0 deletions appsTransport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ func TestAppsTransport(t *testing.T) {
t.Fatalf("error creating transport: %v", err)
}

if tr.appID != appID {
t.Errorf("appID want->got: %d->%d", appID, tr.appID)
}

req := httptest.NewRequest(http.MethodGet, "http://example.com", new(bytes.Buffer))
req.Header.Add("Accept", customHeader)
if _, err := tr.RoundTrip(req); err != nil {
Expand Down
10 changes: 10 additions & 0 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,16 @@ func (t *Transport) Expiry() (expiresAt time.Time, refreshAt time.Time, err erro
return t.token.ExpiresAt, t.token.getRefreshTime(), nil
}

// AppID returns the app ID associated with the transport
func (t *Transport) AppID() int64 {
return t.appID
}

// InstallationID returns the installation ID associated with the transport
func (t *Transport) InstallationID() int64 {
return t.installationID
}

func (t *Transport) refreshToken(ctx context.Context) error {
// Convert InstallationTokenOptions into a ReadWriter to pass as an argument to http.NewRequest.
body, err := GetReadWriter(t.InstallationTokenOptions)
Expand Down
9 changes: 9 additions & 0 deletions transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ func TestNew(t *testing.T) {
}
tr.BaseURL = ts.URL

// test id getter methods
if tr.AppID() != appID {
t.Fatalf("appID got: %q want: %q", tr.AppID(), appID)
}

if tr.InstallationID() != installationID {
t.Fatalf("installationID got: %q want: %q", tr.InstallationID(), installationID)
}

client := http.Client{Transport: tr}
_, err = client.Get(ts.URL + "/auth/with/installation/token/endpoint")
if err != nil {
Expand Down

0 comments on commit a87c06c

Please sign in to comment.