From ada68812491ca1b0c295f3cba9745d3b5eca0e59 Mon Sep 17 00:00:00 2001 From: Zoe Moore Date: Fri, 10 Feb 2023 21:12:33 -0800 Subject: [PATCH] API key not needed at compile time instead read from file on run --- data/ui/main.blp | 8 ++++++++ src/modules/api/yelp.cr | 8 ++++---- src/modules/utils/utils.cr | 19 +++++++++++++++++++ src/modules/views/main.cr | 11 +++++++++++ src/wince.cr | 1 + 5 files changed, 43 insertions(+), 4 deletions(-) diff --git a/data/ui/main.blp b/data/ui/main.blp index e5f3741..555819e 100644 --- a/data/ui/main.blp +++ b/data/ui/main.blp @@ -43,6 +43,14 @@ Adw.ApplicationWindow mainWindow { label: "Wince is powered by Yelp"; } + Gtk.Label configNotFoundText { + halign: center; + valign: center; + vexpand: true; + visible: false; + label: "API key not found. You must place your yelp api key in ~/.config/wince/api_key"; + } + Adw.Leaflet leaflet { can-navigate-forward: false; can-navigate-back: true; diff --git a/src/modules/api/yelp.cr b/src/modules/api/yelp.cr index d6baa87..761f688 100644 --- a/src/modules/api/yelp.cr +++ b/src/modules/api/yelp.cr @@ -2,6 +2,8 @@ require "http/client" require "io" require "json" +require "../utils/utils.cr" + module Wince::Yelp extend self @@ -68,8 +70,6 @@ module Wince::Yelp property day : Int32 end - @@token : String = {{ read_file("./api_key") }} - def search_businesses(search : String, location : String) params = URI::Params.encode({ @@ -83,7 +83,7 @@ module Wince::Yelp path: "/v3/businesses/search", query: params ) - headers = HTTP::Headers{ "Authorization" => "Bearer " + @@token } + headers = HTTP::Headers{ "Authorization" => "Bearer " + Utils.api_key } response = HTTP::Client.get(uri, headers) {response.status_code, SearchResponse.from_json(response.body)} @@ -96,7 +96,7 @@ module Wince::Yelp host: "api.yelp.com", path: "/v3/businesses/#{id}", ) - headers = HTTP::Headers{ "Authorization" => "Bearer " + @@token } + headers = HTTP::Headers{ "Authorization" => "Bearer " + Utils.api_key } response = HTTP::Client.get(uri, headers) {response.status_code, DetailsResponse.from_json(response.body)} diff --git a/src/modules/utils/utils.cr b/src/modules/utils/utils.cr index 069eeea..e4a1346 100644 --- a/src/modules/utils/utils.cr +++ b/src/modules/utils/utils.cr @@ -3,6 +3,9 @@ require "time" # yeah me too module Wince::Utils extend self + @@config_path = Path.home.join("/.config/wince/api_key") + @@api_key = "" + def hours_for_day(hours : Yelp::Hours, day : Time::DayOfWeek, seperator : String) day_number = day_of_week_to_int(day) @@ -36,4 +39,20 @@ module Wince::Utils def format_address(display_address : Array(String)) display_address.join("\n") end + + def api_key_exists? + File.exists? @@config_path + end + + def read_api_key + @@api_key = File.read(@@config_path).strip + end + + def api_key + if @@api_key.blank? + read_api_key + end + + @@api_key + end end diff --git a/src/modules/views/main.cr b/src/modules/views/main.cr index 1385834..21cb326 100644 --- a/src/modules/views/main.cr +++ b/src/modules/views/main.cr @@ -39,6 +39,13 @@ module Wince handle_business_select end + unless Utils.api_key_exists? + POWERD_BY_TEXT.visible = false + CONFIG_NOT_FOUND_TEXT.visible = true + end + + puts Utils.api_key + setup_map window.present @@ -89,6 +96,10 @@ module Wince end def handle_search + unless Utils.api_key_exists? + return + end + search = SEARCH_ENTRY.buffer.text location = LOCATION_ENTRY.buffer.text diff --git a/src/wince.cr b/src/wince.cr index 7e59eb7..e392608 100644 --- a/src/wince.cr +++ b/src/wince.cr @@ -28,6 +28,7 @@ module Wince DETAILS_BACK = Gtk::Button.cast(B_UI["detailsBack"]) DETAILS_HOURS_BOX = Gtk::ListBox.cast(B_UI["detailsHoursBox"]) DETAILS_MAP = Shumate::SimpleMap.cast(B_UI["detailsMap"]) + CONFIG_NOT_FOUND_TEXT = Gtk::Label.cast(B_UI["configNotFoundText"]) APP = Adw::Application.new("space.quietfeathers.wince", Gio::ApplicationFlags::None) end