Debian Codegraph @

helpers/operation_utilities.go

raw
1 // Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
2 // SPDX-License-Identifier: MIT
3
4 package helpers
5
6 import (
7 "mime"
8 "net/http"
9
10 "github.com/pb33f/libopenapi/datamodel/high/v3"
11 )
12
13 // ExtractOperation extracts the operation from the path item based on the request method. If there is no
14 // matching operation found, then nil is returned.
15 func ExtractOperation(request *http.Request, item *v3.PathItem) *v3.Operation {
16 switch request.Method {
17 case http.MethodGet:
18 return item.Get
19 case http.MethodPost:
20 return item.Post
21 case http.MethodPut:
22 return item.Put
23 case http.MethodDelete:
24 return item.Delete
25 case http.MethodOptions:
26 return item.Options
27 case http.MethodHead:
28 if item.Head != nil {
29 return item.Head
30 }
31 return item.Get
32 case http.MethodPatch:
33 return item.Patch
34 case http.MethodTrace:
35 return item.Trace
36 }
37 return nil
38 }
39
40 // ExtractContentType extracts the content type from the request header. First return argument is the content type
41 // of the request.The second (optional) argument is the charset of the request. The third (optional)
42 // argument is the boundary of the type (only used with forms really).
43 func ExtractContentType(contentType string) (string, string, string) {
44 // mime.ParseMediaType: "If there is an error parsing the optional parameter,
45 // the media type will be returned along with the error ErrInvalidMediaParameter."
46 ct, params, _ := mime.ParseMediaType(contentType)
47 return ct, params["charset"], params["boundary"]
48 }