|
| 1 | +// Copyright (c) 2024 Z5Labs and Contributors |
| 2 | +// |
| 3 | +// This software is released under the MIT License. |
| 4 | +// https://opensource.org/licenses/MIT |
| 5 | + |
| 6 | +package endpoint |
| 7 | + |
| 8 | +import ( |
| 9 | + "bytes" |
| 10 | + "context" |
| 11 | + "io" |
| 12 | + "net/http" |
| 13 | + "net/textproto" |
| 14 | + "strconv" |
| 15 | + "strings" |
| 16 | + |
| 17 | + "github.com/z5labs/humus/example/internal/petstorepb" |
| 18 | + |
| 19 | + "github.com/swaggest/openapi-go/openapi3" |
| 20 | + "github.com/z5labs/bedrock/pkg/ptr" |
| 21 | + "github.com/z5labs/humus/rest" |
| 22 | + "go.opentelemetry.io/otel" |
| 23 | + "google.golang.org/protobuf/proto" |
| 24 | + "google.golang.org/protobuf/types/known/emptypb" |
| 25 | +) |
| 26 | + |
| 27 | +type GetImageIndex interface { |
| 28 | + GetImage(context.Context, int64) ([]byte, bool) |
| 29 | +} |
| 30 | + |
| 31 | +type downloadHandler struct { |
| 32 | + store PetByIdStore |
| 33 | + images GetImageIndex |
| 34 | +} |
| 35 | + |
| 36 | +func Download(store PetByIdStore, images GetImageIndex) rest.Endpoint { |
| 37 | + h := &downloadHandler{ |
| 38 | + store: store, |
| 39 | + images: images, |
| 40 | + } |
| 41 | + |
| 42 | + return rest.NewEndpoint( |
| 43 | + http.MethodGet, |
| 44 | + "/download/{id}", |
| 45 | + rest.ConsumesProto( |
| 46 | + rest.ProducesMultipartFormData(h), |
| 47 | + ), |
| 48 | + rest.PathParams( |
| 49 | + rest.PathParam{ |
| 50 | + Name: "id", |
| 51 | + Required: true, |
| 52 | + }, |
| 53 | + ), |
| 54 | + rest.Returns(http.StatusBadRequest), |
| 55 | + ) |
| 56 | +} |
| 57 | + |
| 58 | +type DownloadResponse struct { |
| 59 | + pet *petstorepb.Pet |
| 60 | + imageContent io.Reader |
| 61 | +} |
| 62 | + |
| 63 | +func (DownloadResponse) OpenApiV3Schema() (*openapi3.Schema, error) { |
| 64 | + var req rest.ProtoRequest[petstorepb.Pet, *petstorepb.Pet] |
| 65 | + metadataSchema, err := req.OpenApiV3Schema() |
| 66 | + if err != nil { |
| 67 | + return nil, err |
| 68 | + } |
| 69 | + |
| 70 | + var schema openapi3.Schema |
| 71 | + schema.WithType(openapi3.SchemaTypeObject) |
| 72 | + schema.WithProperties(map[string]openapi3.SchemaOrRef{ |
| 73 | + "pet": { |
| 74 | + Schema: metadataSchema, |
| 75 | + }, |
| 76 | + "image": { |
| 77 | + Schema: &openapi3.Schema{ |
| 78 | + Type: ptr.Ref(openapi3.SchemaTypeString), |
| 79 | + Format: ptr.Ref("binary"), |
| 80 | + }, |
| 81 | + }, |
| 82 | + }) |
| 83 | + return &schema, nil |
| 84 | +} |
| 85 | + |
| 86 | +func (resp *DownloadResponse) WriteParts(mw rest.MultipartWriter) error { |
| 87 | + b, err := proto.Marshal(resp.pet) |
| 88 | + if err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + |
| 92 | + part, err := mw.CreatePart(textproto.MIMEHeader{}) |
| 93 | + if err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + |
| 97 | + _, err = io.Copy(part, bytes.NewReader(b)) |
| 98 | + if err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + |
| 102 | + part, err = mw.CreatePart(textproto.MIMEHeader{}) |
| 103 | + if err != nil { |
| 104 | + return err |
| 105 | + } |
| 106 | + _, err = io.Copy(part, resp.imageContent) |
| 107 | + return err |
| 108 | +} |
| 109 | + |
| 110 | +func (h *downloadHandler) Handle(ctx context.Context, req *emptypb.Empty) (*DownloadResponse, error) { |
| 111 | + spanCtx, span := otel.Tracer("endpoint").Start(ctx, "downloadHandler.Handle") |
| 112 | + defer span.End() |
| 113 | + |
| 114 | + pathId := rest.PathValue(ctx, "id") |
| 115 | + pathId = strings.TrimSpace(pathId) |
| 116 | + if len(pathId) == 0 { |
| 117 | + return nil, rest.Error(http.StatusBadRequest, "missing pet id") |
| 118 | + } |
| 119 | + |
| 120 | + id, err := strconv.ParseInt(pathId, 10, 64) |
| 121 | + if err != nil { |
| 122 | + span.RecordError(err) |
| 123 | + return nil, rest.Error(http.StatusBadRequest, "pet id must be an integer") |
| 124 | + } |
| 125 | + |
| 126 | + pet, found := h.store.Get(spanCtx, id) |
| 127 | + if !found { |
| 128 | + return nil, rest.Error(http.StatusNotFound, "") |
| 129 | + } |
| 130 | + |
| 131 | + resp := &DownloadResponse{ |
| 132 | + pet: pet, |
| 133 | + imageContent: nil, |
| 134 | + } |
| 135 | + return resp, nil |
| 136 | +} |
0 commit comments