basic http functions

This commit is contained in:
Jeff Carr 2025-09-08 04:02:50 -05:00
parent 1fe6c53267
commit 96cd7ffb2d
1 changed files with 22 additions and 5 deletions

View File

@ -36,6 +36,7 @@ func (pb *Files) makeHTTPFile(pf *File) error {
FRUITS := msg.Name
FRUIT := msg.GuiVarName
// fruitVars := color.Vars
httpSendReply(newf, FRUITS)
httpCustom(newf, FRUITS, FRUIT, FRUIT)
log.Printf("NEED TO ADD GUI FOR %s with var %s and found msg struct %s\n", msg.Name, msg.GuiVarName, color.Lockname)
}
@ -90,21 +91,38 @@ func httpTest(w io.Writer, FRUITS string, fRUITS string, FRUIT string, fRUIT str
fmt.Fprintln(w, "// END TABLE UPDATE")
}
func httpSendReply(w io.Writer, FRUITS string) {
fmt.Fprintln(w, "func (p *"+FRUITS+") SendReply(w http.ResponseWriter, reqPB *httppb.HttpRequest) error {")
fmt.Fprintln(w, " data, err := p.Marshal()")
fmt.Fprintln(w, " if err != nil {")
fmt.Fprintln(w, " // reqPB.Errors = append(reqPB.Errors, log.Sprintf(, err))")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " if len(data) == 0 {")
fmt.Fprintln(w, " // reqPB.Errors = append(reqPB.Errors, \"Patches PB data was nil/emtpy without Marsha() error\")")
fmt.Fprintln(w, " return nil")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " _, err = w.Write(data)")
fmt.Fprintln(w, " if err != nil {")
fmt.Fprintln(w, " // reqPB.Errors = append(reqPB.Errors, log.Sprintf(, i, err))")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " return err")
fmt.Fprintln(w, "}")
}
func httpCustom(w io.Writer, FRUITS string, fRUITS string, FRUIT string) {
fmt.Fprintln(w, "// err handling here isn't great")
fmt.Fprintln(w, "func (p *Repos) HttpPost(baseURL string, route string) (*Repos, *httppb.HttpRequest, error) {")
fmt.Fprintln(w, "func (p *"+FRUITS+") HttpPost(baseURL string, route string) (*"+FRUITS+", *httppb.HttpRequest, error) {")
fmt.Fprintln(w, " // if you ever have 'http://www.wit.com//' GO will regect the server recieving it.")
fmt.Fprintln(w, " // Even though the linux kernel gets the network payload")
fmt.Fprintln(w, " // also it never gives you an error about that, it just goes away invisably inside GO")
fmt.Fprintln(w, " tmpURL, _ := url.Parse(baseURL) // 'http://forge.grid.wit.com:2520')")
fmt.Fprintln(w, " finalURL := tmpURL.JoinPath(\"/repos/\", route)")
fmt.Fprintln(w, " finalURL := tmpURL.JoinPath(\"/"+fRUITS+"/\", route)")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " data, _ := p.Marshal()")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " var err error")
fmt.Fprintln(w, " var req *http.Request")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " log.Info(\"reposs PB HttpPost\", finalURL.String())")
fmt.Fprintln(w, " req, err = http.NewRequest(http.MethodPost, finalURL.String(), bytes.NewBuffer(data))")
fmt.Fprintln(w, " if req == nil {")
fmt.Fprintln(w, " return nil, nil, err")
@ -126,10 +144,9 @@ func httpCustom(w io.Writer, FRUITS string, fRUITS string, FRUIT string) {
fmt.Fprintln(w, " // reqPB.Errors = append(reqPB.Errors, fmt.Sprintf(err2))")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " newpb := NewRepos()")
fmt.Fprintln(w, " newpb := New"+FRUITS+"()")
fmt.Fprintln(w, " err = newpb.Unmarshal(newdata)")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " // log.Info(repos PB HttpPost finalURL.String(), p.Len(), newpb.Len())")
fmt.Fprintln(w, " return newpb, reqPB, err")
fmt.Fprintln(w, "}")
}