File: loading_iface.go

package info (click to toggle)
golang-github-go-openapi-swag 1%3A0.25.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,200 kB
  • sloc: sh: 30; makefile: 3
file content (102 lines) | stat: -rw-r--r-- 3,666 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright 2015 go-swagger maintainers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//	http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package swag

import (
	"encoding/json"
	"time"

	"github.com/go-openapi/swag/loading"
)

var (
	// Package-level defaults for the file loading utilities (deprecated).

	// LoadHTTPTimeout the default timeout for load requests.
	//
	// Deprecated: use [loading.WithTimeout] instead.
	LoadHTTPTimeout = 30 * time.Second

	// LoadHTTPBasicAuthUsername the username to use when load requests require basic auth.
	//
	// Deprecated: use [loading.WithBasicAuth] instead.
	LoadHTTPBasicAuthUsername = ""

	// LoadHTTPBasicAuthPassword the password to use when load requests require basic auth.
	//
	// Deprecated: use [loading.WithBasicAuth] instead.
	LoadHTTPBasicAuthPassword = ""

	// LoadHTTPCustomHeaders an optional collection of custom HTTP headers for load requests.
	//
	// Deprecated: use [loading.WithCustomHeaders] instead.
	LoadHTTPCustomHeaders = map[string]string{}
)

// LoadFromFileOrHTTP loads the bytes from a file or a remote http server based on the provided path.
//
// Deprecated: use [loading.LoadFromFileOrHTTP] instead.
func LoadFromFileOrHTTP(pth string, opts ...loading.Option) ([]byte, error) {
	return loading.LoadFromFileOrHTTP(pth, loadingOptionsWithDefaults(opts)...)
}

// LoadFromFileOrHTTPWithTimeout loads the bytes from a file or a remote http server based on the path passed in
// timeout arg allows for per request overriding of the request timeout.
//
// Deprecated: use [loading.LoadFileOrHTTP] with the [loading.WithTimeout] option instead.
func LoadFromFileOrHTTPWithTimeout(pth string, timeout time.Duration, opts ...loading.Option) ([]byte, error) {
	opts = append(opts, loading.WithTimeout(timeout))

	return LoadFromFileOrHTTP(pth, opts...)
}

// LoadStrategy returns a loader function for a given path or URL.
//
// Deprecated: use [loading.LoadStrategy] instead.
func LoadStrategy(pth string, local, remote func(string) ([]byte, error), opts ...loading.Option) func(string) ([]byte, error) {
	return loading.LoadStrategy(pth, local, remote, loadingOptionsWithDefaults(opts)...)
}

// YAMLMatcher matches yaml for a file loader.
//
// Deprecated: use [loading.YAMLMatcher] instead.
func YAMLMatcher(path string) bool { return loading.YAMLMatcher(path) }

// YAMLDoc loads a yaml document from either http or a file and converts it to json.
//
// Deprecated: use [loading.YAMLDoc] instead.
func YAMLDoc(path string) (json.RawMessage, error) {
	return loading.YAMLDoc(path)
}

// YAMLData loads a yaml document from either http or a file.
//
// Deprecated: use [loading.YAMLData] instead.
func YAMLData(path string) (interface{}, error) {
	return loading.YAMLData(path)
}

// loadingOptionsWithDefaults bridges deprecated default settings that use package-level variables,
// with the recommended use of loading.Option.
func loadingOptionsWithDefaults(opts []loading.Option) []loading.Option {
	o := []loading.Option{
		loading.WithTimeout(LoadHTTPTimeout),
		loading.WithBasicAuth(LoadHTTPBasicAuthUsername, LoadHTTPBasicAuthPassword),
		loading.WithCustomHeaders(LoadHTTPCustomHeaders),
	}
	o = append(o, opts...)

	return o
}