-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replaced Header, MIMEType, and RequestMethod enums with structs for greater flexibility. Opened encoding/decoding methods to public for implementing in additional convenience methods.
- Loading branch information
1 parent
fa2c76e
commit b426177
Showing
10 changed files
with
189 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import Foundation | ||
#if canImport(FoundationNetworking) | ||
import FoundationNetworking | ||
#endif | ||
|
||
public extension HTTP { | ||
/// Command HTTP Header | ||
struct Header: ExpressibleByStringLiteral, Equatable { | ||
public let rawValue: String | ||
|
||
public init(stringLiteral value: StringLiteralType) { | ||
rawValue = value | ||
} | ||
} | ||
} | ||
|
||
extension HTTP.Header: Identifiable { | ||
public var id: String { rawValue } | ||
} | ||
|
||
public extension HTTP.Header { | ||
/// HTTP Header date formatter; RFC1123 | ||
static var dateFormatter: DateFormatter = { | ||
let formatter = DateFormatter() | ||
formatter.dateFormat = "EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'" | ||
formatter.timeZone = TimeZone(identifier: "GMT")! | ||
formatter.locale = Locale(identifier: "en_US_POSIX") | ||
return formatter | ||
}() | ||
} | ||
|
||
public extension HTTP.Header { | ||
/// The Accept request HTTP header advertises which content types, expressed as MIME types, the client is able to | ||
/// understand. | ||
static let accept: Self = "Accept" | ||
/// The HTTP Authorization request header contains the credentials to authenticate a user agent with a server, | ||
/// usually after the server has responded with a 401 Unauthorized status and the WWW-Authenticate header. | ||
static let authorization: Self = "Authorization" | ||
/// The Content-Length entity header is indicating the size of the entity-body, in bytes, sent to the recipient. | ||
static let contentLength: Self = "Content-Length" | ||
/// The Content-MD5 header, may be used as a message integrity check (MIC), to verify that the decoded data are the | ||
/// same data that were initially sent. | ||
static let contentMD5: Self = "Content-MD5" | ||
/// The Content-Type entity header is used to indicate the media type of the resource. | ||
static let contentType: Self = "Content-Type" | ||
/// The Date general HTTP header contains the date and time at which the message was originated. | ||
static let date: Self = "Date" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import Foundation | ||
#if canImport(FoundationNetworking) | ||
import FoundationNetworking | ||
#endif | ||
|
||
public extension HTTP { | ||
/// MIME Types used in the API | ||
struct MIMEType: ExpressibleByStringLiteral, Equatable { | ||
public let rawValue: String | ||
|
||
public init(stringLiteral value: StringLiteralType) { | ||
rawValue = value | ||
} | ||
} | ||
} | ||
|
||
extension HTTP.MIMEType: Identifiable { | ||
public var id: String { rawValue } | ||
} | ||
|
||
public extension HTTP.MIMEType { | ||
/// Any kind of binary data | ||
static let bin: Self = "application/octet-stream" | ||
/// Graphics Interchange Format (GIF) | ||
static let gif: Self = "image/gif" | ||
/// HyperText Markup Language | ||
static let html: Self = "text/html" | ||
/// JPEG images | ||
static let jpeg: Self = "image/jpeg" | ||
/// JavaScript | ||
static let js: Self = "text/javascript" | ||
/// JSON Document | ||
static let json: Self = "application/json" | ||
/// JSON-LD Document | ||
static let jsonld: Self = "application/ld+json" | ||
/// Portable Network Graphics | ||
static let png: Self = "image/png" | ||
/// Adobe Portable Document Format | ||
static let pdf: Self = "application/pdf" | ||
/// Scalable Vector Graphics | ||
static let svg: Self = "image/svg+xml" | ||
/// Text | ||
static let txt: Self = "text/plain" | ||
/// XML | ||
static let xml: Self = "application/xml" | ||
|
||
@available(*, deprecated, renamed: "json") | ||
static var applicationJson: Self { json } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import Foundation | ||
#if canImport(FoundationNetworking) | ||
import FoundationNetworking | ||
#endif | ||
|
||
public extension HTTP { | ||
/// Desired action to be performed for a given resource. | ||
/// | ||
/// Although they can also be nouns, these request methods are sometimes referred as HTTP verbs. | ||
struct RequestMethod: ExpressibleByStringLiteral, Equatable { | ||
public let rawValue: String | ||
|
||
public init(stringLiteral value: String) { | ||
rawValue = value | ||
} | ||
} | ||
} | ||
|
||
extension HTTP.RequestMethod: Identifiable { | ||
public var id: String { rawValue } | ||
} | ||
|
||
public extension HTTP.RequestMethod { | ||
/// The GET method requests a representation of the specified resource. | ||
/// | ||
/// Requests using GET should only retrieve data. | ||
static let get: Self = "GET" | ||
/// The PUT method replaces all current representations of the target resource with the request payload. | ||
static let put: Self = "PUT" | ||
/// The POST method is used to submit an entity to the specified resource, often causing a change in state or side | ||
/// effects on the server. | ||
static let post: Self = "POST" | ||
/// The PATCH method is used to apply partial modifications to a resource. | ||
static let patch: Self = "PATCH" | ||
/// The DELETE method deletes the specified resource. | ||
static let delete: Self = "DELETE" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters