Skip to content

Commit 481ecec

Browse files
committed
add postman collection generator support
1 parent e79a033 commit 481ecec

13 files changed

+911
-563
lines changed

README.MD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Restify is an application built for the [EVO Framework](https://github.com/getev
2020
- [Loading Associations](./docs/endpoints.md#loading-associations)
2121
- [Offset and Limit](./docs/endpoints.md#offset-and-limit)
2222
- [Select Specific Fields](./docs/endpoints.md#select-specific-fields)
23+
- [Postman Collection Generator](./docs/endpoints.md#postman-collection-generator)
2324
- [Example Postman Collection](./docs/endpoints.md#example-postman-collection)
2425
- **[Customization](./docs/customization.md)**
2526
- [Hooks](./docs/customization.md#hooks)

app.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,28 @@ import (
66
"github.com/getevo/evo/v2/lib/application"
77
"github.com/getevo/evo/v2/lib/db"
88
"github.com/getevo/evo/v2/lib/db/schema"
9+
"github.com/getevo/postman"
910
"math"
1011
)
1112

1213
var Prefix = "/admin/rest"
1314

1415
var permissionHandler func(permissions Permissions, context *Context) bool
16+
var collection *postman.Collection
1517

1618
type App struct{}
1719

1820
func (app App) Register() error {
1921
if !db.Enabled {
2022
return fmt.Errorf("database is not enabled. restify plugin cannot be registered without a running database. please enable the database in your configuration file")
2123
}
24+
collection = postman.NewCollection("Restify", "")
25+
26+
if postmanAuthType != "none" {
27+
collection.Auth = &postman.Auth{
28+
Type: postman.AuthType(postmanAuthType),
29+
}
30+
}
2231
return nil
2332
}
2433

@@ -38,7 +47,10 @@ func (app App) WhenReady() error {
3847
resources[idx].Actions[i].RegisterRouter()
3948
}
4049
}
41-
evo.Get(Prefix+"/models", controller.Models)
50+
evo.Get(Prefix+"/models", controller.ModelsHandler)
51+
if postmanRegistered {
52+
evo.Get(Prefix+"/postman", controller.PostmanHandler)
53+
}
4254
return nil
4355
}
4456

controller.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,29 @@
11
package restify
22

3-
import "github.com/getevo/evo/v2"
3+
import (
4+
"github.com/getevo/evo/v2"
5+
"github.com/getevo/evo/v2/lib/outcome"
6+
)
47

58
// Controller represents a controller type.
69
type Controller struct{}
710

8-
// Models returns all registered models.
9-
func (c Controller) Models(request *evo.Request) interface{} {
11+
// ModelsHandler returns all registered models.
12+
func (c Controller) ModelsHandler(request *evo.Request) interface{} {
1013
return resources
1114
}
15+
16+
func (c Controller) PostmanHandler(request *evo.Request) any {
17+
b, err := collection.ToJson()
18+
if err != nil {
19+
return err
20+
}
21+
return outcome.Response{
22+
StatusCode: 200,
23+
ContentType: "application/json",
24+
Data: b,
25+
Headers: map[string]string{
26+
"Content-Disposition": "attachment; filename=postman_collection.json",
27+
},
28+
}
29+
}

docs/endpoints.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,5 +122,38 @@ This example retrieves only the username and name fields from all records in the
122122
- **Model Information:** Using `GET /admin/rest/models`, it is possible to get information about all available models.
123123

124124
---
125+
#### Postman Collection Generator
126+
127+
Restify enables developers to automatically generate Postman collections without requiring manual input. These collections are thoughtfully structured, providing comprehensive explanations of the API, making it easy for developers to understand and utilize them effectively.
128+
129+
1- Enable Postman Collection Generator
130+
```golang
131+
func (app App) Register() error {
132+
restify.EnablePostman()
133+
restify.SetPostmanAuthorization(restify.AuthTypeHeader, "apikey", "secret") //force apikey and secret authentication in header
134+
/*
135+
restify.SetPostmanAuthorization(restify.AuthTypeBasic) // force basic authentication on Restify collection
136+
restify.SetPostmanAuthorization(AuthTypeNone)
137+
restify.SetPostmanAuthorization(AuthTypeBearer)
138+
restify.SetPostmanAuthorization(AuthTypeDigest)
139+
restify.SetPostmanAuthorization(AuthTypeEdgeGrid)
140+
restify.SetPostmanAuthorization(AuthTypeHawk)
141+
restify.SetPostmanAuthorization(AuthTypeOAuth1)
142+
restify.SetPostmanAuthorization(AuthTypeOAuth2)
143+
restify.SetPostmanAuthorization(AuthTypeNTLM)
144+
restify.SetPostmanAuthorization(AuthTypeHeader)
145+
*/
146+
return nil
147+
}
148+
```
149+
150+
2- Download Collection
151+
```bash
152+
curl -o "./collection.json" "{{ base_path }}/{{ prefix }}/postman"
153+
```
154+
155+
156+
---
157+
125158
#### Example Postman Collection
126159
**[Postman Collection](https://github.com/getevo/restify/blob/master/example/example.postman_collection.json)**

example/apps/user/app.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ type App struct{}
1111

1212
func (app App) Register() error {
1313
restify.SetPrefix("/admin/rest")
14+
restify.EnablePostman()
15+
16+
//restify.SetPostmanAuthorization(restify.AuthTypeHeader, "username", "password") //force user and password authentication in header
17+
restify.SetPostmanAuthorization(restify.AuthTypeBasic) // force basic authentication on Restify collection
1418
db.UseModel(User{}, Order{}, Product{}, Article{})
1519
evo.GetDBO().AutoMigrate(Article{}, User{}, Order{}, Product{})
1620
return nil

0 commit comments

Comments
 (0)