summaryrefslogtreecommitdiff
path: root/internal/platforms/linkedin
diff options
context:
space:
mode:
Diffstat (limited to 'internal/platforms/linkedin')
-rw-r--r--internal/platforms/linkedin/linkedin.go40
-rw-r--r--internal/platforms/linkedin/oauth2/oauth2.go12
-rw-r--r--internal/platforms/linkedin/preview.go21
3 files changed, 57 insertions, 16 deletions
diff --git a/internal/platforms/linkedin/linkedin.go b/internal/platforms/linkedin/linkedin.go
index 16688d5..ced242a 100644
--- a/internal/platforms/linkedin/linkedin.go
+++ b/internal/platforms/linkedin/linkedin.go
@@ -116,11 +116,11 @@ func postMessageToLinkedInAPI(ctx context.Context, personID, accessToken, conten
payload, err := json.Marshal(post)
if err != nil {
- return fmt.Errorf("Error encoding JSON:%w", err)
+ return fmt.Errorf("error encoding JSON: %w", err)
}
req, err := http.NewRequestWithContext(ctx, "POST", linkedInPostsURL, bytes.NewBuffer(payload))
if err != nil {
- return fmt.Errorf("Error creating request: %w", err)
+ return fmt.Errorf("error creating request: %w", err)
}
// Use configured LinkedIn version if available
@@ -132,17 +132,21 @@ func postMessageToLinkedInAPI(ctx context.Context, personID, accessToken, conten
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
- return fmt.Errorf("Error sending request: %w", err)
+ return fmt.Errorf("error sending request: %w", err)
}
- defer resp.Body.Close()
+ defer func() {
+ if err := resp.Body.Close(); err != nil {
+ // Log the error but don't fail the operation since we've already read the data
+ colour.Errorln("Error closing response body:", err)
+ }
+ }()
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode != http.StatusCreated {
- err = fmt.Errorf("failed to post to LinkedIn. Status: %s\n%s\n",
- resp.Status, string(body))
+ err = fmt.Errorf("failed to post to LinkedIn. Status: %s: %s", resp.Status, string(body))
if resp.StatusCode == http.StatusUnauthorized {
err = errors.Join(err, errUnauthorized)
} else if resp.StatusCode == http.StatusUpgradeRequired {
@@ -190,7 +194,12 @@ func initializeImageUpload(ctx context.Context, personURN, accessToken string, l
if err != nil {
return "", "", fmt.Errorf("error sending request: %w", err)
}
- defer resp.Body.Close()
+ defer func() {
+ if err := resp.Body.Close(); err != nil {
+ // Log the error but don't fail the operation since we've already read the data
+ colour.Errorln("Error closing response body:", err)
+ }
+ }()
body, err := io.ReadAll(resp.Body)
if err != nil {
@@ -198,8 +207,7 @@ func initializeImageUpload(ctx context.Context, personURN, accessToken string, l
}
if resp.StatusCode != http.StatusOK {
- err := fmt.Errorf("image upload initialization failed. Status: %s\n%s\n",
- resp.Status, string(body))
+ err := fmt.Errorf("image upload initialization failed. Status: %s: %s", resp.Status, string(body))
if resp.StatusCode == http.StatusUnauthorized {
err = errors.Join(err, errUnauthorized)
} else if resp.StatusCode == http.StatusUpgradeRequired {
@@ -229,7 +237,12 @@ func performImageUpload(ctx context.Context, imagePath, uploadURL, accessToken s
if err != nil {
return err
}
- defer file.Close()
+ defer func() {
+ if err := file.Close(); err != nil {
+ // Log the error but don't fail the operation since we've already read the data
+ colour.Errorln("Error closing image file:", err)
+ }
+ }()
imageData, err := io.ReadAll(file)
if err != nil {
@@ -248,7 +261,12 @@ func performImageUpload(ctx context.Context, imagePath, uploadURL, accessToken s
if err != nil {
return fmt.Errorf("error sending upload request: %w", err)
}
- defer resp.Body.Close()
+ defer func() {
+ if err := resp.Body.Close(); err != nil {
+ // Log the error but don't fail the operation since we've already read the data
+ colour.Errorln("Error closing response body:", err)
+ }
+ }()
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
diff --git a/internal/platforms/linkedin/oauth2/oauth2.go b/internal/platforms/linkedin/oauth2/oauth2.go
index 45cb9b7..b4d3bb3 100644
--- a/internal/platforms/linkedin/oauth2/oauth2.go
+++ b/internal/platforms/linkedin/oauth2/oauth2.go
@@ -41,7 +41,12 @@ func getOauthPersonID(token *oauth2.Token) (string, error) {
if err != nil {
return "", fmt.Errorf("Error making the request:%w", err)
}
- defer resp.Body.Close()
+ defer func() {
+ if err := resp.Body.Close(); err != nil {
+ // Log the error but don't fail the operation since we've already read the data
+ colour.Errorln("Error closing response body:", err)
+ }
+ }()
body, _ := io.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
@@ -171,7 +176,10 @@ func WaitUntilURLIsReachable(url string) error {
colour.Infofln("URL is not reachable: %v", err)
} else {
colour.Infofln("URL is reachable: %s - Status Code: %d", url, resp.StatusCode)
- resp.Body.Close()
+ if err := resp.Body.Close(); err != nil {
+ // Log the error but don't fail the operation since we've already read the data
+ colour.Errorln("Error closing response body:", err)
+ }
return nil
}
}
diff --git a/internal/platforms/linkedin/preview.go b/internal/platforms/linkedin/preview.go
index b7387c9..54c24ba 100644
--- a/internal/platforms/linkedin/preview.go
+++ b/internal/platforms/linkedin/preview.go
@@ -89,7 +89,12 @@ func (p preview) DownloadImage(destPath string) (string, error) {
if err != nil {
return "", err
}
- defer resp.Body.Close()
+ defer func() {
+ if err := resp.Body.Close(); err != nil {
+ // Log the error but don't fail the operation since we've already read the data
+ colour.Errorln("Error closing response body:", err)
+ }
+ }()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("bad status while trying to download image: %s", resp.Status)
@@ -100,7 +105,12 @@ func (p preview) DownloadImage(destPath string) (string, error) {
if err != nil {
return destFile, fmt.Errorf("%s: %w", destFile, err)
}
- defer out.Close()
+ defer func() {
+ if err := out.Close(); err != nil {
+ // Log the error but don't fail the operation since we've already written the data
+ colour.Errorln("Error closing output file:", err)
+ }
+ }()
_, err = io.Copy(out, resp.Body)
if err != nil {
@@ -177,7 +187,12 @@ func extractFromURL(ctx context.Context, url string) (string, string, error) {
if err != nil {
return "", "", fmt.Errorf("failed to get URL: %w", err)
}
- defer resp.Body.Close()
+ defer func() {
+ if err := resp.Body.Close(); err != nil {
+ // Log the error but don't fail the operation since we've already read the data
+ colour.Errorln("Error closing response body:", err)
+ }
+ }()
if resp.StatusCode != http.StatusOK {
return "", "", fmt.Errorf("failed to get a successful response: %v", resp.StatusCode)