1+ package demo
2+
3+ import (
4+ "context"
5+
6+ "github.com/maniac-en/req/internal/backend/collections"
7+ "github.com/maniac-en/req/internal/backend/endpoints"
8+ "github.com/maniac-en/req/internal/log"
9+ )
10+
11+ type DemoGenerator struct {
12+ collectionsManager * collections.CollectionsManager
13+ endpointsManager * endpoints.EndpointsManager
14+ }
15+
16+ func NewDemoGenerator (collectionsManager * collections.CollectionsManager , endpointsManager * endpoints.EndpointsManager ) * DemoGenerator {
17+ return & DemoGenerator {
18+ collectionsManager : collectionsManager ,
19+ endpointsManager : endpointsManager ,
20+ }
21+ }
22+
23+ func (d * DemoGenerator ) PopulateDummyData (ctx context.Context ) (bool , error ) {
24+ log .Info ("populating dummy data for demo" )
25+
26+ // Check if we already have collections
27+ result , err := d .collectionsManager .ListPaginated (ctx , 1 , 0 )
28+ if err != nil {
29+ log .Error ("failed to check existing collections" , "error" , err )
30+ return false , err
31+ }
32+
33+ if len (result .Collections ) > 0 {
34+ log .Debug ("dummy data already exists, skipping population" , "collections_count" , len (result .Collections ))
35+ return false , nil
36+ }
37+
38+ // Create demo collections and endpoints
39+ if err := d .createJSONPlaceholderCollection (ctx ); err != nil {
40+ return false , err
41+ }
42+
43+ if err := d .createReqresCollection (ctx ); err != nil {
44+ return false , err
45+ }
46+
47+ if err := d .createHTTPBinCollection (ctx ); err != nil {
48+ return false , err
49+ }
50+
51+ log .Info ("dummy data populated successfully" )
52+ return true , nil
53+ }
54+
55+ func (d * DemoGenerator ) createJSONPlaceholderCollection (ctx context.Context ) error {
56+ collection , err := d .collectionsManager .Create (ctx , "JSONPlaceholder API" )
57+ if err != nil {
58+ log .Error ("failed to create JSONPlaceholder collection" , "error" , err )
59+ return err
60+ }
61+
62+ endpoints := []endpoints.EndpointData {
63+ {
64+ CollectionID : collection .ID ,
65+ Name : "Get All Posts" ,
66+ Method : "GET" ,
67+ URL : "https://jsonplaceholder.typicode.com/posts" ,
68+ Headers : `{"Content-Type": "application/json"}` ,
69+ QueryParams : map [string ]string {},
70+ RequestBody : "" ,
71+ },
72+ {
73+ CollectionID : collection .ID ,
74+ Name : "Get Single Post" ,
75+ Method : "GET" ,
76+ URL : "https://jsonplaceholder.typicode.com/posts/1" ,
77+ Headers : `{"Content-Type": "application/json"}` ,
78+ QueryParams : map [string ]string {},
79+ RequestBody : "" ,
80+ },
81+ {
82+ CollectionID : collection .ID ,
83+ Name : "Create Post" ,
84+ Method : "POST" ,
85+ URL : "https://jsonplaceholder.typicode.com/posts" ,
86+ Headers : `{"Content-Type": "application/json"}` ,
87+ QueryParams : map [string ]string {},
88+ RequestBody : `{"title": "My New Post", "body": "This is the content of my new post", "userId": 1}` ,
89+ },
90+ {
91+ CollectionID : collection .ID ,
92+ Name : "Update Post" ,
93+ Method : "PUT" ,
94+ URL : "https://jsonplaceholder.typicode.com/posts/1" ,
95+ Headers : `{"Content-Type": "application/json"}` ,
96+ QueryParams : map [string ]string {},
97+ RequestBody : `{"id": 1, "title": "Updated Post", "body": "This post has been updated", "userId": 1}` ,
98+ },
99+ {
100+ CollectionID : collection .ID ,
101+ Name : "Delete Post" ,
102+ Method : "DELETE" ,
103+ URL : "https://jsonplaceholder.typicode.com/posts/1" ,
104+ Headers : `{"Content-Type": "application/json"}` ,
105+ QueryParams : map [string ]string {},
106+ RequestBody : "" ,
107+ },
108+ }
109+
110+ return d .createEndpoints (ctx , endpoints )
111+ }
112+
113+ func (d * DemoGenerator ) createReqresCollection (ctx context.Context ) error {
114+ collection , err := d .collectionsManager .Create (ctx , "ReqRes API" )
115+ if err != nil {
116+ log .Error ("failed to create ReqRes collection" , "error" , err )
117+ return err
118+ }
119+
120+ endpoints := []endpoints.EndpointData {
121+ {
122+ CollectionID : collection .ID ,
123+ Name : "List Users" ,
124+ Method : "GET" ,
125+ URL : "https://reqres.in/api/users" ,
126+ Headers : `{"Content-Type": "application/json"}` ,
127+ QueryParams : map [string ]string {"page" : "2" },
128+ RequestBody : "" ,
129+ },
130+ {
131+ CollectionID : collection .ID ,
132+ Name : "Single User" ,
133+ Method : "GET" ,
134+ URL : "https://reqres.in/api/users/2" ,
135+ Headers : `{"Content-Type": "application/json"}` ,
136+ QueryParams : map [string ]string {},
137+ RequestBody : "" ,
138+ },
139+ {
140+ CollectionID : collection .ID ,
141+ Name : "Create User" ,
142+ Method : "POST" ,
143+ URL : "https://reqres.in/api/users" ,
144+ Headers : `{"Content-Type": "application/json"}` ,
145+ QueryParams : map [string ]string {},
146+ RequestBody : `{"name": "morpheus", "job": "leader"}` ,
147+ },
148+ {
149+ CollectionID : collection .ID ,
150+ Name : "Login" ,
151+ Method : "POST" ,
152+ URL : "https://reqres.in/api/login" ,
153+ Headers : `{"Content-Type": "application/json"}` ,
154+ QueryParams : map [string ]string {},
155+ RequestBody : `{"email": "eve.holt@reqres.in", "password": "cityslicka"}` ,
156+ },
157+ }
158+
159+ return d .createEndpoints (ctx , endpoints )
160+ }
161+
162+ func (d * DemoGenerator ) createHTTPBinCollection (ctx context.Context ) error {
163+ collection , err := d .collectionsManager .Create (ctx , "HTTPBin Testing" )
164+ if err != nil {
165+ log .Error ("failed to create HTTPBin collection" , "error" , err )
166+ return err
167+ }
168+
169+ endpoints := []endpoints.EndpointData {
170+ {
171+ CollectionID : collection .ID ,
172+ Name : "Test GET" ,
173+ Method : "GET" ,
174+ URL : "https://httpbin.org/get" ,
175+ Headers : `{"User-Agent": "Req-Terminal-Client/1.0"}` ,
176+ QueryParams : map [string ]string {"test" : "value" , "demo" : "true" },
177+ RequestBody : "" ,
178+ },
179+ {
180+ CollectionID : collection .ID ,
181+ Name : "Test POST JSON" ,
182+ Method : "POST" ,
183+ URL : "https://httpbin.org/post" ,
184+ Headers : `{"Content-Type": "application/json", "User-Agent": "Req-Terminal-Client/1.0"}` ,
185+ QueryParams : map [string ]string {},
186+ RequestBody : `{"message": "Hello from Req!", "timestamp": "2024-01-15T10:30:00Z", "data": {"key": "value"}}` ,
187+ },
188+ {
189+ CollectionID : collection .ID ,
190+ Name : "Test Headers" ,
191+ Method : "GET" ,
192+ URL : "https://httpbin.org/headers" ,
193+ Headers : `{"Authorization": "Bearer demo-token", "X-Custom-Header": "req-demo"}` ,
194+ QueryParams : map [string ]string {},
195+ RequestBody : "" ,
196+ },
197+ {
198+ CollectionID : collection .ID ,
199+ Name : "Test Status Codes" ,
200+ Method : "GET" ,
201+ URL : "https://httpbin.org/status/200" ,
202+ Headers : `{"Content-Type": "application/json"}` ,
203+ QueryParams : map [string ]string {},
204+ RequestBody : "" ,
205+ },
206+ }
207+
208+ return d .createEndpoints (ctx , endpoints )
209+ }
210+
211+ func (d * DemoGenerator ) createEndpoints (ctx context.Context , endpointData []endpoints.EndpointData ) error {
212+ for _ , data := range endpointData {
213+ _ , err := d .endpointsManager .CreateEndpoint (ctx , data )
214+ if err != nil {
215+ log .Error ("failed to create endpoint" , "name" , data .Name , "error" , err )
216+ return err
217+ }
218+ log .Debug ("created demo endpoint" , "name" , data .Name , "method" , data .Method , "url" , data .URL )
219+ }
220+ return nil
221+ }
0 commit comments