Skip to content

Commit d2d00db

Browse files
committed
refactor: move request sending to new function
1 parent e0498d8 commit d2d00db

1 file changed

Lines changed: 35 additions & 109 deletions

File tree

bluesky/blueskyapi.go

Lines changed: 35 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,24 @@ type ItemByWithDate struct {
208208
Actor Author `json:"actor"`
209209
}
210210

211+
func SendRequest(token *string, method string, url string, body io.Reader) (*http.Response, error) {
212+
client := &http.Client{}
213+
req, err := http.NewRequest(method, url, body)
214+
if err != nil {
215+
return nil, err
216+
}
217+
if token != nil {
218+
req.Header.Set("Authorization", "Bearer "+*token)
219+
}
220+
221+
resp, err := client.Do(req)
222+
if err != nil {
223+
return nil, err
224+
}
225+
226+
return resp, nil
227+
}
228+
211229
func Authenticate(username, password string) (*AuthResponse, error) {
212230
url := "https://bsky.social/xrpc/com.atproto.server.createSession"
213231

@@ -221,7 +239,7 @@ func Authenticate(username, password string) (*AuthResponse, error) {
221239
return nil, err
222240
}
223241

224-
resp, err := http.Post(url, "application/json", bytes.NewBuffer(reqBody))
242+
resp, err := SendRequest(nil, http.MethodPost, url, bytes.NewBuffer(reqBody))
225243
if err != nil {
226244
return nil, err
227245
}
@@ -247,15 +265,7 @@ func Authenticate(username, password string) (*AuthResponse, error) {
247265
func RefreshToken(refreshToken string) (*AuthResponse, error) {
248266
url := "https://bsky.social/xrpc/com.atproto.server.refreshSession"
249267

250-
client := &http.Client{}
251-
252-
req, err := http.NewRequest(http.MethodPost, url, nil)
253-
if err != nil {
254-
return nil, err
255-
}
256-
req.Header.Set("Authorization", "Bearer "+refreshToken)
257-
258-
resp, err := client.Do(req)
268+
resp, err := SendRequest(&refreshToken, http.MethodPost, url, nil)
259269
if err != nil {
260270
return nil, err
261271
}
@@ -280,16 +290,7 @@ func RefreshToken(refreshToken string) (*AuthResponse, error) {
280290
func GetUserInfo(token string, screen_name string) (*bridge.TwitterUser, error) {
281291
url := "https://public.api.bsky.app/xrpc/app.bsky.actor.getProfile" + "?actor=" + screen_name
282292

283-
client := &http.Client{}
284-
req, err := http.NewRequest(http.MethodGet, url, nil)
285-
if err != nil {
286-
return nil, err
287-
}
288-
if token != "" {
289-
req.Header.Set("Authorization", "Bearer "+token)
290-
}
291-
292-
resp, err := client.Do(req)
293+
resp, err := SendRequest(&token, http.MethodGet, url, nil)
293294
if err != nil {
294295
return nil, err
295296
}
@@ -313,16 +314,7 @@ func GetUserInfo(token string, screen_name string) (*bridge.TwitterUser, error)
313314
func GetUsersInfo(token string, items []string) ([]*bridge.TwitterUser, error) {
314315
url := "https://public.api.bsky.app/xrpc/app.bsky.actor.getProfiles" + "?actors=" + strings.Join(items, "&actors=")
315316

316-
client := &http.Client{}
317-
req, err := http.NewRequest(http.MethodGet, url, nil)
318-
if err != nil {
319-
return nil, err
320-
}
321-
if token != "" {
322-
req.Header.Set("Authorization", "Bearer "+token)
323-
}
324-
325-
resp, err := client.Do(req)
317+
resp, err := SendRequest(&token, http.MethodGet, url, nil)
326318
if err != nil {
327319
return nil, err
328320
}
@@ -395,14 +387,7 @@ func GetTimeline(token string, context string) (error, *Timeline) {
395387
url = "https://public.bsky.social/xrpc/app.bsky.feed.getTimeline?context=" + context
396388
}
397389

398-
client := &http.Client{}
399-
req, err := http.NewRequest(http.MethodGet, url, nil)
400-
if err != nil {
401-
return err, nil
402-
}
403-
req.Header.Set("Authorization", "Bearer "+token)
404-
405-
resp, err := client.Do(req)
390+
resp, err := SendRequest(&token, http.MethodGet, url, nil)
406391
if err != nil {
407392
return err, nil
408393
}
@@ -434,14 +419,7 @@ func GetPost(token string, uri string, depth int, parentHeight int) (error, *Thr
434419

435420
url := "https://public.bsky.social/xrpc/app.bsky.feed.getPostThread?depth=" + fmt.Sprintf("%d", depth) + "&parentHeight=" + fmt.Sprintf("%d", parentHeight) + "&uri=" + uri
436421

437-
client := &http.Client{}
438-
req, err := http.NewRequest(http.MethodGet, url, nil)
439-
if err != nil {
440-
return err, nil
441-
}
442-
req.Header.Set("Authorization", "Bearer "+token)
443-
444-
resp, err := client.Do(req)
422+
resp, err := SendRequest(&token, http.MethodGet, url, nil)
445423
if err != nil {
446424
return err, nil
447425
}
@@ -471,18 +449,12 @@ func GetPost(token string, uri string, depth int, parentHeight int) (error, *Thr
471449
func UpdateStatus(token string, status string) error {
472450
url := "https://public.bsky.social/xrpc/com.atproto.repo.createRecord"
473451

474-
client := &http.Client{}
475-
req, err := http.NewRequest(http.MethodPost, url, nil)
476-
if err != nil {
477-
return err
478-
}
479-
req.Header.Set("Authorization", "Bearer "+token)
480-
481-
resp, err := client.Do(req)
452+
resp, err := SendRequest(&token, http.MethodPost, url, nil)
482453
if err != nil {
483454
return err
484455
}
485456
defer resp.Body.Close()
457+
486458
if resp.StatusCode != http.StatusOK {
487459
bodyBytes, _ := io.ReadAll(resp.Body)
488460
bodyString := string(bodyBytes)
@@ -497,16 +469,10 @@ func ReTweet(token string, id string, my_did string) (error, *ThreadRoot, *strin
497469
url := "https://bsky.social/xrpc/com.atproto.repo.createRecord"
498470

499471
err, thread := GetPost(token, id, 0, 1)
500-
501472
if err != nil {
502473
return errors.New("failed to fetch post"), nil, nil
503474
}
504475

505-
client := &http.Client{}
506-
req, err := http.NewRequest(http.MethodPost, url, nil)
507-
if err != nil {
508-
return err, nil, nil
509-
}
510476
payload := CreateRecordPayload{
511477
Collection: "app.bsky.feed.repost",
512478
Repo: my_did,
@@ -525,13 +491,7 @@ func ReTweet(token string, id string, my_did string) (error, *ThreadRoot, *strin
525491
return errors.New("failed to marshal payload"), nil, nil
526492
}
527493

528-
req.Body = io.NopCloser(bytes.NewReader(reqBody))
529-
req.Header.Set("Authorization", "Bearer "+token)
530-
req.Header.Set("Content-Type", "application/json")
531-
532-
// if it works, we should get something like:
533-
// {"uri":"at://did:plc:khcyntihpu7snjszuojjgjc4/app.bsky.feed.repost/3lcm7b2pjio22","cid":"bafyreidw2uvnhns5bacdii7gozrou4rg25cpcxhe6cbhfws2c5hpsvycdm","commit":{"cid":"bafyreicu7db6k3vxbvtwiumggynbps7cuozsofbvo3kq7lz723smvpxne4","rev":"3lcm7b2ptb622"},"validationStatus":"valid"}
534-
resp, err := client.Do(req)
494+
resp, err := SendRequest(&token, http.MethodPost, url, bytes.NewReader(reqBody))
535495
if err != nil {
536496
return err, nil, nil
537497
}
@@ -557,16 +517,10 @@ func LikePost(token string, id string, my_did string) (error, *ThreadRoot) {
557517
url := "https://bsky.social/xrpc/com.atproto.repo.createRecord"
558518

559519
err, thread := GetPost(token, id, 0, 1)
560-
561520
if err != nil {
562521
return errors.New("failed to fetch post"), nil
563522
}
564523

565-
client := &http.Client{}
566-
req, err := http.NewRequest(http.MethodPost, url, nil)
567-
if err != nil {
568-
return err, nil
569-
}
570524
payload := CreateRecordPayload{
571525
Collection: "app.bsky.feed.like",
572526
Repo: my_did,
@@ -585,13 +539,7 @@ func LikePost(token string, id string, my_did string) (error, *ThreadRoot) {
585539
return errors.New("failed to marshal payload"), nil
586540
}
587541

588-
req.Body = io.NopCloser(bytes.NewReader(reqBody))
589-
req.Header.Set("Authorization", "Bearer "+token)
590-
req.Header.Set("Content-Type", "application/json")
591-
592-
// if it works, we should get something like:
593-
// {"uri":"at://did:plc:khcyntihpu7snjszuojjgjc4/app.bsky.feed.repost/3lcm7b2pjio22","cid":"bafyreidw2uvnhns5bacdii7gozrou4rg25cpcxhe6cbhfws2c5hpsvycdm","commit":{"cid":"bafyreicu7db6k3vxbvtwiumggynbps7cuozsofbvo3kq7lz723smvpxne4","rev":"3lcm7b2ptb622"},"validationStatus":"valid"}
594-
resp, err := client.Do(req)
542+
resp, err := SendRequest(&token, http.MethodPost, url, bytes.NewReader(reqBody))
595543
if err != nil {
596544
return err, nil
597545
}
@@ -619,16 +567,10 @@ func UnlikePost(token string, id string, my_did string) (error, *ThreadRoot) {
619567
url := "https://bsky.social/xrpc/com.atproto.repo.deleteRecord"
620568

621569
err, thread := GetPost(token, id, 0, 1)
622-
623570
if err != nil {
624571
return errors.New("failed to fetch post"), nil
625572
}
626573

627-
client := &http.Client{}
628-
req, err := http.NewRequest(http.MethodPost, url, nil)
629-
if err != nil {
630-
return err, nil
631-
}
632574
payload := DeleteRecordPayload{
633575
Collection: "app.bsky.feed.like",
634576
Repo: my_did,
@@ -640,13 +582,7 @@ func UnlikePost(token string, id string, my_did string) (error, *ThreadRoot) {
640582
return errors.New("failed to marshal payload"), nil
641583
}
642584

643-
req.Body = io.NopCloser(bytes.NewReader(reqBody))
644-
req.Header.Set("Authorization", "Bearer "+token)
645-
req.Header.Set("Content-Type", "application/json")
646-
647-
// if it works, we should get something like:
648-
// {"uri":"at://did:plc:khcyntihpu7snjszuojjgjc4/app.bsky.feed.repost/3lcm7b2pjio22","cid":"bafyreidw2uvnhns5bacdii7gozrou4rg25cpcxhe6cbhfws2c5hpsvycdm","commit":{"cid":"bafyreicu7db6k3vxbvtwiumggynbps7cuozsofbvo3kq7lz723smvpxne4","rev":"3lcm7b2ptb622"},"validationStatus":"valid"}
649-
resp, err := client.Do(req)
585+
resp, err := SendRequest(&token, http.MethodPost, url, bytes.NewReader(reqBody))
650586
if err != nil {
651587
return err, nil
652588
}
@@ -673,14 +609,7 @@ func UnlikePost(token string, id string, my_did string) (error, *ThreadRoot) {
673609
func GetLikes(token string, uri string, limit int) (*Likes, error) {
674610
url := fmt.Sprintf("https://public.bsky.social/xrpc/app.bsky.feed.getLikes?limit=%d&uri=%s", limit, uri)
675611

676-
client := &http.Client{}
677-
req, err := http.NewRequest(http.MethodGet, url, nil)
678-
if err != nil {
679-
return nil, err
680-
}
681-
req.Header.Set("Authorization", "Bearer "+token)
682-
683-
resp, err := client.Do(req)
612+
resp, err := SendRequest(&token, http.MethodGet, url, nil)
684613
if err != nil {
685614
return nil, err
686615
}
@@ -710,14 +639,7 @@ func GetLikes(token string, uri string, limit int) (*Likes, error) {
710639
func GetRetweetAuthors(token string, uri string, limit int) (*RepostedBy, error) {
711640
url := fmt.Sprintf("https://public.bsky.social/xrpc/app.bsky.feed.getRepostedBy?limit=%d&uri=%s", limit, uri)
712641

713-
client := &http.Client{}
714-
req, err := http.NewRequest(http.MethodGet, url, nil)
715-
if err != nil {
716-
return nil, err
717-
}
718-
req.Header.Set("Authorization", "Bearer "+token)
719-
720-
resp, err := client.Do(req)
642+
resp, err := SendRequest(&token, http.MethodGet, url, nil)
721643
if err != nil {
722644
return nil, err
723645
}
@@ -743,3 +665,7 @@ func GetRetweetAuthors(token string, uri string, limit int) (*RepostedBy, error)
743665

744666
return &retweetAuthors, nil
745667
}
668+
669+
// func UserSearch(token string, query string) (bridge.TwitterUser, error) {
670+
671+
// }

0 commit comments

Comments
 (0)