Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
221 changes: 221 additions & 0 deletions internal/backend/demo/dummy_data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
package demo

import (
"context"

"github.com/maniac-en/req/internal/backend/collections"
"github.com/maniac-en/req/internal/backend/endpoints"
"github.com/maniac-en/req/internal/log"
)

type DemoGenerator struct {
collectionsManager *collections.CollectionsManager
endpointsManager *endpoints.EndpointsManager
}

func NewDemoGenerator(collectionsManager *collections.CollectionsManager, endpointsManager *endpoints.EndpointsManager) *DemoGenerator {
return &DemoGenerator{
collectionsManager: collectionsManager,
endpointsManager: endpointsManager,
}
}

func (d *DemoGenerator) PopulateDummyData(ctx context.Context) (bool, error) {
log.Info("populating dummy data for demo")

// Check if we already have collections
result, err := d.collectionsManager.ListPaginated(ctx, 1, 0)
if err != nil {
log.Error("failed to check existing collections", "error", err)
return false, err
}

if len(result.Collections) > 0 {
log.Debug("dummy data already exists, skipping population", "collections_count", len(result.Collections))
return false, nil
}

// Create demo collections and endpoints
if err := d.createJSONPlaceholderCollection(ctx); err != nil {
return false, err
}

if err := d.createReqresCollection(ctx); err != nil {
return false, err
}

if err := d.createHTTPBinCollection(ctx); err != nil {
return false, err
}

log.Info("dummy data populated successfully")
return true, nil
}

func (d *DemoGenerator) createJSONPlaceholderCollection(ctx context.Context) error {
collection, err := d.collectionsManager.Create(ctx, "JSONPlaceholder API")
if err != nil {
log.Error("failed to create JSONPlaceholder collection", "error", err)
return err
}

endpoints := []endpoints.EndpointData{
{
CollectionID: collection.ID,
Name: "Get All Posts",
Method: "GET",
URL: "https://jsonplaceholder.typicode.com/posts",
Headers: `{"Content-Type": "application/json"}`,
QueryParams: map[string]string{},
RequestBody: "",
},
{
CollectionID: collection.ID,
Name: "Get Single Post",
Method: "GET",
URL: "https://jsonplaceholder.typicode.com/posts/1",
Headers: `{"Content-Type": "application/json"}`,
QueryParams: map[string]string{},
RequestBody: "",
},
{
CollectionID: collection.ID,
Name: "Create Post",
Method: "POST",
URL: "https://jsonplaceholder.typicode.com/posts",
Headers: `{"Content-Type": "application/json"}`,
QueryParams: map[string]string{},
RequestBody: `{"title": "My New Post", "body": "This is the content of my new post", "userId": 1}`,
},
{
CollectionID: collection.ID,
Name: "Update Post",
Method: "PUT",
URL: "https://jsonplaceholder.typicode.com/posts/1",
Headers: `{"Content-Type": "application/json"}`,
QueryParams: map[string]string{},
RequestBody: `{"id": 1, "title": "Updated Post", "body": "This post has been updated", "userId": 1}`,
},
{
CollectionID: collection.ID,
Name: "Delete Post",
Method: "DELETE",
URL: "https://jsonplaceholder.typicode.com/posts/1",
Headers: `{"Content-Type": "application/json"}`,
QueryParams: map[string]string{},
RequestBody: "",
},
}

return d.createEndpoints(ctx, endpoints)
}

func (d *DemoGenerator) createReqresCollection(ctx context.Context) error {
collection, err := d.collectionsManager.Create(ctx, "ReqRes API")
if err != nil {
log.Error("failed to create ReqRes collection", "error", err)
return err
}

endpoints := []endpoints.EndpointData{
{
CollectionID: collection.ID,
Name: "List Users",
Method: "GET",
URL: "https://reqres.in/api/users",
Headers: `{"Content-Type": "application/json"}`,
QueryParams: map[string]string{"page": "2"},
RequestBody: "",
},
{
CollectionID: collection.ID,
Name: "Single User",
Method: "GET",
URL: "https://reqres.in/api/users/2",
Headers: `{"Content-Type": "application/json"}`,
QueryParams: map[string]string{},
RequestBody: "",
},
{
CollectionID: collection.ID,
Name: "Create User",
Method: "POST",
URL: "https://reqres.in/api/users",
Headers: `{"Content-Type": "application/json"}`,
QueryParams: map[string]string{},
RequestBody: `{"name": "morpheus", "job": "leader"}`,
},
{
CollectionID: collection.ID,
Name: "Login",
Method: "POST",
URL: "https://reqres.in/api/login",
Headers: `{"Content-Type": "application/json"}`,
QueryParams: map[string]string{},
RequestBody: `{"email": "eve.holt@reqres.in", "password": "cityslicka"}`,
},
}

return d.createEndpoints(ctx, endpoints)
}

func (d *DemoGenerator) createHTTPBinCollection(ctx context.Context) error {
collection, err := d.collectionsManager.Create(ctx, "HTTPBin Testing")
if err != nil {
log.Error("failed to create HTTPBin collection", "error", err)
return err
}

endpoints := []endpoints.EndpointData{
{
CollectionID: collection.ID,
Name: "Test GET",
Method: "GET",
URL: "https://httpbin.org/get",
Headers: `{"User-Agent": "Req-Terminal-Client/1.0"}`,
QueryParams: map[string]string{"test": "value", "demo": "true"},
RequestBody: "",
},
{
CollectionID: collection.ID,
Name: "Test POST JSON",
Method: "POST",
URL: "https://httpbin.org/post",
Headers: `{"Content-Type": "application/json", "User-Agent": "Req-Terminal-Client/1.0"}`,
QueryParams: map[string]string{},
RequestBody: `{"message": "Hello from Req!", "timestamp": "2024-01-15T10:30:00Z", "data": {"key": "value"}}`,
},
{
CollectionID: collection.ID,
Name: "Test Headers",
Method: "GET",
URL: "https://httpbin.org/headers",
Headers: `{"Authorization": "Bearer demo-token", "X-Custom-Header": "req-demo"}`,
QueryParams: map[string]string{},
RequestBody: "",
},
{
CollectionID: collection.ID,
Name: "Test Status Codes",
Method: "GET",
URL: "https://httpbin.org/status/200",
Headers: `{"Content-Type": "application/json"}`,
QueryParams: map[string]string{},
RequestBody: "",
},
}

return d.createEndpoints(ctx, endpoints)
}

func (d *DemoGenerator) createEndpoints(ctx context.Context, endpointData []endpoints.EndpointData) error {
for _, data := range endpointData {
_, err := d.endpointsManager.CreateEndpoint(ctx, data)
if err != nil {
log.Error("failed to create endpoint", "name", data.Name, "error", err)
return err
}
log.Debug("created demo endpoint", "name", data.Name, "method", data.Method, "url", data.URL)
}
return nil
}
22 changes: 14 additions & 8 deletions internal/tui/app/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (
)

type Context struct {
Collections *collections.CollectionsManager
Endpoints *endpoints.EndpointsManager
HTTP *http.HTTPManager
History *history.HistoryManager
Collections *collections.CollectionsManager
Endpoints *endpoints.EndpointsManager
HTTP *http.HTTPManager
History *history.HistoryManager
DummyDataCreated bool
}

func NewContext(
Expand All @@ -21,9 +22,14 @@ func NewContext(
history *history.HistoryManager,
) *Context {
return &Context{
Collections: collections,
Endpoints: endpoints,
HTTP: httpManager,
History: history,
Collections: collections,
Endpoints: endpoints,
HTTP: httpManager,
History: history,
DummyDataCreated: false,
}
}

func (c *Context) SetDummyDataCreated(created bool) {
c.DummyDataCreated = created
}
17 changes: 14 additions & 3 deletions internal/tui/app/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,15 @@ type Model struct {
}

func NewModel(ctx *Context) Model {
collectionsView := views.NewCollectionsView(ctx.Collections)
if ctx.DummyDataCreated {
collectionsView.SetDummyDataNotification(true)
}

m := Model{
ctx: ctx,
mode: CollectionsViewMode,
collectionsView: views.NewCollectionsView(ctx.Collections),
collectionsView: collectionsView,
addCollectionView: views.NewAddCollectionView(ctx.Collections),
}
return m
Expand Down Expand Up @@ -74,9 +79,9 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.selectedIndex = m.collectionsView.GetSelectedIndex()
m.mode = SelectedCollectionViewMode
if m.width > 0 && m.height > 0 {
m.selectedCollectionView = views.NewSelectedCollectionViewWithSize(m.ctx.Endpoints, *selectedItem, m.width, m.height)
m.selectedCollectionView = views.NewSelectedCollectionViewWithSize(m.ctx.Endpoints, m.ctx.HTTP, *selectedItem, m.width, m.height)
} else {
m.selectedCollectionView = views.NewSelectedCollectionView(m.ctx.Endpoints, *selectedItem)
m.selectedCollectionView = views.NewSelectedCollectionView(m.ctx.Endpoints, m.ctx.HTTP, *selectedItem)
}
return m, m.selectedCollectionView.Init()
} else {
Expand Down Expand Up @@ -117,6 +122,9 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.height = msg.Height
if m.mode == CollectionsViewMode && !m.collectionsView.IsInitialized() {
m.collectionsView = views.NewCollectionsViewWithSize(m.ctx.Collections, m.width, m.height)
if m.ctx.DummyDataCreated {
m.collectionsView.SetDummyDataNotification(true)
}
return m, m.collectionsView.Init()
}
if m.mode == CollectionsViewMode {
Expand All @@ -126,6 +134,9 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.mode = CollectionsViewMode
if m.width > 0 && m.height > 0 {
m.collectionsView = views.NewCollectionsViewWithSize(m.ctx.Collections, m.width, m.height)
if m.ctx.DummyDataCreated {
m.collectionsView.SetDummyDataNotification(true)
}
}
m.collectionsView.SetSelectedIndex(m.selectedIndex)
return m, m.collectionsView.Init()
Expand Down
Loading
Loading