templates

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 2, 2020 License: BSD-2-Clause Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Curl = RequestTemplate{
	RequestStr: `curl -X {{.Method}} {{.URL}}{{if .Headers}} \{{else if .Body}} \{{end}}
{{range $name, $value := .Headers}} -H {{$name}} {{range $internal := $value}}{{$internal}}{{end}} \{{end}}
{{if .Body}}
-d '{{.Body}}'
{{end}}`,
}

Curl : template

View Source
var Go = RequestTemplate{
	ClientStr: `package main
import (
  "fmt"
  "io/ioutil"
  "net/http"
  "strings"
)
// Client : my client
type Client struct { }
{{.Code}}
	`,
	FunctionStr: `func (c Client) {{.Label}}() {
{{.Code}}
}
		`,
	RequestStr: `req, err := http.NewRequest("{{.Method}}", "{{.URL}}", {{if .Body}}strings.NewReader(` + "`" + `{{.Body}}` + "`" + `){{else}}nil{{end}})
{{range $name, $value := .Headers}}req.Header.Set("{{$name}}", "{{range $internal := $value}}{{$internal}}{{end}}")
{{end}}
res, err := http.DefaultClient.Do(req)
if err != nil {
  fmt.Println(err)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
  fmt.Println(err)
}
fmt.Println(string(body))`,
}

Go : template

View Source
var Javascript = RequestTemplate{
	ClientStr: `
	class Client {
	{{- .Code -}}
	}
	`,
	FunctionStr: `{{if not .Client}}function {{end}}{{.Label}}() {
	{{.Code}}
	}`,
	RequestStr: `fetch('{{.URL}}', {
  method: '{{.Method}}',
{{- if .Headers}}
  headers: {
{{range $name, $value := .Headers}}    '{{$name}}': '{{range $internal := $value}}{{$internal}}{{end}}',
{{end}}  },
{{- end}}
{{- if .Body}}
  body: JSON.stringify({{.Body}}),
{{- end}}
})
  .then((res) => res.json().then((data) => ({ status: res.status, data })))
  .then(({ status, data }) => console.log(status, data))`,
}

Javascript : template

View Source
var Node = RequestTemplate{
	ClientStr: `const fetch = require('node-fetch')
	class Client {
	{{- .Code -}}
	}
	`,
	FunctionStr: `{{if not .Client}}function {{end}}{{.Label}}() {
	{{.Code}}
	}`,
	RequestStr: `fetch('{{.URL}}', {
  method: '{{.Method}}',
{{- if .Headers}}
  headers: {
{{range $name, $value := .Headers}}    '{{$name}}': '{{range $internal := $value}}{{$internal}}{{end}}',
{{end}}  },
{{- end}}
{{- if .Body}}
  body: JSON.stringify({{.Body}}),
{{- end}}
})
  .then((res) => res.json().then((data) => ({ status: res.status, data })))
  .then(({ status, data }) => console.log(status, data))`,
}

Node : template

Functions

This section is empty.

Types

type RequestTemplate

type RequestTemplate struct {
	ClientStr   string
	FunctionStr string
	RequestStr  string
	Client      *template.Template
	Function    *template.Template
	Request     *template.Template
}

RequestTemplate : for building onther language requests

func Get

func Get(name string) *RequestTemplate

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL