Skip to content

Commit

Permalink
Recent posts GET implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
drags committed Oct 7, 2019
1 parent 8b17702 commit 0f5644a
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 19 deletions.
40 changes: 21 additions & 19 deletions pinboard.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
package pinboard

import "fmt"
import "time"
import (
"fmt"
"log"
"net/http"
"net/url"
)

type Pinboard struct {
user string
token string
User string
Token string
}

type Pin struct {
url string
description string
hash string
tags []string
date time.Time
func (p *Pinboard) AuthQuery() string {
return fmt.Sprintf("%s:%s", p.User, p.Token)
}

func (p *Pinboard) GetPins() []Pin {
fmt.Println("hi")
Pins := make([]Pin, 3)
return Pins
}

func (p *Pinboard) GetAllPins() []Pin {
Pins := make([]Pin, 3)
return Pins
func (p *Pinboard) Get(uri string) (*http.Response, error) {
u, err := url.Parse(uri)
q := u.Query()
q.Set("auth_token", p.AuthQuery())
u.RawQuery = q.Encode()
log.Println("Calling API with URL", u.String())
resp, err := http.Get(u.String())
if err != nil {
return nil, err
}
return resp, err
}
52 changes: 52 additions & 0 deletions posts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package pinboard

import (
"encoding/xml"
"io/ioutil"
"log"
)

type Posts struct {
XMLName xml.Name `xml:"posts"`
User string `xml:"user,attr"`
Date string `xml:"dt,attr"`
Posts []Post `xml:"post"`
}

type Post struct {
XMLName xml.Name `xml:"post"`
Url string `xml:"href,attr"`
Description string `xml:"description,attr"`
Hash string `xml:"hash,attr"`
Tags string `xml:"tag,attr"`
Extended string `xml:"extended,attr"`
Date string `xml:"time,attr"`
Shared string `xml:"shared,attr"`
}

func (p *Pinboard) GetRecentPosts() ([]Post, error) {
posts := &Posts{}
url := "https://api.pinboard.in/v1/posts/recent"
resp, err := p.Get(url)
if err != nil {
return nil, err
}
resp_body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return nil, err
}
log.Println("Got resp_body length ", len(resp_body))
log.Println(string(resp_body))
err = xml.Unmarshal(resp_body, &posts)
if err != nil {
return nil, err
}
log.Println("Got user ", posts.User)
return posts.Posts, err
}

func (p *Pinboard) GetAllPosts() []Post {
posts := make([]Post, 3)
return posts
}

0 comments on commit 0f5644a

Please sign in to comment.