Skip to content

Commit

Permalink
fix import from postman
Browse files Browse the repository at this point in the history
  • Loading branch information
mirzakhany committed Oct 11, 2024
1 parent 1ffdb79 commit 4a5663f
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 21 deletions.
6 changes: 6 additions & 0 deletions internal/importer/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package importer

import (
"encoding/json"
"errors"
"fmt"
"os"
"strings"
Expand Down Expand Up @@ -115,6 +116,11 @@ func ImportPostmanCollection(data []byte) error {

var collection PostmanCollection
if err := json.Unmarshal(data, &collection); err != nil {
// it might be v2 of the collection. inform the user to convert it to v2.1
if strings.Contains(string(data), "https://schema.getpostman.com/json/collection/v2.0.0/collection.json") {
return errors.New("it seems like you are using v2 of the collection. Please export it as v2.1 and try again")
}

return fmt.Errorf("error parsing JSON: %w", err)
}

Expand Down
8 changes: 8 additions & 0 deletions ui/explorer/explorer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package explorer

import (
"errors"
"fmt"
"io"
"os"
Expand All @@ -22,6 +23,8 @@ type Result struct {
Data []byte
Error error
FilePath string

Declined bool
}

func NewExplorer(w *app.Window) *Explorer {
Expand All @@ -39,6 +42,11 @@ func (e *Explorer) ChoseFile(onResult func(r Result), extensions ...string) {

file, err := e.expl.ChooseFile(extensions...)
if err != nil {
if errors.Is(err, explorer.ErrUserDecline) {
onResult(Result{Error: err, Declined: true})
return
}

err = fmt.Errorf("failed opening file: %w", err)
onResult(Result{Error: err})
return
Expand Down
7 changes: 5 additions & 2 deletions ui/pages/environments/controller.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package environments

import (
"errors"
"fmt"

"github.com/chapar-rest/chapar/internal/domain"
Expand Down Expand Up @@ -64,7 +63,11 @@ func (c *Controller) onNewEnvironment() {

func (c *Controller) onImportEnvironment() {
c.explorer.ChoseFile(func(result explorer.Result) {
if !errors.Is(result.Error, explorer.ErrUserDecline) {
if result.Declined {
return
}

if result.Error != nil {
c.view.showError(fmt.Errorf("failed to get file %w", result.Error))
return
}
Expand Down
9 changes: 5 additions & 4 deletions ui/pages/protofiles/controller.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package protofiles

import (
"errors"
"path/filepath"

"google.golang.org/protobuf/reflect/protoreflect"
Expand Down Expand Up @@ -79,10 +78,12 @@ func (c *Controller) addPath(path string) {

func (c *Controller) onAdd() {
c.explorer.ChoseFile(func(result explorer.Result) {
if result.Declined {
return
}

if result.Error != nil {
if !errors.Is(result.Error, explorer.ErrUserDecline) {
c.showError("Failed to open file", result.Error.Error())
}
c.showError("Failed to open file", result.Error.Error())
return
}

Expand Down
29 changes: 19 additions & 10 deletions ui/pages/requests/controller.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package requests

import (
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -89,12 +88,15 @@ func (c *Controller) LoadData() error {

func (c *Controller) onSelectBinaryFile(id string) {
c.explorer.ChoseFile(func(result explorer.Result) {
if result.Declined {
return
}

if result.Error != nil {
if !errors.Is(result.Error, explorer.ErrUserDecline) {
c.view.showError(fmt.Errorf("failed to get file, %w", result.Error))
return
}
c.view.showError(fmt.Errorf("failed to get file, %w", result.Error))
return
}

if result.FilePath == "" {
return
}
Expand All @@ -104,12 +106,15 @@ func (c *Controller) onSelectBinaryFile(id string) {

func (c *Controller) onFormDataFileSelect(requestId, fieldId string) {
c.explorer.ChoseFile(func(result explorer.Result) {
if result.Declined {
return
}

if result.Error != nil {
if !errors.Is(result.Error, explorer.ErrUserDecline) {
c.view.showError(fmt.Errorf("failed to get file, %w", result.Error))
return
}
c.view.showError(fmt.Errorf("failed to get file, %w", result.Error))
return
}

if result.FilePath == "" {
return
}
Expand Down Expand Up @@ -690,7 +695,11 @@ func (c *Controller) onNewRequest(requestType string) {

func (c *Controller) onImport() {
c.explorer.ChoseFile(func(result explorer.Result) {
if !errors.Is(result.Error, explorer.ErrUserDecline) {
if result.Declined {
return
}

if result.Error != nil {
c.view.showError(fmt.Errorf("failed to get file, %w", result.Error))
return
}
Expand Down
10 changes: 5 additions & 5 deletions ui/widgets/file_selector.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package widgets

import (
"errors"
"fmt"

"gioui.org/layout"
Expand Down Expand Up @@ -50,13 +49,14 @@ func (b *FileSelector) handleExplorerSelect() {
}

b.explorer.ChoseFile(func(result explorer.Result) {
if result.Declined {
return
}
if result.Error != nil {
if !errors.Is(result.Error, explorer.ErrUserDecline) {
fmt.Printf("failed to get file, %s\n", result.Error)
return
}
fmt.Printf("failed to get file, %s\n", result.Error)
return
}

if result.FilePath == "" {
return
}
Expand Down

0 comments on commit 4a5663f

Please sign in to comment.