-
Notifications
You must be signed in to change notification settings - Fork 45
URLTemplate
A URL that may contain variable placeholders.
public struct URLTemplate: Hashable, RawRepresentable Variable placeholders are enclosed with curly braces. They can exist anywhere within the URL.
Examples:
// no variables
"https://website.com/a/path?query=value"
// scheme
"{scheme}://website.com/a/path?query=value"
// host
"https://{host}/a/path?query=value"
// just the domain
"https://{domain}.com/a/path?query=value"
// the whole url
"{server}"
// etc.
A URLTemplate that does not contain any variables can be turned into
a Foundation URL (assuming it is an otherwise valid URL) which you can
access with the url property.
A URLTemplate that contains variables can be turned into a template
with no variables (and subsequently a Foundation URL) with the
replacing(_:) function that takes a dictionary of variable values to insert
into the template. You can also choose to only replace some of the variables this
way.
Decodable, Encodable, Hashable, RawRepresentable
Create a URLTemplate from the string if possible.
public init?(rawValue: String) A non-throwing version of init(templateString:).
Create a URLTemplate from the string if possible.
public init(templateString: String) throws A throwing version of init?(rawValue:).
Create a constant (no variables) URLTemplate from the given URL.
public init(url: URL) Construct a URL template from the given components. Neighboring .constant
components in the array will be combined.
public init(reducingComponents components: [Component]) public init(from decoder: Decoder) throws The string value of the URL.
public let rawValue: StringThis is equivalent to the absoluteString
(i.e. the entire URL).
The variable and constant components of teh URL.
public let components: [Component]Variables are enclosed in curly braces ({variable})
and everything between variables is "constant."
In the URL {scheme}://website.com/{path}
there are two variables (scheme and path) separated
by the "://website.com/" portion which is a constant.
The entire URL as a string.
public var absoluteString: String This is equivalent to the absoluteString provided
by the Foundation URL type except that a
URLTemplate's absoluteString can contain
variable placeholders.
Get a URL from this templated URL if it is a valid URL already.
public var url: URL? Templated URLs with variables in them will not be valid URLs
and are therefore guaranteed to return nil.
Get the names of all variables in the URL Template.
public var variables: [String] For all variables in this template that have keys in the given dictionary, replace the variable with the value under the given key.
public func replacing(_ variableDictionary: [String : String]) -> URLTemplate This results in a new URLTemplate with a constant value in place of all
variables that were replaced. The URLTemplate gets reformed, so any
constants formed by such a replacement that neighbor other constants
become one constant (there will never be two constants next to each other,
even after variable replacement occurs).
If you have replaced all variables in the template, you can attempt to create a
well-formed Foundation URL by accessing the url property of the resulting
URLTemplate.
Example
"{scheme}://{website}.com/{path}"
[
.variable(name: "scheme"),
.constant("://"),
.variable(name: "website"),
.constant(".com/")
.variable(name: "path")
]
-> replacing(["scheme": "https", "path": "welcome"])
"https://{website}.com/welcome"
[
.constant("https://"),
.variable(name: "website"),
.constant(".com/welcome")
]
If you want to create a valid URL from the template, all variables must be
replaced. Picking up with the above example, we could replace the last variable
and then request the URLTemplate url property.
Example
-> replacing(["website": "mysite"])
"https://mysite.com/welcome"
[
.constant("https://mysite.com/welcome")
]
public func encode(to encoder: Encoder) throws .
Types
- AnyCodable
- DereferencedContent
- DereferencedContentEncoding
- DereferencedDocument
- DereferencedDocument.Route
- DereferencedHeader
- DereferencedJSONSchema
- DereferencedJSONSchema.ArrayContext
- DereferencedJSONSchema.ObjectContext
- DereferencedOperation
- DereferencedOperation.ResponseOutcome
- DereferencedParameter
- DereferencedPathItem
- DereferencedPathItem.Endpoint
- DereferencedRequest
- DereferencedResponse
- DereferencedSchemaContext
- DereferencedSecurityRequirement
- DereferencedSecurityRequirement.ScopedScheme
- Either
- EitherDecodeNoTypesMatchedError
- EitherDecodeNoTypesMatchedError.IndividualFailure
- ErrorCategory
- ErrorCategory.KeyValue
- InconsistencyError
- JSONReference
- JSONReference.InternalReference
- JSONReference.Path
- JSONReference.PathComponent
- JSONSchema
- JSONSchema.ArrayContext
- JSONSchema.CoreContext
- JSONSchema.CoreContext.Permissions
- JSONSchema.IntegerContext
- JSONSchema.IntegerContext.Bound
- JSONSchema.NumericContext
- JSONSchema.NumericContext.Bound
- JSONSchema.ObjectContext
- JSONSchema.StringContext
- JSONSchemaResolutionError
- JSONType
- JSONTypeFormat
- JSONTypeFormat.AnyFormat
- JSONTypeFormat.ArrayFormat
- JSONTypeFormat.BooleanFormat
- JSONTypeFormat.IntegerFormat
- JSONTypeFormat.IntegerFormat.Extended
- JSONTypeFormat.NumberFormat
- JSONTypeFormat.ObjectFormat
- JSONTypeFormat.StringFormat
- JSONTypeFormat.StringFormat.Extended
- OpenAPI
- OpenAPI.CallbackURL
- OpenAPI.ComponentKey
- OpenAPI.Components
- OpenAPI.Components.ReferenceCycleError
- OpenAPI.Components.ReferenceError
- OpenAPI.Content
- OpenAPI.Content.Encoding
- OpenAPI.ContentType
- OpenAPI.Discriminator
- OpenAPI.Document
- OpenAPI.Document.Info
- OpenAPI.Document.Info.Contact
- OpenAPI.Document.Info.License
- OpenAPI.Document.Route
- OpenAPI.Document.Version
- OpenAPI.Error
- OpenAPI.Error.Decoding
- OpenAPI.Error.Decoding.Document
- OpenAPI.Error.Decoding.Document.Context
- OpenAPI.Error.Decoding.Operation
- OpenAPI.Error.Decoding.Operation.Context
- OpenAPI.Error.Decoding.Path
- OpenAPI.Error.Decoding.Path.Context
- OpenAPI.Error.Decoding.Request
- OpenAPI.Error.Decoding.Request.Context
- OpenAPI.Error.Decoding.Response
- OpenAPI.Error.Decoding.Response.Context
- OpenAPI.Example
- OpenAPI.ExternalDocumentation
- OpenAPI.Header
- OpenAPI.HttpMethod
- OpenAPI.Link
- OpenAPI.OAuthFlows
- OpenAPI.OAuthFlows.AuthorizationCode
- OpenAPI.OAuthFlows.ClientCredentials
- OpenAPI.OAuthFlows.CommonFields
- OpenAPI.OAuthFlows.Implicit
- OpenAPI.OAuthFlows.Password
- OpenAPI.Operation
- OpenAPI.Operation.ResponseOutcome
- OpenAPI.Parameter
- OpenAPI.Parameter.Context
- OpenAPI.Parameter.Context.Location
- OpenAPI.Parameter.SchemaContext
- OpenAPI.Parameter.SchemaContext.Style
- OpenAPI.Path
- OpenAPI.PathItem
- OpenAPI.PathItem.Endpoint
- OpenAPI.Request
- OpenAPI.Response
- OpenAPI.Response.StatusCode
- OpenAPI.Response.StatusCode.Range
- OpenAPI.RuntimeExpression
- OpenAPI.RuntimeExpression.Source
- OpenAPI.SecurityScheme
- OpenAPI.SecurityScheme.Location
- OpenAPI.SecurityScheme.SecurityType
- OpenAPI.SecurityScheme.SecurityType.Name
- OpenAPI.Server
- OpenAPI.Server.Variable
- OpenAPI.Tag
- OpenAPI.XML
- OrderedDictionary
- OrderedDictionary.Iterator
- ResolvedDocument
- ResolvedEndpoint
- ResolvedRoute
- URLTemplate
- URLTemplate.Component
- Validation
- ValidationContext
- ValidationError
- ValidationErrorCollection
- Validator
- Validator.CodingKey
Protocols
Global Functions
Extensions
- Array
- Bool
- Dictionary
- Double
- Float
- Int
- Int32
- Int64
- OpenAPI.Callbacks
- OpenAPI.Content.Encoding
- OpenAPI.Document.Info
- OpenAPI.Document.Info.Contact
- OpenAPI.Document.Info.License
- OpenAPI.Error.Decoding
- OpenAPI.Error.Decoding.Document
- OpenAPI.Error.Decoding.Operation
- OpenAPI.Error.Decoding.Path
- OpenAPI.Error.Decoding.Request
- OpenAPI.Error.Decoding.Response
- OpenAPI.OAuthFlows.AuthorizationCode
- OpenAPI.OAuthFlows.ClientCredentials
- OpenAPI.OAuthFlows.CommonFields
- OpenAPI.OAuthFlows.Implicit
- OpenAPI.OAuthFlows.Password
- OpenAPI.Parameter.Context
- OpenAPI.Parameter.SchemaContext
- OpenAPI.Response.StatusCode
- OpenAPI.Server.Variable
- Optional
- String
- URL
- UUID