-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from Hashnode/scriptonist/get-all-responses
Fix to show all responses
- Loading branch information
Showing
4 changed files
with
135 additions
and
32 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
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
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,93 @@ | ||
package posts | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"net/url" | ||
|
||
"github.com/rivo/tview" | ||
) | ||
|
||
func openResponses(t *tview.TextView, postcuid string, totalResponses int) { | ||
|
||
const defaultSortOrder = "totalReactions" | ||
|
||
r, err := getResponses(postcuid, totalResponses, 1, defaultSortOrder) | ||
if err != nil { | ||
log.Println(err) | ||
} | ||
var responsesAPI responsesAPI | ||
err = json.Unmarshal(r, &responsesAPI) | ||
if err != nil { | ||
log.Println(err) | ||
} | ||
|
||
noresponse := len(responsesAPI.Responses) | ||
for ind, response := range responsesAPI.Responses { | ||
writeToTextView( | ||
t, | ||
fmt.Sprintf("\n[green]Response %d/%d[white]", ind+1, noresponse), | ||
fmt.Sprintf("[green]--------------[green]"), | ||
renderTerminal(response.ContentMarkdown), | ||
) | ||
if len(response.Replies) > 0 { | ||
writeToTextView(t, | ||
"\n[yellow]Replies[white]", | ||
"[yellow]=======[white]", | ||
) | ||
noreplies := len(response.Replies) | ||
for indreply, reply := range response.Replies { | ||
writeToTextView( | ||
t, | ||
fmt.Sprintf("\n[yellow]Reply %d/%d[white]", indreply+1, noreplies), | ||
fmt.Sprintf("[yellow]~~~~~~~~~~~[white]"), | ||
fmt.Sprintf("Author: %s", reply.Author.Name), | ||
indentMarkdown(renderTerminal(reply.ContentMarkdown), "\t"), | ||
) | ||
|
||
} | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
func getResponses(postID string, perPage, page int, sortOrder string) ([]byte, error) { | ||
const apiURL = "https://hashnode.com/ajax/responses" | ||
u, err := url.Parse(apiURL) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
q := u.Query() | ||
q.Set("post_id", postID) | ||
q.Set("page", fmt.Sprintf("%d", page)) | ||
q.Set("per_page", fmt.Sprintf("%d", perPage)) | ||
q.Set("sort_order", sortOrder) | ||
u.RawQuery = q.Encode() | ||
|
||
client := getHttpClient() | ||
resp, err := client.Get(u.String()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
b, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return b, err | ||
} | ||
|
||
type responsesAPI struct { | ||
Pagination struct { | ||
Page string `json:"page"` | ||
PerPage string `json:"per_page"` | ||
Total int `json:"total"` | ||
} `json:"pagination"` | ||
Order string `json:"order"` | ||
Responses []Response | ||
} |
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,31 @@ | ||
package posts | ||
|
||
import "testing" | ||
|
||
func Test_getResponses(t *testing.T) { | ||
type args struct { | ||
postID string | ||
perPage int | ||
page int | ||
sortOrder string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
}{ | ||
{ | ||
name: "test1", | ||
args: args{ | ||
postID: "5c34ffe75aa5738b323c386b", | ||
page: 2, | ||
perPage: 5, | ||
sortOrder: "totalReactions", | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
getResponses(tt.args.postID, tt.args.perPage, tt.args.page, tt.args.sortOrder) | ||
}) | ||
} | ||
} |