Skip to content
Snippets Groups Projects
Select Git revision
  • 90ec162f4b59c68672a981f51916632a94e95b1c
  • main default protected
  • v1.298.0
  • v1.297.0
  • v1.296.0
  • v1.295.0
  • v1.294.0
  • v1.293.0
  • v1.292.0
  • v1.291.0
  • v1.290.0
  • v1.289.0
  • v1.288.0
  • v1.287.0
  • v1.286.0
  • v1.285.0
  • v1.284.0
  • v1.283.0
  • v1.282.0
  • v1.281.0
  • v1.280.0
  • v1.279.0
22 results

handler.go

Blame
  • api_documentation.go 4.19 KiB
    package api_documentation
    
    import (
    	"fmt"
    	"reflect"
    	"strings"
    
    	"gitlab.com/uafrica/go-utils/handler_utils"
    
    	"gitlab.com/uafrica/go-utils/errors"
    )
    
    type NoParams struct{}
    
    type Docs struct {
    	Paths map[string]DocPath `json:"paths"`
    }
    
    type DocPath struct {
    	Methods map[string]DocMethod `json:"methods"`
    }
    
    type DocMethod struct {
    	Description string              `json:"description"`
    	Parameters  map[string]DocParam `json:"parameters,omitempty"`
    	Request     interface{}         `json:"request,omitempty"`
    	Response    interface{}         `json:"response,omitempty"`
    }
    
    type DocParam struct {
    	Name        string
    	Type        string
    	Description string
    }
    
    func GetDocs(endpointHandlers map[string]map[string]interface{}) (Docs, error) {
    	docs := Docs{
    		Paths: map[string]DocPath{},
    	}
    	for path, methods := range endpointHandlers {
    		docPath := DocPath{
    			Methods: map[string]DocMethod{},
    		}
    		for method, methodHandler := range methods {
    			docMethod := DocMethod{}
    			if handler, ok := methodHandler.(handler_utils.Handler); !ok {
    				docMethod.Description = "Not available"
    			} else {
    				//purpose
    				docMethod.Description = "Not available - see request and response structs"
    
    				//describe parameters
    				docMethod.Parameters = map[string]DocParam{}
    				for i := 0; i < handler.RequestParamsType.NumField(); i++ {
    					f := handler.RequestParamsType.Field(i)
    
    					name := f.Tag.Get("json")
    					if name == "" {
    						name = f.Name
    					}
    
    					docMethod.Parameters[f.Name] = DocParam{
    						Name:        name,
    						Type:        fmt.Sprintf("%v", f.Type),
    						Description: f.Tag.Get("doc"),
    					}
    				}
    
    				//describe request schema
    				var err error