commit 8d5fb78ccdaffed216401291ebff387c2e483181
parent 745946ce1bd7305edef904461b42d2fa7ebcdccb
Author: MTRNord <mtrnord1@gmail.com>
Date: Sun, 18 Feb 2018 20:21:59 +0100
Implement "GET /papers" Endpoint
Diffstat:
2 files changed, 77 insertions(+), 0 deletions(-)
diff --git a/api/webserver.go b/api/webserver.go
@@ -2,13 +2,18 @@ package api
import (
"encoding/csv"
+ "encoding/json"
"fmt"
+ "github.com/SocialNetworkNews/SocialNetworkNews_API/db"
"github.com/SocialNetworkNews/SocialNetworkNews_API/twitter"
+ "github.com/dgraph-io/badger"
"github.com/gorilla/mux"
+ "github.com/pkg/errors"
"net/http"
"os"
"path/filepath"
"strconv"
+ "strings"
"time"
)
@@ -27,6 +32,76 @@ func Yesterday(w http.ResponseWriter, r *http.Request) {
w.Write(tweets)
}
+type Paper struct {
+ Name string `json:"name,omitempty"`
+ UUID string `json:"uuid,omitempty"`
+ Description string `json:"description,omitempty"`
+ Author `json:",omitempty"`
+}
+
+type Author struct {
+ Username string `json:"username,omitempty"`
+ ProfileIMGURL string `json:"profile_image_url,omitempty"`
+ TwitterProfile string `json:"twitter_profile,omitempty"`
+ GoogleProfile string `json:"google_profile,omitempty"`
+ GithubProfile string `json:"github_profile,omitempty"`
+}
+
+func Papers(w http.ResponseWriter, r *http.Request) {
+ papers, err := getPapers()
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ }
+
+ w.Header().Set("Content-Type", "application/json")
+ w.Header().Set("API-VERSION", "0.0.0")
+ w.WriteHeader(http.StatusOK)
+ w.Write(papers)
+}
+
+func getPapers() ([]byte, error) {
+ papersDB, openErr := db.OpenDB()
+ if openErr != nil {
+ return nil, openErr
+ }
+
+ var papers []Paper
+
+ papersDB.View(func(txn *badger.Txn) error {
+ opts := badger.DefaultIteratorOptions
+ opts.PrefetchSize = 10
+ it := txn.NewIterator(opts)
+ prefix := []byte("papers|paper|")
+
+ for it.Seek(prefix); it.ValidForPrefix(prefix); it.Next() {
+ item := it.Item()
+ key := item.Key()
+ stringKey := fmt.Sprintf("%s", key)
+ stringKeySlice := strings.Split(stringKey, "|")
+ stringKeyEnd := stringKeySlice[len(stringKeySlice)-1]
+ paper := Paper{}
+ paper.UUID = stringKeyEnd
+
+ nameResult, QueryErr := db.Get(txn, []byte(fmt.Sprintf("%s|name", stringKeyEnd)))
+ if QueryErr != nil {
+ return errors.WithMessage(QueryErr, fmt.Sprintf("Key: %s|name", stringKeyEnd))
+ }
+
+ paper.Name = fmt.Sprintf("%s", nameResult)
+
+ papers = append(papers, paper)
+ }
+ return nil
+ })
+
+ papersArray, err := json.Marshal(papers)
+ if err != nil {
+ return nil, err
+ }
+
+ return papersArray, nil
+}
+
func getTweets() ([]byte, error) {
api := twitter.NewTwitterAPIStruct()
diff --git a/main.go b/main.go
@@ -22,6 +22,8 @@ func main() {
fmt.Println("Logged in!")
r := mux.NewRouter()
+ r.HandleFunc("/papers", web_api.Papers).Methods("GET")
+
p := r.PathPrefix("/paper/{uuid}").Subrouter()
p.HandleFunc("/yesterday", web_api.Yesterday).Methods("GET")
// cors.Default() setup the middleware with default options being