Skip to content

Commit 468e8fd

Browse files
committed
feat: adds save/export support in json format
fix #1
1 parent a40cf1e commit 468e8fd

File tree

9 files changed

+121
-16
lines changed

9 files changed

+121
-16
lines changed

app.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,14 @@ func HeadersToStr(h *http.Header) string {
6262
return result
6363
}
6464

65-
type RequestFE struct {
66-
Body string
67-
HeadersStr string
68-
Error string
69-
}
70-
7165
type RequestResult struct {
72-
Method string
73-
URL string
74-
ReqHeaders string
75-
RequestBody string
76-
Body string
77-
HeadersStr string
78-
Error string
66+
Method string `json:"Method"`
67+
URL string `json:"URL"`
68+
ReqHeaders string `json:"ReqHeaders"`
69+
RequestBody string `json:"RequestBody"`
70+
Body string `json:"Body"`
71+
HeadersStr string `json:"HeadersStr"`
72+
Error string `json:"Error"`
7973
}
8074

8175
func (a *App) RunCurl(curl string) RequestResult {

export.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"log"
6+
"os"
7+
8+
"github.com/wailsapp/wails/v2/pkg/runtime"
9+
)
10+
11+
func (a *App) Export(r RequestResult) error {
12+
filepath, err := runtime.SaveFileDialog(a.ctx, runtime.SaveDialogOptions{
13+
DefaultFilename: "untitled-01.json",
14+
Title: "Export",
15+
})
16+
if err != nil {
17+
log.Println(err)
18+
return err
19+
}
20+
21+
js, err := json.MarshalIndent(&r, "", " ")
22+
if err != nil {
23+
log.Println(err)
24+
return err
25+
}
26+
f, err := os.Create(filepath)
27+
if err != nil {
28+
log.Println(err)
29+
return err
30+
}
31+
32+
defer f.Close()
33+
f.Write(js)
34+
35+
return nil
36+
}

frontend/src/App.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {useState} from 'react';
22
import './App.css';
3-
import {MakeRequest, RunCurl} from "../wailsjs/go/main/App";
3+
import {MakeRequest, RunCurl, Export} from "../wailsjs/go/main/App";
4+
import {main} from "../wailsjs/go/models";
45
import Button from '@mui/material/Button';
56
import TextField from '@mui/material/TextField';
67
import Divider from '@mui/material/Divider';
@@ -10,6 +11,7 @@ import Modal from '@mui/material/Modal';
1011
import Select from '@mui/material/Select';
1112
import Box from '@mui/material/Box';
1213
import Typography from '@mui/material/Typography';
14+
import SaveIcon from '@mui/icons-material/Save';
1315
import { ThemeProvider, createTheme } from '@mui/material/styles';
1416
import {
1517
Experimental_CssVarsProvider as CssVarsProvider,
@@ -24,6 +26,7 @@ const darkTheme = createTheme({
2426

2527

2628
function App() {
29+
const [result, setResult] = useState(new main.RequestResult)
2730
const [resultText, setResultText] = useState("");
2831
const [curlBody, setCurlBody] = useState("");
2932
const [reqMethod, setReqMethod] = useState("GET");
@@ -35,6 +38,7 @@ function App() {
3538
const [open, setOpen] = useState(false);
3639
const updateURL = (e: any) => setURL(e.target.value);
3740
const updateResultText = (result: any) => {
41+
setResult(result)
3842
setResultHeader(result.HeadersStr)
3943
setResultText(result.Body)
4044
setErrorText(result.Error)
@@ -73,6 +77,10 @@ function App() {
7377
updateCurlResult(result)
7478
})
7579
}
80+
81+
function handleExport() {
82+
Export(result)
83+
}
7684
return (
7785
<CssVarsProvider>
7886
<CssBaseline />
@@ -96,6 +104,9 @@ function App() {
96104
</Box>
97105
</Modal>
98106
<Grid container className="App">
107+
<Grid xs={1}>
108+
<Button onClick={handleExport} variant="outlined"><SaveIcon></SaveIcon></Button>
109+
</Grid>
99110
<Grid xs={1}>
100111
<Button onClick={handleOpen} variant="outlined">Curl</Button>
101112
</Grid>
@@ -106,7 +117,7 @@ function App() {
106117
<MenuItem value={"PUT"}>PUT</MenuItem>
107118
</Select>
108119
</Grid>
109-
<Grid xs={9}>
120+
<Grid xs={8}>
110121
<TextField id="url" className="url" variant="standard" value={url}
111122
onChange={updateURL} autoComplete="off" name="url"/>
112123
</Grid>

frontend/wailsjs/go/main/App.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// This file is automatically generated. DO NOT EDIT
33
import {main} from '../models';
44

5+
export function Export(arg1:main.RequestResult):Promise<void>;
6+
57
export function MakeRequest(arg1:string,arg2:string,arg3:string,arg4:string):Promise<main.RequestResult>;
68

79
export function RunCurl(arg1:string):Promise<main.RequestResult>;

frontend/wailsjs/go/main/App.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
33
// This file is automatically generated. DO NOT EDIT
44

5+
export function Export(arg1) {
6+
return window['go']['main']['App']['Export'](arg1);
7+
}
8+
59
export function MakeRequest(arg1, arg2, arg3, arg4) {
610
return window['go']['main']['App']['MakeRequest'](arg1, arg2, arg3, arg4);
711
}

frontend/wailsjs/go/models.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
export namespace main {
2+
3+
export class RequestResult {
4+
Method: string;
5+
URL: string;
6+
ReqHeaders: string;
7+
RequestBody: string;
8+
Body: string;
9+
HeadersStr: string;
10+
Error: string;
11+
12+
static createFrom(source: any = {}) {
13+
return new RequestResult(source);
14+
}
15+
16+
constructor(source: any = {}) {
17+
if ('string' === typeof source) source = JSON.parse(source);
18+
this.Method = source["Method"];
19+
this.URL = source["URL"];
20+
this.ReqHeaders = source["ReqHeaders"];
21+
this.RequestBody = source["RequestBody"];
22+
this.Body = source["Body"];
23+
this.HeadersStr = source["HeadersStr"];
24+
this.Error = source["Error"];
25+
}
26+
}
27+
28+
}
29+

main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"embed"
5+
"log"
56

67
"github.com/wailsapp/wails/v2"
78
"github.com/wailsapp/wails/v2/pkg/options"
@@ -12,6 +13,7 @@ import (
1213
var assets embed.FS
1314

1415
func main() {
16+
log.SetFlags(log.LstdFlags | log.Lshortfile)
1517
// Create an instance of the app structure
1618
app := NewApp()
1719

package-lock.json

Lines changed: 27 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"@emotion/react": "^11.11.1",
44
"@emotion/styled": "^11.11.0",
55
"@fontsource/roboto": "^5.0.3",
6+
"@mui/icons-material": "^5.11.16",
67
"@mui/material": "^5.13.5"
78
}
89
}

0 commit comments

Comments
 (0)