-
Notifications
You must be signed in to change notification settings - Fork 56
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
236 simple api #241
base: main
Are you sure you want to change the base?
236 simple api #241
Conversation
…on code for API using basic auth
…he back-end, Added admin check, Render json for adopted drains
@jafowler49 👍 this looks good. I'm a little concerned about how it will fare as the number of adopted drains grows though. Would you be able to test this with, say, 10000 adopted drains (noting that the Heroku request timeout is 60s)? We may need to introduce pagination of the responses. |
oh ok, sounds like scalability/response times might be an issue. good call I will test it out |
before_action :authenticate | ||
|
||
def index | ||
@things = Thing.where.not(user_id: !nil) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you want !nil
, but just nil
Interestingly -- not that anyone cares -- !nil
evaluates to true
and true
gets converted to 1
in the sql, so you end up with WHERE user_id != 1
. (I only know this because I checked the console)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is true, good catch
…s. I created this initially to test out how the API would handle load
hey @jszwedko , sorry this took so long... anyways added some work on this. hoping you can check it and see what you think. thanks |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @jafowler49 !
Overall, this is is shaping up well, but I'm still a little concerned about the CSV API route returning all of the records rather than paginating like the other formats. I realize it is difficult to encode the pagination information directly in the response body, but we can leverage HTTP response headers to indicate the paging information. I also think we could opt to just serve JSON for now since that is the most common API format these days and drop XML and CSV.
Could we also configure the max_per_page
to be 1000 and drop the default to 100
? Or whatever makes sense with your testing; I just want to make sure we have a maximum.
Gemfile
Outdated
@@ -25,6 +26,7 @@ end | |||
group :development do | |||
gem 'byebug' | |||
gem 'spring' | |||
gem 'kaminari' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll need this gem in production too, no? It feels like it should be at the top level. Do you mind locking it to a major version as well (I locked the rest in #259) so that it is easier to update without breaking compatibility.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
~>1.0 includes supported versions. the older version dropped support for an earlier version of Ruby
Gemfile
Outdated
@@ -3,6 +3,7 @@ ruby '2.2.3' | |||
|
|||
gem 'dotenv-rails', groups: [:development, :test] | |||
gem 'rails', '~> 4.2.4' | |||
gem 'faker', '1.7.3' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need an exact version here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed that to have the version ~> 1.7.0... These are the sure compatible versions, not sure if they will change support in the future.
@@ -59,7 +59,7 @@ | |||
# given strategies, for example, `config.http_authenticatable = [:database]` will | |||
# enable it only for database authentication. The supported strategies are: | |||
# :database = Support basic authentication with authentication key + password | |||
# config.http_authenticatable = false | |||
config.http_authenticatable = false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was this uncommented?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Never mind, I see that the API is locked to authenticated users.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cool
@@ -19,4 +19,13 @@ | |||
resource :things | |||
mount RailsAdmin::Engine => '/admin', :as => 'rails_admin' | |||
root to: 'main#index' | |||
|
|||
# API | |||
scope '/api' do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 to the URL pathing here
db/seeds.rb
Outdated
@@ -6,6 +6,7 @@ | |||
|
|||
r = Random.new | |||
|
|||
=begin |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you mean to leave this commented?
lib/tasks/data.rake
Outdated
unless Rails.env.production? | ||
Thing.first(10_000).each do |t| | ||
if t.user_id.blank? | ||
t.user_id = User.find_by('id' => Random.rand(1..User.last.id)).id |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think something like User.order('RANDOM()').limit(1)
is more idiomatic here. Could we maybe print out an error message if the user attempts to run this in production?
|
||
# Determine if the user supplied a valid page number, if not they get first page | ||
def make_cur_page | ||
page = params[:page].blank? ? 2 : params[:page] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be 1
?
@@ -0,0 +1,44 @@ | |||
require 'test_helper' | |||
|
|||
class AdoptedControllerTest < ActionController::TestCase |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like checking the content-type, but could we make a few assertions on the response content to make sure the right records are returned? It'd also be nice to exercise the paging logic a little here.
… had to change it on my local env)
Hi @jszwedko Finally worked on this. Sorry for all the commits, I merged Prod in to my local branch. Last 1 was fixing merge conflicts and Rubocop requests, next 10 were actual changes, and the rest were just commits from Prod. Let me know what further changes I should make to the API. I added basic tests. Is there an easy way to test 1000 or so items within a Test Case? Hope this helps Also, since I left the City I've been working on my iOS and UI skills. So if you guys need an iOS app for Adopt a Drain, I'm your guy. Best! |
This should be ready for initial review
Thanks!