Skip to content

Commit

Permalink
feat: adds curl import supoort
Browse files Browse the repository at this point in the history
  • Loading branch information
dropdevrahul committed Jun 22, 2023
1 parent ab1b1e2 commit dfb1c9e
Show file tree
Hide file tree
Showing 7 changed files with 309 additions and 17 deletions.
53 changes: 44 additions & 9 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,61 @@ func MakeRequest(c *http.Client,
return body, resp, nil
}

type RequestResult struct {
func HeadersToStr(h *http.Header) string {
headersArr := []string{}
for k, v := range *h {
vals := strings.Join(v, "; ")
headersArr = append(headersArr, k+": "+vals)
}

result := strings.Join(headersArr, "\n")
return result
}

type RequestFE struct {
Body string
HeadersStr string
Error string
}

type RequestResult struct {
Method string
URL string
ReqHeaders string
RequestBody string
Body string
HeadersStr string
Error string
}

func (a *App) RunCurl(curl string) RequestResult {
res := RequestResult{}
r, ok := Parse(curl)
if !ok {
res.Error = "Unable to parse curl"
return res
}

res = a.MakeRequest(r.Url, r.Method, r.Body, r.Header.ToString())
res.ReqHeaders = r.Header.ToString()
res.Method = r.Method
res.URL = r.Url
res.RequestBody = r.Body
return res
}

// Greet returns a greeting for the given name
func (a *App) MakeRequest(
urlIn string,
method string,
body string,
headers string,
) RequestResult {
result := RequestResult{}
result := RequestResult{
URL: urlIn,
Method: method,
RequestBody: body,
}
rbody := bytes.NewBuffer([]byte(body))
r, err := http.NewRequest(method, urlIn, rbody)
if err != nil {
Expand All @@ -86,13 +127,7 @@ func (a *App) MakeRequest(
return result
}

headersArr := []string{}
for k, v := range httpResp.Header {
vals := strings.Join(v, "; ")
headersArr = append(headersArr, k+": "+vals)
}

result.HeadersStr = strings.Join(headersArr, "\n")
result.HeadersStr = HeadersToStr(&httpResp.Header)
b := bytes.NewBuffer(make([]byte, 0, len(res)))
err = json.Indent(b, res, "\n", " ")
if err != nil {
Expand Down
68 changes: 61 additions & 7 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import {useState} from 'react';
import './App.css';
import {MakeRequest} from "../wailsjs/go/main/App";
import {MakeRequest, RunCurl} from "../wailsjs/go/main/App";
import Button from '@mui/material/Button';
import TextField from '@mui/material/TextField';
import Divider from '@mui/material/Divider';
import Grid from '@mui/material/Unstable_Grid2'; // Grid version 2
import MenuItem from '@mui/material/MenuItem'; // Grid version 2
import Select from '@mui/material/Select'; // Grid version 2
import MenuItem from '@mui/material/MenuItem';
import Modal from '@mui/material/Modal';
import Select from '@mui/material/Select';
import Box from '@mui/material/Box';
import Typography from '@mui/material/Typography';
import { ThemeProvider, createTheme } from '@mui/material/styles';
import {
Experimental_CssVarsProvider as CssVarsProvider,
Expand All @@ -22,38 +25,89 @@ const darkTheme = createTheme({

function App() {
const [resultText, setResultText] = useState("");
const [curlBody, setCurlBody] = useState("");
const [reqMethod, setReqMethod] = useState("GET");
const [errorText, setErrorText] = useState("");
const [reqBody, setReqBody] = useState("");
const [reqHeaders, setReqHeaders] = useState("");
const [resultHeader, setResultHeader] = useState("");
const [url, setURL] = useState('');
const [open, setOpen] = useState(false);
const updateURL = (e: any) => setURL(e.target.value);
const updateResultText = (result: any) => {
setResultHeader(result.HeadersStr)
setResultHeader(result.HeadersStr)
setResultText(result.Body)
setErrorText(result.Error)
};
const updateCurlResult = (result: any) => {
setURL(result.URL)
setReqBody(result.RequestBody)
setReqHeaders(result.ReqHeaders)
setReqMethod(result.Method)
handleClose()
}
const handleOpen = () => setOpen(true);
const handleClose = () => setOpen(false);
const style = {
position: 'absolute' as 'absolute',
top: '50%',
left: '50%',
transform: 'translate(-50%, -50%)',
width: 800,
bgcolor: 'background.paper',
p: 4,
};

const updateMethod = (e: any) => setReqMethod(e.target.value);
const updateBody = (e:any) => setReqBody(e.target.value)
const updateReqHeaders = (e:any) => setReqHeaders(e.target.value)
const updateCurlBody = (e:any) => setCurlBody(e.target.value)
function makeRequest() {
MakeRequest(url, reqMethod, reqBody, reqHeaders).then(updateResultText);
}

function importCurl() {
console.log("importing curl")
RunCurl(curlBody).then((result: any)=>{
updateResultText(result)
updateCurlResult(result)
})
}
return (
<CssVarsProvider>
<CssBaseline />
<Modal
open={open}
onClose={handleClose}
aria-labelledby="modal-modal-title"
aria-describedby="modal-modal-description"
>
<Box sx={style}>
<Typography id="modal-modal-title" variant="h6" component="h2">
Enter Curl
</Typography>
<Typography id="modal-modal-description" sx={{ mt: 2, width:"100%" }}>
<TextField id="reqheaders" multiline rows={10} color="primary" sx={{width:"100%"}}
onChange={updateCurlBody} autoComplete="off" name="curl"/>
</Typography>
<Typography id="modal-modal-menu" sx={{ mt: 2, width:"100%" }}>
<Button onClick={importCurl} variant="outlined">Curl</Button>
</Typography>
</Box>
</Modal>
<Grid container className="App">
<Grid xs={1}>
<Button onClick={handleOpen} variant="outlined">Curl</Button>
</Grid>
<Grid xs={1}>
<Select label="Type" value={reqMethod} className="url" color="success" variant="standard" onChange={updateMethod}>
<MenuItem value={"GET"}>GET</MenuItem>
<MenuItem value={"POST"}>POST</MenuItem>
<MenuItem value={"PUT"}>PUT</MenuItem>
</Select>
</Grid>
<Grid xs={10}>
<TextField id="url" className="url" variant="standard"
<Grid xs={9}>
<TextField id="url" className="url" variant="standard" value={url}
onChange={updateURL} autoComplete="off" name="url"/>
</Grid>
<Grid xs={1}>
Expand All @@ -63,7 +117,7 @@ function App() {
</Grid>
<Grid xs={4} className="req-headers-p">
<Divider textAlign="left" className="req-header-div" color="success"><span>Request Headers</span></Divider>
<TextField id="reqheaders" multiline className="req-headers" variant="filled" rows={10} color="success"
<TextField id="reqheaders" multiline className="req-headers" variant="filled" rows={10} color="success" value={reqHeaders}
onChange={updateReqHeaders} autoComplete="off" name="reqheaders"/>
</Grid>
<Grid xs={8} className="req-body-p">
Expand Down
2 changes: 2 additions & 0 deletions frontend/wailsjs/go/main/App.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
import {main} from '../models';

export function MakeRequest(arg1:string,arg2:string,arg3:string,arg4:string):Promise<main.RequestResult>;

export function RunCurl(arg1:string):Promise<main.RequestResult>;
4 changes: 4 additions & 0 deletions frontend/wailsjs/go/main/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@
export function MakeRequest(arg1, arg2, arg3, arg4) {
return window['go']['main']['App']['MakeRequest'](arg1, arg2, arg3, arg4);
}

export function RunCurl(arg1) {
return window['go']['main']['App']['RunCurl'](arg1);
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ module changeme

go 1.18

require github.com/wailsapp/wails/v2 v2.5.1
require (
github.com/mattn/go-shellwords v1.0.12
github.com/wailsapp/wails/v2 v2.5.1
)

require (
github.com/bep/debounce v1.2.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ github.com/mattn/go-colorable v0.1.11 h1:nQ+aFkoE2TMGc0b68U2OKSexC+eq46+XwZzWXHR
github.com/mattn/go-colorable v0.1.11/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-shellwords v1.0.12 h1:M2zGm7EW6UQJvDeQxo4T51eKPurbeFbe8WtebGE2xrk=
github.com/mattn/go-shellwords v1.0.12/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y=
github.com/pkg/browser v0.0.0-20210706143420-7d21f8c997e2 h1:acNfDZXmm28D2Yg/c3ALnZStzNaZMSagpbr96vY6Zjc=
github.com/pkg/browser v0.0.0-20210706143420-7d21f8c997e2/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand Down
Loading

0 comments on commit dfb1c9e

Please sign in to comment.