|
| 1 | +package process |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "github.com/rog-golang-buddies/internal/load" |
| 6 | + "github.com/rog-golang-buddies/internal/model" |
| 7 | + "github.com/rog-golang-buddies/internal/parse" |
| 8 | + "github.com/rog-golang-buddies/internal/recognize" |
| 9 | +) |
| 10 | + |
| 11 | +//UrlProcessor represents provide entrypoint for the url processing |
| 12 | +//full processing of the incoming |
| 13 | +type UrlProcessor interface { |
| 14 | + process(ctx context.Context, url string) (*model.ApiSpecDoc, error) |
| 15 | +} |
| 16 | + |
| 17 | +type ProcessorImpl struct { |
| 18 | + recognizer recognize.Recognizer |
| 19 | + converter parse.Converter |
| 20 | + contentLoader load.ContentLoader |
| 21 | +} |
| 22 | + |
| 23 | +func (p *ProcessorImpl) process(ctx context.Context, url string) (*model.ApiSpecDoc, error) { |
| 24 | + //Check availability of url |
| 25 | + //... |
| 26 | + |
| 27 | + //Load content by url |
| 28 | + file, err := p.contentLoader.Load(ctx, url) |
| 29 | + if err != nil { |
| 30 | + return nil, err |
| 31 | + } |
| 32 | + |
| 33 | + //If no errs recognize file type by content |
| 34 | + fileType, err := p.recognizer.RecognizeFileType(file) |
| 35 | + if err != nil { |
| 36 | + return nil, err |
| 37 | + } |
| 38 | + |
| 39 | + //Parse API spec of defined type |
| 40 | + apiSpec, err := p.converter.Convert(file.Content, fileType) |
| 41 | + if err != nil { |
| 42 | + return nil, err |
| 43 | + } |
| 44 | + |
| 45 | + return apiSpec, nil |
| 46 | +} |
| 47 | + |
| 48 | +func NewProcessor() (UrlProcessor, error) { |
| 49 | + //Need to pass dependencies through constructor |
| 50 | + return &ProcessorImpl{ |
| 51 | + recognizer: recognize.NewRecognizer(), |
| 52 | + converter: parse.NewConverter([]parse.Parser{parse.NewYamlOpenApiParser(), parse.NewJsonOpenApiParser()}), |
| 53 | + contentLoader: load.NewContentLoader(), |
| 54 | + }, nil |
| 55 | +} |
0 commit comments