Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POSTCODE API #9

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions APIClient.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

/* Begin PBXBuildFile section */
078B558C6C77F3C6DCDC4678 /* Pods_APIClientUITests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 82531BA6D6228998A00AB5B6 /* Pods_APIClientUITests.framework */; };
17B83C331D0313F600164152 /* PostCodeApi.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17B83C321D0313F600164152 /* PostCodeApi.swift */; };
17B83C371D046BF600164152 /* MainViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17B83C361D046BF600164152 /* MainViewController.swift */; };
872A275C1CF0D87100A988C4 /* APIResource.swift in Sources */ = {isa = PBXBuildFile; fileRef = 872A275B1CF0D87100A988C4 /* APIResource.swift */; };
874D06591CEF295E009A494D /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 874D06581CEF295E009A494D /* AppDelegate.swift */; };
874D065E1CEF295E009A494D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 874D065C1CEF295E009A494D /* Main.storyboard */; };
Expand Down Expand Up @@ -35,6 +37,8 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
17B83C321D0313F600164152 /* PostCodeApi.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PostCodeApi.swift; sourceTree = "<group>"; };
17B83C361D046BF600164152 /* MainViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MainViewController.swift; sourceTree = "<group>"; };
198B23CD94C6D84016A5208A /* Pods-APIClientUITests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-APIClientUITests.release.xcconfig"; path = "Pods/Target Support Files/Pods-APIClientUITests/Pods-APIClientUITests.release.xcconfig"; sourceTree = "<group>"; };
4FFA0A7AA37697BD5CBC3211 /* Pods_APIClientTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_APIClientTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
65C820A0F52E3181871F5ECA /* Pods-APIClientTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-APIClientTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-APIClientTests/Pods-APIClientTests.release.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -88,6 +92,7 @@
872A27571CF0D4E200A988C4 /* View Controller */ = {
isa = PBXGroup;
children = (
17B83C361D046BF600164152 /* MainViewController.swift */,
);
name = "View Controller";
sourceTree = "<group>";
Expand Down Expand Up @@ -142,6 +147,7 @@
874D06571CEF295E009A494D /* APIClient */ = {
isa = PBXGroup;
children = (
17B83C321D0313F600164152 /* PostCodeApi.swift */,
874D06581CEF295E009A494D /* AppDelegate.swift */,
874D065C1CEF295E009A494D /* Main.storyboard */,
872A27571CF0D4E200A988C4 /* View Controller */,
Expand Down Expand Up @@ -469,7 +475,9 @@
buildActionMask = 2147483647;
files = (
872A275C1CF0D87100A988C4 /* APIResource.swift in Sources */,
17B83C331D0313F600164152 /* PostCodeApi.swift in Sources */,
874D06591CEF295E009A494D /* AppDelegate.swift in Sources */,
17B83C371D046BF600164152 /* MainViewController.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
13 changes: 13 additions & 0 deletions APIClient/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,27 @@
//

import UIKit
import Moya


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

// PostCodeApi Provider represents the Server
let postCodeApi = MoyaProvider<Zippopotam>()


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

// pass PostCodeApi Provider on to the root vier controller
if let mainViewController = (window?.rootViewController as? UINavigationController)?.topViewController as? MainViewController {

// Pass the PokeAPI Provider on to the root view controller
mainViewController.postCodeApi = postCodeApi

}
return true
}

Expand Down
2 changes: 1 addition & 1 deletion APIClient/Base.lproj/LaunchScreen.storyboard
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10117" systemVersion="15F34" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" initialViewController="01J-lp-oVM">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10116" systemVersion="15B42" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" initialViewController="01J-lp-oVM">
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
Expand Down
60 changes: 56 additions & 4 deletions APIClient/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10117" systemVersion="15F34" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="E10-YL-lxp">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10116" systemVersion="15B42" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="E10-YL-lxp">
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
</dependencies>
<scenes>
<!--Navigation Controller-->
Expand All @@ -21,24 +22,75 @@
</objects>
<point key="canvasLocation" x="-658" y="301"/>
</scene>
<!--View Controller-->
<!--Main View Controller-->
<scene sceneID="adh-8k-Iuh">
<objects>
<viewController id="rPr-Fp-ALE" sceneMemberID="viewController">
<viewController id="rPr-Fp-ALE" customClass="MainViewController" customModule="APIClient" customModuleProvider="target" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="pj8-zF-1z6"/>
<viewControllerLayoutGuide type="bottom" id="kt2-9S-3TK"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="rEB-Za-Zhx">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" misplaced="YES" axis="vertical" alignment="center" translatesAutoresizingMaskIntoConstraints="NO" id="4UP-rx-6RK">
<rect key="frame" x="34" y="170" width="500" height="71"/>
<subviews>
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="uvy-OW-5IC">
<rect key="frame" x="20" y="0.0" width="460" height="30"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits"/>
<connections>
<action selector="entered:" destination="rPr-Fp-ALE" eventType="primaryActionTriggered" id="a7a-Hz-HWx"/>
</connections>
</textField>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" misplaced="YES" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="R1u-8d-oWw">
<rect key="frame" x="20" y="30" width="460" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" misplaced="YES" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="1yi-sb-Ils">
<rect key="frame" x="20" y="51" width="460" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<constraints>
<constraint firstItem="R1u-8d-oWw" firstAttribute="leading" secondItem="4UP-rx-6RK" secondAttribute="leading" constant="20" id="0WC-Th-lSG"/>
<constraint firstItem="uvy-OW-5IC" firstAttribute="leading" secondItem="4UP-rx-6RK" secondAttribute="leading" constant="20" id="3ob-tx-UBi"/>
<constraint firstAttribute="trailing" secondItem="uvy-OW-5IC" secondAttribute="trailing" constant="20" id="5ns-xL-gbA"/>
<constraint firstItem="1yi-sb-Ils" firstAttribute="leading" secondItem="4UP-rx-6RK" secondAttribute="leading" constant="20" id="KXb-hq-PP8"/>
<constraint firstAttribute="trailing" secondItem="1yi-sb-Ils" secondAttribute="trailing" constant="20" id="Mex-jl-hv1"/>
<constraint firstAttribute="trailing" secondItem="R1u-8d-oWw" secondAttribute="trailing" constant="20" id="QI9-pm-QaV"/>
<constraint firstItem="1yi-sb-Ils" firstAttribute="top" secondItem="R1u-8d-oWw" secondAttribute="bottom" constant="10" id="dbN-nZ-rwH"/>
<constraint firstItem="R1u-8d-oWw" firstAttribute="top" secondItem="uvy-OW-5IC" secondAttribute="bottom" constant="10" id="eTk-9m-ynJ"/>
<constraint firstAttribute="bottom" secondItem="1yi-sb-Ils" secondAttribute="bottom" constant="10" id="ia7-Ho-D1z"/>
<constraint firstItem="uvy-OW-5IC" firstAttribute="top" secondItem="4UP-rx-6RK" secondAttribute="top" constant="20" id="t5U-du-VVF"/>
<constraint firstItem="R1u-8d-oWw" firstAttribute="top" secondItem="uvy-OW-5IC" secondAttribute="bottom" constant="10" id="tL0-ct-EYF"/>
<constraint firstItem="1yi-sb-Ils" firstAttribute="top" secondItem="R1u-8d-oWw" secondAttribute="bottom" constant="10" id="usJ-nF-6Rp"/>
</constraints>
</stackView>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstAttribute="trailingMargin" secondItem="4UP-rx-6RK" secondAttribute="trailing" constant="30" id="3sV-rC-Kxr"/>
<constraint firstItem="4UP-rx-6RK" firstAttribute="leading" secondItem="rEB-Za-Zhx" secondAttribute="leadingMargin" constant="30" id="dgC-B5-xdB"/>
<constraint firstItem="4UP-rx-6RK" firstAttribute="top" secondItem="pj8-zF-1z6" secondAttribute="bottom" constant="20" id="zfF-V3-nL5"/>
</constraints>
</view>
<navigationItem key="navigationItem" id="CZR-l9-xMg"/>
<connections>
<outlet property="placeNameLabel" destination="R1u-8d-oWw" id="bUj-pa-t5N"/>
<outlet property="positionLabel" destination="1yi-sb-Ils" id="yTk-mB-C7c"/>
<outlet property="postCodeTextfield" destination="uvy-OW-5IC" id="mwn-3V-Nha"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="OaC-W5-AeM" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="65" y="301"/>
<point key="canvasLocation" x="1" y="301"/>
</scene>
</scenes>
</document>
11 changes: 11 additions & 0 deletions APIClient/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>api.zippopotam.us</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
Expand Down
88 changes: 88 additions & 0 deletions APIClient/MainViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
//
// MainViewController.swift
// APIClient
//
// Created by Florian M. on 05/06/16.
// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved.
//

import UIKit
import Freddy
import Moya

class MainViewController: UIViewController {


var postCodeApi: MoyaProvider<Zippopotam>? = nil
var placeInfo: PlaceInfo? {
didSet {
placeNameLabel.text = self.placeInfo?.placeName
positionLabel.text = "x: " + (self.placeInfo?.placeLatitude)! + " y: " + (self.placeInfo?.placeLongitude)!
}
}

// MARK : OUTLETS

@IBOutlet weak var postCodeTextfield: UITextField!
@IBOutlet weak var placeNameLabel: UILabel!
@IBOutlet weak var positionLabel: UILabel!

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

// MARK: - Actions


@IBAction func entered(sender: AnyObject) {
print("editing did end")
postCodeApi?.request(.germany(postalCode: postCodeTextfield.text!)) {response in
print(response)

switch response {
case .Success(let response):
do {
try response.filterSuccessfulStatusCodes()
// Try to parse the response to JSON
let json = try JSON(data: response.data)
// Try to decode the JSON to the required type
let placeInfo = try PlaceInfo(json: json)
// Configure view according to model
self.placeInfo = placeInfo
} catch {
print(error)
}
case .Failure(let error):
print(error)
}
}

}
}
// @IBAction func postCodeEntered(sender: AnyObject) {
// print("editing did end")
// postCodeApi?.request(.germany(postalCode: postCodeTextfield.text!)) {response in
// print(response)
//
// }
// }


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/


47 changes: 47 additions & 0 deletions APIClient/PostCodeApi.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// PostCodeApi.swift
// Pods
//
// Created by Florian M. on 04/06/16.
//
//

import Foundation
import Freddy
import Moya


enum Zippopotam: Moya.TargetType {

/// MARK: Endpoints


case germany(postalCode: String)


var baseURL: NSURL { return NSURL (string: "http://api.zippopotam.us")! }
var path: String {
switch self {
case .germany(postalCode: let a): return "/DE/"+a
}
}
var method: Moya.Method { return .GET }
var parameters: [String : AnyObject]? {
return nil
}
var sampleData: NSData {return "{\"post code\": \"90210\",\"country\": \"United States\",\"country abbreviation\": \"US\",\"places\": [\"place name\": \"Beverly Hills\",\"longitude\": \"-118.4065\",\"state\": \"California\",\"state abbreviation\": \"CA\", \"latitude\": \"34.0901\"]}".dataUsingEncoding(NSUTF8StringEncoding)!}



}

struct PlaceInfo{
let placeName: String
let placeLongitude: String
let placeLatitude: String
public init(json: JSON) throws {
self.placeName = try json.array("places")[0].string("place name")
self.placeLongitude = try json.array("places")[0].string("longitude")
self.placeLatitude = try json.array("places")[0].string("latitude")
}
}
15 changes: 15 additions & 0 deletions APIClient/View.xib
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="6211" systemVersion="14A298i" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6204"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<view contentMode="scaleToFill" id="iN0-l3-epB">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
</view>
</objects>
</document>
35 changes: 35 additions & 0 deletions APIClient/WeatherViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// WeatherViewController.swift
// APIClient
//
// Created by Florian M. on 13/06/16.
// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved.
//

import UIKit

class WeatherViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/

}
Loading