Skip to content

Commit

Permalink
🚀 Release: v0.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
MrSkwiggs authored Aug 20, 2021
2 parents 58a5cc3 + 71ca956 commit b32d34a
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
# * https://www.objc.io/issues/6-build-tools/travis-ci/
# * https://github.com/supermarin/xcpretty#usage

osx_image: xcode11.3
osx_image: xcode12.5.1
language: swift
#cache: cocoapods
#podfile: Netswift/Podfile
#before_install:
# - gem install cocoapods # Since Travis is not always on latest version
# - pod install --project-directory=Netswift
script:
- set -o pipefail && xcodebuild test -enableCodeCoverage YES -workspace Example/Netswift.xcworkspace -scheme Netswift-Example -sdk iphonesimulator13.2 -destination 'platform=iOS Simulator,name=iPhone 11' ONLY_ACTIVE_ARCH=NO | xcpretty
- set -o pipefail && xcodebuild test -enableCodeCoverage YES -workspace Example/Netswift.xcworkspace -scheme Netswift-Example -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 12' ONLY_ACTIVE_ARCH=NO | xcpretty
- pod lib lint
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@
# Change Log
All notable changes to this project will be documented in this file

## [0.5.0 (20210820)]
### Added
- New `NetswiftEncoder` wrapper protocol for types such as `JSONEncoder` or `PropertyListEncoder` (those 2 are already made to conform to `NetswiftEncoder`)

### Changed
- `NetswiftRequest` now has new requirements:
- A `bodyEncoder: NetswiftEncoder?` var, which can be used to encode any data into the request's `httpBody`;
- A `body(encodedBy encoder: NetswiftEncoder?) -> Data?` function that uses the given encoder to return `Data?`, if applicable.

## [0.4.0]
### Changed
- HTTP Status Codes are now included in `NetswiftError.Category`

## [0.3.1 (20200824)]
### Changed
- `NetswiftError.Category` now conforms to `CustomDebugStringConvertible`
Expand Down
14 changes: 9 additions & 5 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions Sources/Netswift/Core/NetswiftEncoder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// NetswiftEncoder.swift
// Netswift
//
// Created by Dorian Grolaux on 20/08/2021.
//

import Foundation

/**
A convenience wrapper for common encoders
*/
public protocol NetswiftEncoder {
func encode<T: Encodable>(_ value: T) throws -> Data
}

extension JSONEncoder: NetswiftEncoder {}
extension PropertyListEncoder: NetswiftEncoder {}
27 changes: 26 additions & 1 deletion Sources/Netswift/Core/NetswiftRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ public protocol NetswiftRequest {
*/
var contentType: MimeType { get }

/**
Specifies which encoder should be used for encoding this request's body
- important: Defaults to `JSONEncoder()`
*/
var bodyEncoder: NetswiftEncoder? { get }

/**
Encodes any arbitrary data defined by this request with the given encoder to be used as the request's body.
- important: Returns `nil` by default
*/
func body(encodedBy encoder: NetswiftEncoder?) -> Data?

/**
Specifies what type of content this request expects back.
Expand Down Expand Up @@ -68,7 +82,6 @@ public protocol NetswiftRequest {
*/
func deserialise(_ incomingData: IncomingType) -> NetswiftResult<Response>


/**
Tries to intercept and handle an error thrown while the Request is being performed.
Expand All @@ -88,6 +101,14 @@ public extension NetswiftRequest {
return .json
}

var bodyEncoder: NetswiftEncoder? {
return JSONEncoder()
}

func body(encodedBy encoder: NetswiftEncoder?) -> Data? {
return nil
}

var accept: MimeType {
return .json
}
Expand All @@ -109,6 +130,10 @@ public extension NetswiftRequest where Self: NetswiftRoute {
headers.append(.contentType(contentType))
headers.append(.accept(accept))

if let encoder = bodyEncoder {
request.httpBody = body(encodedBy: encoder)
}

request.addHeaders(headers)

return .success(request)
Expand Down

0 comments on commit b32d34a

Please sign in to comment.