ClientFiles returns the generated HTTP client files.
(genpkg string, data *httpcodegen.ServicesData)
| 13 | |
| 14 | // ClientFiles returns the generated HTTP client files. |
| 15 | func ClientFiles(genpkg string, data *httpcodegen.ServicesData) []*codegen.File { |
| 16 | jsvcs := data.Root.API.JSONRPC.Services |
| 17 | files := make([]*codegen.File, 0, len(jsvcs)*3) |
| 18 | for _, svc := range jsvcs { |
| 19 | files = append(files, clientFile(genpkg, svc, data)) |
| 20 | if f := websocketClientFile(genpkg, svc, data); f != nil { |
| 21 | files = append(files, f) |
| 22 | } |
| 23 | if f := sseClientFile(genpkg, svc, data); f != nil { |
| 24 | files = append(files, f) |
| 25 | } |
| 26 | } |
| 27 | for _, svc := range jsvcs { |
| 28 | f := httpcodegen.ClientEncodeDecodeFile(genpkg, svc, data) |
| 29 | if f == nil { |
| 30 | continue |
| 31 | } |
| 32 | updateHeader(f) |
| 33 | var sections []*codegen.SectionTemplate |
| 34 | for _, s := range f.SectionTemplates { |
| 35 | switch s.Name { |
| 36 | case "source-header": |
| 37 | codegen.AddImport(s, &codegen.ImportSpec{Path: "bufio"}) |
| 38 | codegen.AddImport(s, &codegen.ImportSpec{Path: "bytes"}) |
| 39 | codegen.AddImport(s, &codegen.ImportSpec{Path: "sync"}) |
| 40 | codegen.AddImport(s, &codegen.ImportSpec{Path: "sync/atomic"}) |
| 41 | codegen.AddImport(s, &codegen.ImportSpec{Path: "github.com/google/uuid"}) |
| 42 | codegen.AddImport(s, codegen.GoaImport("jsonrpc")) |
| 43 | case "request-encoder": |
| 44 | re := regexp.MustCompile(`body := (.*)\n`) |
| 45 | s.Source = re.ReplaceAllStringFunc(s.Source, func(match string) string { |
| 46 | matches := re.FindStringSubmatch(match) |
| 47 | return strings.Replace(newJSONRPCBody, "{{ .NewBody }}", matches[1], 1) |
| 48 | }) |
| 49 | case "response-decoder": |
| 50 | s.Source = jsonrpcTemplates.Read(responseDecoderT, singleResponseP, queryTypeConversionP, elementSliceConversionP, sliceItemConversionP) |
| 51 | } |
| 52 | s.Name = "jsonrpc-" + s.Name |
| 53 | sections = append(sections, s) |
| 54 | } |
| 55 | |
| 56 | // For JSON-RPC methods without request encoders, add one |
| 57 | for _, endpoint := range data.Get(svc.Name()).Endpoints { |
| 58 | if endpoint.RequestEncoder == "" { |
| 59 | // Add the encoder function |
| 60 | encoderSection := &codegen.SectionTemplate{ |
| 61 | Name: "jsonrpc-minimal-request-encoder", |
| 62 | Source: jsonrpcTemplates.Read("minimal_request_encoder"), |
| 63 | Data: endpoint, |
| 64 | } |
| 65 | sections = append(sections, encoderSection) |
| 66 | // Update endpoint data to reference the encoder |
| 67 | endpoint.RequestEncoder = fmt.Sprintf("Encode%sRequest", endpoint.Method.VarName) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | f.SectionTemplates = sections |
| 72 | f.Path = strings.Replace(f.Path, "/http/", "/jsonrpc/", 1) |