API key not needed at compile time instead read from file on run

This commit is contained in:
2023-02-10 21:12:33 -08:00
parent 992a8e9e8d
commit ada6881249
5 changed files with 43 additions and 4 deletions

View File

@@ -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;

View File

@@ -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)}

View File

@@ -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

View File

@@ -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

View File

@@ -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