-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
73 additions
and
19 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
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 | ||
} |
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,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 | ||
} |