Skip to content

Instantly share code, notes, and snippets.

@anargu
Created July 20, 2021 13:20
Show Gist options
  • Save anargu/6234700fc83c6dcdc7535d3d7de30da3 to your computer and use it in GitHub Desktop.
Save anargu/6234700fc83c6dcdc7535d3d7de30da3 to your computer and use it in GitHub Desktop.
PostComments API into CSV
package main
import (
"io/ioutil"
"net/http"
"encoding/json"
"encoding/csv"
"strings"
"fmt"
"strconv"
"os"
"log"
)
var (
postAPI = "https://jsonplaceholder.typicode.com/posts"
commentsAPI = "https://jsonplaceholder.typicode.com/comments"
)
type Post struct {
UserID int `json:"userId"`
ID int
Title string
Body string
Comments string
}
type Comment struct {
PostId int
ID int
Body string
}
func getHttpApiResponse(api string) ([]byte, error) {
resp, err := http.Get(api)
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}
func parseCommentApiResponse(rawData []byte) ([]Comment, error) {
var comments []Comment
err := json.Unmarshal(rawData, &comments)
if err != nil {
return nil, err
}
return comments, err
}
func parsePostApiResponse(rawData []byte) ([]Post, error) {
var posts []Post
err := json.Unmarshal(rawData, &posts)
if err != nil {
return nil, err
}
return posts, err
}
func main() {
rawPosts, err := getHttpApiResponse(postAPI)
if err != nil {
panic(err)
}
rawComments, err := getHttpApiResponse(commentsAPI)
if err != nil {
panic(err)
}
posts, err := parsePostApiResponse(rawPosts)
comments, err := parseCommentApiResponse(rawComments)
for i := 0; i < len(posts); i++ {
post := posts[i]
filteredComments := []string{}
for i := 0; i < len(comments); i++ {
comment := comments[i]
if comment.PostId == post.ID {
filteredComments = append(filteredComments, comment.Body)
}
}
posts[i].Comments = strings.Join(filteredComments, " | ")
// fmt.Println("---")
}
// fmt.Printf("POSTS\n%v\n\n\n", posts)
csvFile, err := os.Create("posts.csv")
if err != nil {
log.Fatalf("failed creating file: %s", err)
}
csvwriter := csv.NewWriter(csvFile)
// postsToCSV = [][]string{}
for _, post := range posts {
postId := strconv.Itoa(post.ID)
// postTitle := strconv.Itoa(post.Title)
// postsToCSV := append(postsToCSV, []string{postId, postTitle, post.Comments})
csvPost := []string{postId,post.Body, post.Title, post.Comments}
// rowcsv := fmt.Sprintf("%v,%v,%v", postId, post.Title, post.Comments)
_ = csvwriter.Write(csvPost)
}
csvwriter.Flush()
csvFile.Close()
fmt.Printf("END") //%v",posts)
}
package main
import (
"testing"
"fmt"
)
func TestHttpClientCall(t *testing.T) {
resp, err := getHttpApiResponse(postAPI)
if err != nil {
t.Fail()
}
fmt.Printf("%v", resp)
}
func TestParseResponse(t *testing.T) {
rawData, _ := getHttpApiResponse(postAPI)
resp, err := parsePostApiResponse(rawData)
if err != nil {
t.Fail()
}
fmt.Printf("POST=====\n%v\n", resp)
rawData, _ = getHttpApiResponse(commentsAPI)
resp, err = parseCommentApiResponse(rawData)
if err != nil {
t.Fail()
}
fmt.Printf("COMMENT=====\n%v\n", resp)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment