Commit 02ce28c0 authored by Elias Häußler's avatar Elias Häußler 🐛

[FEATURE] Change API provider and add option to provide API key

parent 2b7a6881
# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
## Project specific
/MultiConvert/Config.plist
## Build generated
build/
......
......@@ -13,6 +13,7 @@
6401804C1E683CE10047A498 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 6401804A1E683CE10047A498 /* LaunchScreen.storyboard */; };
640180701E683CF00047A498 /* Currency.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6401806F1E683CF00047A498 /* Currency.swift */; };
640A94C41E8554E80047088C /* HistoryViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 640A94C31E8554E80047088C /* HistoryViewController.swift */; };
64579EFE20CD83C600ABE660 /* Config.plist in Resources */ = {isa = PBXBuildFile; fileRef = 64579EFC20CD83C500ABE660 /* Config.plist */; };
646F8C091E7C3817003DEE7D /* FavoritesViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 646F8C081E7C3817003DEE7D /* FavoritesViewController.swift */; };
646F8C0F1E7D66F8003DEE7D /* Converter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 646F8C0E1E7D66F8003DEE7D /* Converter.swift */; };
646F8C131E7D69F1003DEE7D /* MCError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 646F8C121E7D69F1003DEE7D /* MCError.swift */; };
......@@ -40,6 +41,7 @@
6401804D1E683CE10047A498 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
6401806F1E683CF00047A498 /* Currency.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Currency.swift; sourceTree = "<group>"; };
640A94C31E8554E80047088C /* HistoryViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HistoryViewController.swift; sourceTree = "<group>"; };
64579EFC20CD83C500ABE660 /* Config.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Config.plist; sourceTree = "<group>"; };
646F8C021E7ACA2F003DEE7D /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; };
646F8C081E7C3817003DEE7D /* FavoritesViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FavoritesViewController.swift; sourceTree = "<group>"; };
646F8C0E1E7D66F8003DEE7D /* Converter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Converter.swift; sourceTree = "<group>"; };
......@@ -98,6 +100,7 @@
6401804A1E683CE10047A498 /* LaunchScreen.storyboard */,
646F8C2B1E7DAF6B003DEE7D /* Assets.xcassets */,
6401804D1E683CE10047A498 /* Info.plist */,
64579EFC20CD83C500ABE660 /* Config.plist */,
640180451E683CE10047A498 /* MultiConvert.xcdatamodeld */,
);
path = MultiConvert;
......@@ -229,6 +232,7 @@
files = (
6401804C1E683CE10047A498 /* LaunchScreen.storyboard in Resources */,
646F8C2C1E7DAF6B003DEE7D /* Assets.xcassets in Resources */,
64579EFE20CD83C600ABE660 /* Config.plist in Resources */,
640180441E683CE10047A498 /* Main.storyboard in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
......
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>API_KEY</key>
<string></string>
</dict>
</plist>
......@@ -14,20 +14,17 @@ class Currency: NSObject, QuantityProtocol {
//-- CONSTANTS
/** API url to catch latest currencies */
static let API_URL = "https://api.fixer.io/latest"
static let API_URL = "https://openexchangerates.org/api/latest.json?app_id="
/** API keys for section rates */
static let API_KEY_RATES = "rates"
/** API keys for section date */
static let API_KEY_DATE = "date"
static let API_KEY_TIMESTAMP = "timestamp"
/** API keys for section base */
static let API_KEY_BASE = "base"
/** API update date format */
static let API_DATE_FORMAT = "yyyy-MM-dd"
/** UI elements */
static let placeholder = "45.99"
......@@ -47,13 +44,31 @@ class Currency: NSObject, QuantityProtocol {
//-- FUNCTIONS
/** Get latest exchange rates from fixer API and save them into CoreData */
static func getApiUrl() throws -> String
{
if let configPath = Bundle.main.path(forResource: "Config", ofType: "plist") {
if let config = NSDictionary(contentsOfFile: configPath) {
if let apiKey = config["API_KEY"] as? String {
if apiKey.trimmingCharacters(in: [" "]).count > 0 {
return API_URL + apiKey
}
}
}
}
throw MCError.missingApiKey
}
/** Get latest exchange rates from openexchangerates API and save them into CoreData */
static func update() throws -> Date?
{
// Define API url
let apiUrl = try getApiUrl()
do
{
// Read json data
try jsonData = Data(contentsOf: URL(string: API_URL)!)
try jsonData = Data(contentsOf: URL(string: apiUrl)!)
// Convert json data
guard let json = try? JSONSerialization.jsonObject(with: jsonData!, options: []) else {
......@@ -67,12 +82,8 @@ class Currency: NSObject, QuantityProtocol {
units = []
// Update date
if let jsonDate = jsonResult[API_KEY_DATE] as? String
{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = API_DATE_FORMAT
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
date = dateFormatter.date(from: jsonDate)
if let jsonDate = jsonResult[API_KEY_TIMESTAMP] as? Double {
date = Date(timeIntervalSince1970: jsonDate)
}
// Base currency
......
......@@ -10,6 +10,7 @@ enum MCError: Error
{
case noConnection
case noData
case missingApiKey
case noCompatibleQuantity
case noCompatibleInputUnit
......
......@@ -178,6 +178,11 @@ class MainViewController: UIViewController, UISearchBarDelegate, UITableViewDele
// No error
error = nil
} catch MCError.missingApiKey {
// Error: No API key provided
error = .missingApiKey
} catch MCError.noConnection {
// Error: No connection
......@@ -209,6 +214,11 @@ class MainViewController: UIViewController, UISearchBarDelegate, UITableViewDele
{
switch error!
{
// No API key provided
case .missingApiKey:
_ = self.alert(title: "Update failed \u{1F631}", message: "Please provide a valid API key.", actions: [action], show: true)
break
// No connection
case .noConnection:
_ = self.alert(title: "Update failed \u{1F631}", message: "Please check your internet connection.", actions: [action], show: true)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment