1
0
mirror of https://github.com/mrusme/neonmodem.git synced 2024-09-22 04:35:55 -04:00

Fixed API requests, enhanced posts implementation

This commit is contained in:
マリウス 2022-12-29 23:02:19 -05:00
parent 28af26cc07
commit d203f0a112
No known key found for this signature in database
GPG Key ID: 272ED814BF63261F
5 changed files with 74 additions and 68 deletions

View File

@ -80,8 +80,8 @@ var rootCmd = &cobra.Command{
_ = loadSystems(&c) // TODO: Handle errs _ = loadSystems(&c) // TODO: Handle errs
posts, err := (*c.Systems[0]).ListPosts() posts, err := (*c.Systems[0]).ListPosts()
fmt.Println("-----------------------")
fmt.Printf("%v\n", posts) fmt.Printf("%v\n", posts)
fmt.Printf("%s\n", err)
os.Exit(0) os.Exit(0)
tui := tea.NewProgram(ui.NewModel(&c), tea.WithAltScreen()) tui := tea.NewProgram(ui.NewModel(&c), tea.WithAltScreen())

View File

@ -5,7 +5,6 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
@ -110,7 +109,6 @@ func NewClient(cc *ClientConfig) *Client {
c.httpClient.HTTPClient = cc.HTTPClient c.httpClient.HTTPClient = cc.HTTPClient
c.endpoint, _ = url.Parse(cc.Endpoint) c.endpoint, _ = url.Parse(cc.Endpoint)
c.credentials = cc.Credentials c.credentials = cc.Credentials
fmt.Printf("%v\n", c.credentials)
c.Posts = &PostServiceHandler{client: c} c.Posts = &PostServiceHandler{client: c}
@ -149,10 +147,9 @@ func (c *Client) NewRequest(
req.Header.Add("User-Agent", "gobbs") req.Header.Add("User-Agent", "gobbs")
req.Header.Add("Accept", "application/json") req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json") req.Header.Add("Content-Type", "application/json")
req.Header.Add("Api-Username", c.credentials["username"]) req.Header.Add("User-Api-Client-Id", c.credentials["client_id"])
req.Header.Add("Api-Key", c.credentials["key"]) req.Header.Add("User-Api-Key", c.credentials["key"])
fmt.Printf("%v\n", req)
return req, nil return req, nil
} }
@ -186,8 +183,6 @@ func (c *Client) Do(
} }
} }
fmt.Printf("%v\n", res)
if res.StatusCode < http.StatusOK || if res.StatusCode < http.StatusOK ||
res.StatusCode > http.StatusNoContent { res.StatusCode > http.StatusNoContent {
return &RequestError{ return &RequestError{

View File

@ -2,7 +2,6 @@ package discourse
import ( import (
"bufio" "bufio"
"context"
"crypto/rand" "crypto/rand"
"crypto/rsa" "crypto/rsa"
"crypto/x509" "crypto/x509"
@ -10,13 +9,11 @@ import (
"encoding/json" "encoding/json"
"encoding/pem" "encoding/pem"
"fmt" "fmt"
"net/http"
"net/url" "net/url"
"os" "os"
"strings" "strings"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/mrusme/gobbs/models/post"
"github.com/pkg/browser" "github.com/pkg/browser"
) )
@ -79,7 +76,7 @@ func (sys *System) Connect(sysURL string) error {
values := url.Values{} values := url.Values{}
values.Set("application_name", "gobbs") values.Set("application_name", "gobbs")
values.Set("client_id", clientID) values.Set("client_id", clientID)
values.Set("scopes", "read,write,notifications") values.Set("scopes", "read,write")
values.Set("public_key", publicKeyPEM) values.Set("public_key", publicKeyPEM)
values.Set("nonce", nonce) values.Set("nonce", nonce)
@ -108,6 +105,7 @@ func (sys *System) Connect(sysURL string) error {
if err != nil { if err != nil {
return err return err
} }
fmt.Println(string(decodedUserAPIKey))
decryptedUserAPIKey, err := privateKey.Decrypt( decryptedUserAPIKey, err := privateKey.Decrypt(
rand.Reader, rand.Reader,
@ -117,18 +115,21 @@ func (sys *System) Connect(sysURL string) error {
if err != nil { if err != nil {
return err return err
} }
fmt.Println(string(decryptedUserAPIKey))
var userAPIKey UserAPIKey var userAPIKey UserAPIKey
err = json.Unmarshal(decryptedUserAPIKey, &userAPIKey) err = json.Unmarshal(decryptedUserAPIKey, &userAPIKey)
if err != nil { if err != nil {
return err return err
} }
fmt.Printf("%v\n", userAPIKey)
// Credentials // Credentials
credentials := make(map[string]string) credentials := make(map[string]string)
credentials["pk"] = privateKeyPEM credentials["pk"] = privateKeyPEM
credentials["username"] = username credentials["username"] = username
credentials["key"] = userAPIKey.Key credentials["key"] = userAPIKey.Key
credentials["client_id"] = clientID
if sys.config == nil { if sys.config == nil {
sys.config = make(map[string]interface{}) sys.config = make(map[string]interface{})
@ -138,25 +139,3 @@ func (sys *System) Connect(sysURL string) error {
return nil return nil
} }
func (sys *System) ListPosts() ([]post.Post, error) {
credentials := make(map[string]string)
for k, v := range (sys.config["credentials"]).(map[string]interface{}) {
credentials[k] = v.(string)
}
c := NewClient(&ClientConfig{
Endpoint: sys.config["url"].(string),
Credentials: credentials,
HTTPClient: http.DefaultClient,
Logger: sys.logger,
})
posts, err := c.Posts.List(context.Background())
if err != nil {
return []post.Post{}, err
}
fmt.Printf("%v\n", posts)
return []post.Post{}, nil
}

View File

@ -1,6 +1,10 @@
package discourse package discourse
import ( import (
"context"
"net/http"
"github.com/mrusme/gobbs/models/post"
"github.com/mrusme/gobbs/system/adapter" "github.com/mrusme/gobbs/system/adapter"
"go.uber.org/zap" "go.uber.org/zap"
) )
@ -45,3 +49,31 @@ func (sys *System) GetCapabilities() []adapter.Capability {
return caps return caps
} }
func (sys *System) ListPosts() ([]post.Post, error) {
credentials := make(map[string]string)
for k, v := range (sys.config["credentials"]).(map[string]interface{}) {
credentials[k] = v.(string)
}
c := NewClient(&ClientConfig{
Endpoint: sys.config["url"].(string),
Credentials: credentials,
HTTPClient: http.DefaultClient,
Logger: sys.logger,
})
posts, err := c.Posts.List(context.Background())
if err != nil {
return []post.Post{}, err
}
var mPosts []post.Post
for _, p := range posts {
mPosts = append(mPosts, post.Post{
ID: string(p.ID),
Subject: p.TopicTitle,
})
}
return mPosts, nil
}

View File

@ -8,40 +8,40 @@ import (
const PostsBaseURL = "/posts" const PostsBaseURL = "/posts"
type PostModel struct { type PostModel struct {
ID string `json:"id"` ID int `json:"id"`
Name string `json:"name"` Name string `json:"name"`
Username string `json:"username"` Username string `json:"username"`
AvatarTemplate string `json:"avater_template"` AvatarTemplate string `json:"avater_template"`
CreatedAt string `json:"created_at"` CreatedAt string `json:"created_at"`
Cooked string `json:"cooked"` Cooked string `json:"cooked"`
PostNumber int `json:"post_number"` PostNumber int `json:"post_number"`
PostType int `json:"post_type"` PostType int `json:"post_type"`
UpdatedAt string `json:"updated_at"` UpdatedAt string `json:"updated_at"`
ReplyCount int `json:"reply_count"` ReplyCount int `json:"reply_count"`
IncomingLinkCount int `json:"incoming_link_count"` IncomingLinkCount int `json:"incoming_link_count"`
Reads int `json:"reads"` Reads int `json:"reads"`
ReadersCount int `json:"readers_count"` ReadersCount int `json:"readers_count"`
Score int `json:"score"` Score float32 `json:"score"`
Yours bool `json:"yours"` Yours bool `json:"yours"`
TopicID int `json:"topic_id"` TopicID int `json:"topic_id"`
TopicSlug string `json:"topic_slug"` TopicSlug string `json:"topic_slug"`
TopicTitle string `json:"topic_title"` TopicTitle string `json:"topic_title"`
TopicHTMLTitle string `json:"topic_html_title"` TopicHTMLTitle string `json:"topic_html_title"`
CategoryID int `json:"category_id"` CategoryID int `json:"category_id"`
DisplayUsername string `json:"display_username"` DisplayUsername string `json:"display_username"`
PrimaryGroupName string `json:"primary_group_name"` PrimaryGroupName string `json:"primary_group_name"`
FlairName string `json:"flair_name"` FlairName string `json:"flair_name"`
FlairURL string `json:"flair_url"` FlairURL string `json:"flair_url"`
FlairBGColor string `json:"flair_bg_color"` FlairBGColor string `json:"flair_bg_color"`
FlairColor string `json:"flair_color"` FlairColor string `json:"flair_color"`
Version int `json:"version"` Version int `json:"version"`
CanEdit bool `json:"can_edit"` CanEdit bool `json:"can_edit"`
CanDelete bool `json:"can_delete"` CanDelete bool `json:"can_delete"`
CanRecover bool `json:"can_recover"` CanRecover bool `json:"can_recover"`
CanWiki bool `json:"can_wiki"` CanWiki bool `json:"can_wiki"`
UserTitle string `json:"user_title"` UserTitle string `json:"user_title"`
Raw string `json:"raw"` Raw string `json:"raw"`
ActionsSummary struct { ActionsSummary []struct {
ID int `json:"id"` ID int `json:"id"`
CanAct bool `json:"can_act"` CanAct bool `json:"can_act"`
} `json:"actions_summary"` } `json:"actions_summary"`