ClientEncodeDecodeFile returns the file containing the HTTP client encoding and decoding logic.
(genpkg string, svc *expr.HTTPServiceExpr, services *ServicesData)
| 32 | // ClientEncodeDecodeFile returns the file containing the HTTP client encoding |
| 33 | // and decoding logic. |
| 34 | func ClientEncodeDecodeFile(genpkg string, svc *expr.HTTPServiceExpr, services *ServicesData) *codegen.File { |
| 35 | data := services.Get(svc.Name()) |
| 36 | svcName := data.Service.PathName |
| 37 | path := filepath.Join(codegen.Gendir, "http", svcName, "client", "encode_decode.go") |
| 38 | title := fmt.Sprintf("%s HTTP client encoders and decoders", svc.Name()) |
| 39 | imports := []*codegen.ImportSpec{ |
| 40 | {Path: "bytes"}, |
| 41 | {Path: "context"}, |
| 42 | {Path: "encoding/json"}, |
| 43 | {Path: "fmt"}, |
| 44 | {Path: "io"}, |
| 45 | {Path: "mime/multipart"}, |
| 46 | {Path: "net/http"}, |
| 47 | {Path: "net/url"}, |
| 48 | {Path: "os"}, |
| 49 | {Path: "strconv"}, |
| 50 | {Path: "strings"}, |
| 51 | {Path: "unicode/utf8"}, |
| 52 | codegen.GoaImport(""), |
| 53 | codegen.GoaNamedImport("http", "goahttp"), |
| 54 | {Path: genpkg + "/" + svcName, Name: data.Service.PkgName}, |
| 55 | {Path: genpkg + "/" + svcName + "/" + "views", Name: data.Service.ViewsPkg}, |
| 56 | } |
| 57 | sections := []*codegen.SectionTemplate{codegen.Header(title, "client", imports)} |
| 58 | |
| 59 | for _, e := range data.Endpoints { |
| 60 | sections = append(sections, &codegen.SectionTemplate{ |
| 61 | Name: "request-builder", |
| 62 | Source: httpTemplates.Read(requestBuilderT), |
| 63 | Data: e, |
| 64 | }) |
| 65 | if e.RequestEncoder != "" && e.Payload.Ref != "" { |
| 66 | sections = append(sections, &codegen.SectionTemplate{ |
| 67 | Name: "request-encoder", |
| 68 | Source: httpTemplates.Read(requestEncoderT, clientTypeConversionP, clientMapConversionP), |
| 69 | FuncMap: map[string]any{ |
| 70 | "typeConversionData": typeConversionData, |
| 71 | "mapConversionData": mapConversionData, |
| 72 | "goTypeRef": func(dt expr.DataType) string { |
| 73 | return services.ServicesData.Get(svc.Name()).Scope.GoTypeRef(&expr.AttributeExpr{Type: dt}) |
| 74 | }, |
| 75 | "isBearer": isBearer, |
| 76 | "aliasedType": fieldType, |
| 77 | "isAlias": func(dt expr.DataType) bool { |
| 78 | _, ok := dt.(expr.UserType) |
| 79 | return ok |
| 80 | }, |
| 81 | "underlyingType": func(dt expr.DataType) expr.DataType { |
| 82 | if ut, ok := dt.(expr.UserType); ok { |
| 83 | return ut.Attribute().Type |
| 84 | } |
| 85 | return dt |
| 86 | }, |
| 87 | "requestStructPkg": requestStructPkg, |
| 88 | }, |
| 89 | Data: e, |
| 90 | }) |
| 91 | } |