Skip to content

Commit

Permalink
modified
Browse files Browse the repository at this point in the history
  • Loading branch information
jumaevkova04 committed Nov 7, 2023
1 parent eb81319 commit 4c34b28
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 37 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea/
docs/
docs/
run.sh
33 changes: 18 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,21 @@ func main() {

// -------------------------------------------------------------------------

filePath := *file

fileFields, err := p.ReadOpenApiFileAndTakeRequiredFields(filePath)
checkErrorAndPrintResponse("READ_FILE", filePath, err)

checkErrorAndPrintResponse("FILE_FORMAT", fileFields.Format, nil)
checkErrorAndPrintResponse("API_SCHEMA_TYPE", fileFields.APISchemaType, nil)
checkErrorAndPrintResponse("COLLECTION_NAME", fileFields.CollectionName, nil)

// -------------------------------------------------------------------------

apiRequest := postman.CreateAPIRequest{Api: postman.Api{
Name: "Test API",
Summary: "Test API Schema",
Description: "This is a test API",
Name: fileFields.CollectionName,
Summary: fmt.Sprintf("%s Schema", fileFields.CollectionName),
Description: fmt.Sprintf("This is a %s", fileFields.CollectionName),
}}

api, err := p.CreateAPI(apiRequest)
Expand All @@ -53,18 +64,10 @@ func main() {

// -------------------------------------------------------------------------

path := *file

fileContent, fileFormat, apiSchemaType, err := p.ReadFileAndFindApiSchemaType(path)
checkErrorAndPrintResponse("READ_FILE", path, err)

checkErrorAndPrintResponse("FILE_FORMAT", fileFormat, nil)
checkErrorAndPrintResponse("API_SCHEMA_TYPE", apiSchemaType, nil)

apiSchemaRequest := postman.CreateAPISchemaRequest{Schema: postman.Schema{
Language: fileFormat,
Schema: string(fileContent),
Type: apiSchemaType,
Language: fileFields.Format,
Schema: string(fileFields.Content),
Type: fileFields.APISchemaType,
}}

apiSchema, err := p.CreateAPISchema(api.Api.ID, apiVersions.Versions[0].ID, apiSchemaRequest)
Expand All @@ -73,7 +76,7 @@ func main() {
// -------------------------------------------------------------------------

apiCollectionRequest := postman.CreateAPICollectionFromSchemaRequest{
Name: "Test Collection",
Name: fileFields.CollectionName,
Relations: []postman.Relations{
{
Type: "documentation",
Expand Down
30 changes: 30 additions & 0 deletions postman/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,36 @@ func (p *Postman) ReplaceCollectionsData(collectionID string, body []byte) (*Rep

// ---------------------------------------------------------------------------------------------------------------------

type UpdateCollectionsDataResponse struct {
Collection struct {
ID string `json:"id"`
Name string `json:"name"`
Uid string `json:"uid"`
} `json:"collection"`
}

func (p *Postman) UpdateCollectionsData(collectionID string, body []byte) (*UpdateCollectionsDataResponse, error) {
if err := p.validation(); err != nil {
return nil, err
}

var (
method = "PATCH"
url = fmt.Sprintf("https://api.getpostman.com/collections/%s", collectionID)
payload = body
response *UpdateCollectionsDataResponse
)

err := p.DoRequestAndUnmarshal(method, url, payload, &response)
if err != nil {
return nil, err
}

return response, nil
}

// ---------------------------------------------------------------------------------------------------------------------

type DeleteCollectionResponse struct {
Collection struct {
ID string `json:"id"`
Expand Down
92 changes: 72 additions & 20 deletions postman/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,53 +17,105 @@ const (
prefixSchemaType = "openapi"
)

type ApiSchemaType struct {
type OpenApiFileFields struct {
FileType
Info FileInfo `json:"info" yaml:"info"`

Content []byte `json:"-" yaml:"-"`
}

type FileType struct {
Swagger string `json:"swagger" yaml:"swagger"`
Openapi string `json:"openapi" yaml:"openapi"`
}

func (p *Postman) ReadFileAndFindApiSchemaType(filePath string) (content []byte, format string, schemaType string, err error) {
type FileInfo struct {
Title string `json:"title" yaml:"title"`
Contact struct{} `json:"contact" yaml:"contact"`
Version string `json:"version" yaml:"version"`
}

type OpenApiFileResponse struct {
Format string
APISchemaType string
CollectionName string
Content []byte
}

func (p *Postman) ReadOpenApiFileAndTakeRequiredFields(filePath string) (openApiFileResponse *OpenApiFileResponse, err error) {
fileFormat, err := getFileFormat(filePath)
if err != nil {
return nil, err
}

file, err := readFileAndUnmarshal(filePath, fileFormat)
if err != nil {
return nil, err
}

schemaType := getAPISchemaType(file.FileType)

openApiFileResponse = &OpenApiFileResponse{
Format: fileFormat,
APISchemaType: schemaType,
CollectionName: file.Info.Title,
Content: file.Content,
}

return openApiFileResponse, nil
}

func getFileFormat(filePath string) (string, error) {
fileExtension := strings.Split(filePath, ".")
if len(fileExtension) < 2 {
return nil, "", "", fmt.Errorf("invalid file: %s", filePath)
return "", fmt.Errorf("invalid file path: %s", filePath)
}

fileFormat := fileExtension[len(fileExtension)-1]

if fileFormat != fileFormatJson && fileFormat != fileFormatYaml && fileFormat != fileFormatYml {
return nil, "", "", fmt.Errorf("invalid file format: %q, must be .%s or .%s (.%s)", fileFormat, fileFormatJson, fileFormatYaml, fileFormatYml)
return "", fmt.Errorf("invalid file format: %q, must be .%s or .%s (.%s)", fileFormat, fileFormatJson, fileFormatYaml, fileFormatYml)
}

content, err = os.ReadFile(filePath)
return fileFormat, nil
}

func readFileAndUnmarshal(filePath string, fileFormat string) (*OpenApiFileFields, error) {
content, err := os.ReadFile(filePath)
if err != nil {
return nil, "", "", err
return nil, err
}

var apiSchemaType ApiSchemaType
var file OpenApiFileFields

if fileFormat == fileFormatJson {
err = json.Unmarshal(content, &apiSchemaType)
err := json.Unmarshal(content, &file)
if err != nil {
return nil, "", "", err
return nil, err
}
} else {
err := yaml.Unmarshal(content, &apiSchemaType)
err := yaml.Unmarshal(content, &file)
if err != nil {
return nil, "", "", err
return nil, err
}
}

schemaType = apiSchemaType.Swagger
file.Content = content

return &file, nil
}

func getAPISchemaType(fileType FileType) string {
schemaType := fileType.Swagger

if schemaType == "" {
schemaType = apiSchemaType.Openapi
if strings.TrimSpace(schemaType) == "" {
schemaType = fileType.Openapi
}

if schemaType == "" {
schemaType = defaultSchemaType
} else {
apiSchemaTypeFirstNumber := strings.Split(schemaType, ".")[0]
schemaType = prefixSchemaType + apiSchemaTypeFirstNumber
if strings.TrimSpace(schemaType) == "" {
return defaultSchemaType
}

return content, fileFormat, schemaType, nil
apiSchemaTypeFirstNumber := strings.Split(schemaType, ".")[0]
return prefixSchemaType + apiSchemaTypeFirstNumber
}
2 changes: 1 addition & 1 deletion swagger.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"swagger": "2.0",
"info": {
"title": "feature.proto",
"title": "TEST API",
"version": "version not set"
},
"schemes": [
Expand Down

0 comments on commit 4c34b28

Please sign in to comment.