Skip to content

Commit

Permalink
fix(http): update endpoint authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
yoannfleurydev committed Jul 6, 2020
1 parent a85eaa8 commit 2f6a5fb
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 84 deletions.
89 changes: 42 additions & 47 deletions GitHub Notifications/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,60 +66,55 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}

@objc func update() {
let username = Defaults[.username]
let password = Defaults[.password]

let basicAuth = "\(username):\(password)".data(using: .utf8)

if let base64Encoded = basicAuth?.base64EncodedString(options: Data.Base64EncodingOptions(rawValue: 0)) {
let headers = ["authorization": "Basic \(base64Encoded)"]

let headers = ["authorization": "token \(password)"]

let request = NSMutableURLRequest(
url: NSURL(string: "https://api.github.com/notifications")! as URL,
cachePolicy: .reloadIgnoringLocalCacheData,
timeoutInterval: 10.0
)
request.httpMethod = "GET"
request.allHTTPHeaderFields = headers
let request = NSMutableURLRequest(
url: NSURL(string: "https://api.github.com/notifications")! as URL,
cachePolicy: .reloadIgnoringLocalCacheData,
timeoutInterval: 10.0
)

request.httpMethod = "GET"
request.allHTTPHeaderFields = headers

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
if error != nil {
print(error!)
} else {
guard let responseData = data else {
print("Error: did not receive data")
return
}
let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
if error != nil {
print(error!)
} else {
guard let responseData = data else {
print("Error: did not receive data")
return
}

do {
guard let notifications = try JSONSerialization.jsonObject(with: responseData, options: [])
as? [Any] else {
print("error trying to convert data to JSON 1")
return
}

self.firstMenuItem?.title = "\(notifications.count) notification\(notifications.count > 1 ? "s" : "")"
do {
guard let notifications = try JSONSerialization.jsonObject(with: responseData, options: [])
as? [Any] else {
print("error trying to convert data to JSON 1")
return
}

self.firstMenuItem?.title = "\(notifications.count) notification\(notifications.count > 1 ? "s" : "")"

let doesNotificationExist = notifications.count >= 1

let doesNotificationExist = notifications.count >= 1

DispatchQueue.main.async {
self.setStatusItemImage(
named: doesNotificationExist
? "StatusItemImageNotification"
: "StatusItemImage"
)
}
} catch {
print("error trying to convert data to JSON")
return
}
DispatchQueue.main.async {
self.setStatusItemImage(
named: doesNotificationExist
? "StatusItemImageNotification"
: "StatusItemImage"
)
}
} catch {
print("error trying to convert data to JSON")
return
}
})
}
})

dataTask.resume()
}
dataTask.resume()
}

func setStatusItemImage(named: String = "StatusItemImage") {
Expand Down
1 change: 0 additions & 1 deletion GitHub Notifications/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ import Cocoa
import Defaults

extension Defaults.Keys {
static let username = Key<String>("username", default: "")
static let password = Key<String>("password", default: "")
}
6 changes: 1 addition & 5 deletions GitHub Notifications/GeneralPreferenceViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,13 @@ final class GeneralPreferenceViewController: NSViewController, PreferencePane {
let preferencePaneTitle = "General"

override var nibName: NSNib.Name? { "GeneralPreferenceViewController" }

@IBOutlet private var usernameTextField: NSTextField!

@IBOutlet private var passwordTextField: NSTextField!
@IBOutlet private var saveButton: NSButton!

override func viewDidLoad() {
super.viewDidLoad()

usernameTextField?.placeholderString = "username"
usernameTextField?.stringValue = Defaults[.username]
passwordTextField?.placeholderString = "********"
passwordTextField?.stringValue = Defaults[.password]

Expand All @@ -34,7 +31,6 @@ final class GeneralPreferenceViewController: NSViewController, PreferencePane {

@objc func save() {
// Save input values in User Defaults
Defaults[.username] = usernameTextField!.stringValue
Defaults[.password] = passwordTextField!.stringValue

// Close the window when done
Expand Down
38 changes: 8 additions & 30 deletions GitHub Notifications/GeneralPreferenceViewController.xib
Original file line number Diff line number Diff line change
Expand Up @@ -9,54 +9,32 @@
<connections>
<outlet property="passwordTextField" destination="0nJ-7C-dkw" id="mZW-Cn-tij"/>
<outlet property="saveButton" destination="djG-W7-VfA" id="aWI-3q-aho"/>
<outlet property="usernameTextField" destination="vxI-a4-2Tu" id="F1o-1f-f8D"/>
<outlet property="view" destination="c22-O7-iKe" id="ZQe-UH-LCX"/>
</connections>
</customObject>
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
<customView id="c22-O7-iKe">
<rect key="frame" x="0.0" y="0.0" width="510" height="194"/>
<customView misplaced="YES" id="c22-O7-iKe">
<rect key="frame" x="0.0" y="0.0" width="480" height="136"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<subviews>
<gridView xPlacement="center" yPlacement="center" rowAlignment="none" rowSpacing="8" columnSpacing="10" translatesAutoresizingMaskIntoConstraints="NO" id="lkV-Y2-eOl">
<rect key="frame" x="30" y="20" width="450" height="154"/>
<rect key="frame" x="30" y="20" width="450" height="96"/>
<constraints>
<constraint firstAttribute="width" constant="450" id="5HG-su-XZq"/>
</constraints>
<rows>
<gridRow height="47" id="ukr-az-gqc"/>
<gridRow yPlacement="top" id="gSl-iE-cwq"/>
<gridRow id="zkx-zp-WqT"/>
</rows>
<columns>
<gridColumn xPlacement="trailing" width="153" id="GGj-JC-w4E"/>
<gridColumn xPlacement="trailing" width="151" id="GGj-JC-w4E"/>
<gridColumn xPlacement="leading" id="cs9-fm-l2F"/>
</columns>
<gridCells>
<gridCell row="ukr-az-gqc" column="GGj-JC-w4E" id="sRb-hw-f4U">
<textField key="contentView" horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="Vt6-jC-8Jo">
<rect key="frame" x="85" y="123" width="70" height="16"/>
<textFieldCell key="cell" lineBreakMode="clipping" title="Username:" id="uKy-ok-dDi">
<font key="font" usesAppearanceFont="YES"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
</gridCell>
<gridCell row="ukr-az-gqc" column="cs9-fm-l2F" id="jPa-TM-cRP">
<textField key="contentView" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="vxI-a4-2Tu">
<rect key="frame" x="163" y="120" width="100" height="21"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" borderStyle="bezel" drawsBackground="YES" id="Dfa-xZ-Y74">
<font key="font" usesAppearanceFont="YES"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
</gridCell>
<gridCell row="gSl-iE-cwq" column="GGj-JC-w4E" id="IXl-mV-waK">
<textField key="contentView" horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="D0h-m5-qzI" userLabel="Personal Access Token:">
<rect key="frame" x="8" y="83" width="147" height="16"/>
<rect key="frame" x="6" y="80" width="147" height="16"/>
<textFieldCell key="cell" lineBreakMode="clipping" title="Personal Access Token:" id="Z3e-UB-ZDm" userLabel="Personal Access Token:">
<font key="font" usesAppearanceFont="YES"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
Expand All @@ -66,7 +44,7 @@
</gridCell>
<gridCell row="gSl-iE-cwq" column="cs9-fm-l2F" id="yFx-k0-cPt">
<secureTextField key="contentView" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="0nJ-7C-dkw">
<rect key="frame" x="163" y="78" width="100" height="21"/>
<rect key="frame" x="161" y="75" width="100" height="21"/>
<secureTextFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" borderStyle="bezel" drawsBackground="YES" usesSingleLineMode="YES" id="nAh-Vn-Nyc">
<font key="font" metaFont="system"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
Expand All @@ -80,7 +58,7 @@
<gridCell row="zkx-zp-WqT" column="GGj-JC-w4E" id="1B5-ah-fUN"/>
<gridCell row="zkx-zp-WqT" column="cs9-fm-l2F" id="xrS-Ra-IX6">
<button key="contentView" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="djG-W7-VfA">
<rect key="frame" x="157" y="18" width="70" height="32"/>
<rect key="frame" x="155" y="16" width="70" height="32"/>
<buttonCell key="cell" type="push" title="Save" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="TLl-wY-a6p">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
Expand All @@ -97,7 +75,7 @@
<constraint firstAttribute="bottom" secondItem="lkV-Y2-eOl" secondAttribute="bottom" priority="750" constant="20" id="bsa-Ju-YOT"/>
<constraint firstItem="lkV-Y2-eOl" firstAttribute="centerX" secondItem="c22-O7-iKe" secondAttribute="centerX" id="rdL-1S-HXy"/>
</constraints>
<point key="canvasLocation" x="83" y="211"/>
<point key="canvasLocation" x="70" y="141"/>
</customView>
</objects>
</document>
2 changes: 1 addition & 1 deletion GitHub Notifications/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>1.0.0</string>
<string>1.0.1</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSApplicationCategoryType</key>
Expand Down

0 comments on commit 2f6a5fb

Please sign in to comment.