Skip to content

Commit

Permalink
Merge pull request #90 from speakeasy-api/add_free_warning_gen_action
Browse files Browse the repository at this point in the history
feat: usage warning in SDK generation action
  • Loading branch information
ryan-timothy-albert authored Feb 23, 2024
2 parents 6b0952c + 72d14f7 commit c0f0c60
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
38 changes: 38 additions & 0 deletions internal/cli/speakeasy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"archive/tar"
"archive/zip"
"compress/gzip"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path"
Expand Down Expand Up @@ -183,3 +185,39 @@ func extractTarGZ(archive, dest string) error {

return nil
}

func CheckFreeUsageAccess() (bool, error) {
apiURL := "https://api.speakeasyapi.dev/v1/workspace/access?passive=true"

req, err := http.NewRequest("GET", apiURL, nil)
if err != nil {
return false, fmt.Errorf("error creating the request: %w", err)
}

apiKey := os.Getenv("SPEAKEASY_API_KEY")
req.Header.Set("x-api-key", apiKey)

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return false, fmt.Errorf("error making the API request: %w", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return false, fmt.Errorf("API request failed with status code: %d", resp.StatusCode)
}

body, err := io.ReadAll(resp.Body)
if err != nil {
return false, fmt.Errorf("error reading the response body: %w", err)
}
var accessDetails struct {
GenerationAllowed bool `json:"generation_allowed"`
}
if err := json.Unmarshal(body, &accessDetails); err != nil {
return false, fmt.Errorf("error unmarshaling the response: %w", err)
}

return accessDetails.GenerationAllowed, nil
}
11 changes: 10 additions & 1 deletion internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/json"
"fmt"
"math"
"net/url"
"os"
"os/exec"
Expand Down Expand Up @@ -403,6 +404,14 @@ Based on:
- OpenAPI Doc %s %s
- Speakeasy CLI %s (%s) https://github.com/speakeasy-api/speakeasy%s`, releaseInfo.DocVersion, releaseInfo.DocLocation, releaseInfo.SpeakeasyVersion, releaseInfo.GenerationVersion, changelog)

// TODO: To be removed after we start blocking on usage limits.
if accessAllowed, err := cli.CheckFreeUsageAccess(); err == nil && !accessAllowed {
warningDate := time.Date(2024, time.March, 18, 0, 0, 0, 0, time.UTC)
daysToLimit := int(math.Round(warningDate.Sub(time.Now().Truncate(24*time.Hour)).Hours() / 24))
body += "\n\n\n" + fmt.Sprintf(`## 🚀 Time to Upgrade 🚀
You have exceeded the limit of one free generated SDK. Please reach out to the Speakeasy team in the next %d days to ensure continued access`, daysToLimit) + "\n\n"
}

const maxBodyLength = 65536

if len(body) > maxBodyLength {
Expand Down Expand Up @@ -629,7 +638,7 @@ func (g *Git) GetDownloadLink(version string) (string, string, error) {
if err != nil {
return "", "", fmt.Errorf("failed to get speakeasy cli releases: %w", err)
}

if len(releases) == 0 {
return "", "", fmt.Errorf("no speakeasy cli releases found")
} else {
Expand Down

0 comments on commit c0f0c60

Please sign in to comment.