-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
402f506
commit 062e71a
Showing
2 changed files
with
98 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package revenuecat | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/url" | ||
"time" | ||
|
||
"github.com/wearemojo/mojo-public-go/lib/httpclient" | ||
"github.com/wearemojo/mojo-public-go/lib/jsonclient" | ||
"github.com/wearemojo/mojo-public-go/lib/secret" | ||
) | ||
|
||
const baseURL = "https://api.revenuecat.com/v1" | ||
|
||
type Client struct { | ||
client *jsonclient.Client | ||
} | ||
|
||
func NewClient(ctx context.Context, serverTokenSecretID string) (*Client, error) { | ||
if _, err := secret.Get(ctx, serverTokenSecretID); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Client{ | ||
client: jsonclient.NewClient( | ||
baseURL, | ||
httpclient.NewClient(5*time.Second, roundTripper{serverTokenSecretID}), | ||
), | ||
}, nil | ||
} | ||
|
||
type UserInfoResponse struct { | ||
Subscriber Subscriber `json:"subscriber"` | ||
} | ||
|
||
type Subscriber struct { | ||
Entitlements map[string]Entitlement `json:"entitlements"` | ||
Subscriptions map[string]Subscription `json:"subscriptions"` | ||
} | ||
|
||
type Entitlement struct { | ||
ExpiresDate time.Time `json:"expires_date"` | ||
ProductIdentifier string `json:"product_identifier"` | ||
} | ||
|
||
type Subscription struct { | ||
PeriodType PeriodType `json:"period_type"` | ||
UnsubscribeDetectedAt *time.Time `json:"unsubscribe_detected_at"` | ||
} | ||
|
||
type PeriodType string | ||
|
||
const ( | ||
PeriodTypeTrial PeriodType = "trial" | ||
PeriodTypeIntro PeriodType = "intro" | ||
PeriodTypeNormal PeriodType = "normal" | ||
) | ||
|
||
// https://www.revenuecat.com/reference/subscribers | ||
// This API either fetches a user or creates one :shrug: | ||
// for now, we only care about the expiry date of the full_access entitlement | ||
// and the period type of the subscription. See useSubscriptionStateIap.ts | ||
func (c *Client) GetOrCreateSubscriberInfo(ctx context.Context, appUserID string) (res *UserInfoResponse, err error) { | ||
escapedAppUserID := url.PathEscape(appUserID) | ||
path := fmt.Sprintf("/subscribers/%s", escapedAppUserID) | ||
return res, c.client.Do(ctx, "GET", path, nil, nil, &res) | ||
} |
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,30 @@ | ||
package revenuecat | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/wearemojo/mojo-public-go/lib/secret" | ||
) | ||
|
||
type roundTripper struct { | ||
SecretID string | ||
} | ||
|
||
func (r roundTripper) RoundTrip(req *http.Request) (*http.Response, error) { | ||
ctx := req.Context() | ||
req = req.Clone(ctx) | ||
|
||
apiKey, err := secret.Get(ctx, r.SecretID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if req.Header == nil { | ||
req.Header = http.Header{} | ||
} | ||
|
||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", apiKey)) | ||
|
||
return http.DefaultTransport.RoundTrip(req) | ||
} |