summaryrefslogtreecommitdiff
path: root/internal/platforms
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2024-10-27 11:34:16 +0200
committerPaul Buetow <paul@buetow.org>2024-10-27 11:34:16 +0200
commitf9a1a4dd6f154c08f308431b821cc23e5dac8942 (patch)
tree2850b7f610b55d84e15d40cad898a355044029af /internal/platforms
parent063d630bc1acbaaf1b9743a33bef559601b117d3 (diff)
fix timeouts
Diffstat (limited to 'internal/platforms')
-rw-r--r--internal/platforms/linkedin/linkedin.go14
-rw-r--r--internal/platforms/mastodon/mastodon.go8
2 files changed, 18 insertions, 4 deletions
diff --git a/internal/platforms/linkedin/linkedin.go b/internal/platforms/linkedin/linkedin.go
index 54752b9..a6507a3 100644
--- a/internal/platforms/linkedin/linkedin.go
+++ b/internal/platforms/linkedin/linkedin.go
@@ -9,6 +9,7 @@ import (
"io"
"log"
"net/http"
+ "time"
"codeberg.org/snonux/gos/internal/config"
"codeberg.org/snonux/gos/internal/entry"
@@ -18,7 +19,8 @@ import (
var errUnauthorized = errors.New("unauthorized access, refresh or create token?")
-// TODO: Why are no previews of links shown then posted?
+const linkedInTimeout = 10 * time.Second
+
func Post(ctx context.Context, args config.Args, sizeLimit int, ent entry.Entry) error {
err := post(ctx, args, sizeLimit, ent)
if errors.Is(err, errUnauthorized) {
@@ -34,7 +36,10 @@ func post(ctx context.Context, args config.Args, sizeLimit int, ent entry.Entry)
log.Println("Not posting", ent, "to LinkedIn as dry-run enabled")
return nil
}
- personID, accessToken, err := oauth2.LinkedInCreds(ctx, args)
+
+ newCtx, cancel := context.WithTimeout(ctx, linkedInTimeout)
+ defer cancel()
+ personID, accessToken, err := oauth2.LinkedInCreds(newCtx, args)
if err != nil {
return err
}
@@ -53,7 +58,10 @@ func post(ctx context.Context, args config.Args, sizeLimit int, ent entry.Entry)
}
return err
}
- return callLinkedInAPI(ctx, personID, accessToken, content, urls)
+
+ newCtx, cancel = context.WithTimeout(ctx, linkedInTimeout)
+ defer cancel()
+ return callLinkedInAPI(newCtx, personID, accessToken, content, urls)
}
func callLinkedInAPI(ctx context.Context, personID, accessToken, content string, urls []string) error {
diff --git a/internal/platforms/mastodon/mastodon.go b/internal/platforms/mastodon/mastodon.go
index 42700e5..f896290 100644
--- a/internal/platforms/mastodon/mastodon.go
+++ b/internal/platforms/mastodon/mastodon.go
@@ -9,12 +9,15 @@ import (
"io"
"log"
"net/http"
+ "time"
"codeberg.org/snonux/gos/internal/config"
"codeberg.org/snonux/gos/internal/entry"
"codeberg.org/snonux/gos/internal/prompt"
)
+const mastodonTimeout = 10 * time.Second
+
func Post(ctx context.Context, args config.Args, sizeLimit int, ent entry.Entry) error {
content, _, err := ent.ContentWithLimit(sizeLimit)
if err != nil {
@@ -38,7 +41,10 @@ func Post(ctx context.Context, args config.Args, sizeLimit int, ent entry.Entry)
}
return err
}
- req, err := http.NewRequestWithContext(ctx, "POST", args.Secrets.MastodonURL, bytes.NewBuffer(payloadBytes))
+
+ newCtx, cancel := context.WithTimeout(ctx, mastodonTimeout)
+ defer cancel()
+ req, err := http.NewRequestWithContext(newCtx, "POST", args.Secrets.MastodonURL, bytes.NewBuffer(payloadBytes))
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}