Skip to content

Commit

Permalink
Fix RestClient.get, add URI.encode_www_form(query_args)
Browse files Browse the repository at this point in the history
  • Loading branch information
nimmolo committed Aug 14, 2024
1 parent 128a5ca commit f392f20
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
28 changes: 17 additions & 11 deletions app/controllers/observations/inat_imports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ def authenticate
access_token = JSON.parse(oauth_response.body)["access_token"]

# Use the `access_token` to request a `jwt`, right away.
jwt_response = RestClient.get(
"https://www.inaturalist.org/users/api_token",
jwt_response = RestClient::Request.execute(
method: :get, url: "https://www.inaturalist.org/users/api_token",
headers: { authorization: "Bearer #{access_token}", accept: :json }
)
api_token = JSON.parse(jwt_response)["api_token"]
Expand Down Expand Up @@ -184,15 +184,21 @@ def inat_id_list(inat_import)
end

# https://api.inaturalist.org/v1/docs/#!/Observations/get_observations
def inat_search_observations(ids: nil, id_above: nil, only_id: false,
per_page: 200, sort: "order=asc&order_by=id",
# prevents user from importing others' obss
user_login: nil)
operation =
"/observations?id=#{ids}&id_above=#{id_above}&only_id=#{only_id}" \
"&per_page=#{per_page}&#{sort}&user_login=#{user_login}" \
"&iconic_taxa=Fungi,Protozoa"
::Inat.new(operation: operation, token: inat_import.token).body
# https://stackoverflow.com/a/11251654/3357635
# Note that the `ids` parameter may be a comma-separated list of iNat obs
# ids - that needs to be URL encoded to a string when passed as an arg here
# because URI.encode_www_form deals with arrays by passing the same key
# multiple times.
def inat_search_observations(**args)
query_args = {
ids: nil, id_above: nil, only_id: false, per_page: 200,
order: "asc", order_by: "id",
# prevents user from importing others' obss
user_login: nil, iconic_taxa: ICONIC_TAXA
}.merge(args)

query = URI.encode_www_form(query_args)
::Inat.new(operation: query, token: inat_import.token).body
end

def import_page(page)
Expand Down
22 changes: 11 additions & 11 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
t.integer "project_id"
end

create_table "field_slip_job_trackers", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
create_table "field_slip_job_trackers", charset: "utf8mb3", force: :cascade do |t|
t.integer "start"
t.integer "count"
t.string "prefix"
Expand Down Expand Up @@ -637,7 +637,7 @@
t.datetime "updated_at", precision: nil, null: false
end

create_table "solid_queue_blocked_executions", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
create_table "solid_queue_blocked_executions", charset: "utf8mb3", force: :cascade do |t|
t.bigint "job_id", null: false
t.string "queue_name", null: false
t.integer "priority", default: 0, null: false
Expand All @@ -649,22 +649,22 @@
t.index ["job_id"], name: "index_solid_queue_blocked_executions_on_job_id", unique: true
end

create_table "solid_queue_claimed_executions", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
create_table "solid_queue_claimed_executions", charset: "utf8mb3", force: :cascade do |t|
t.bigint "job_id", null: false
t.bigint "process_id"
t.datetime "created_at", null: false
t.index ["job_id"], name: "index_solid_queue_claimed_executions_on_job_id", unique: true
t.index ["process_id", "job_id"], name: "index_solid_queue_claimed_executions_on_process_id_and_job_id"
end

create_table "solid_queue_failed_executions", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
create_table "solid_queue_failed_executions", charset: "utf8mb3", force: :cascade do |t|
t.bigint "job_id", null: false
t.text "error"
t.datetime "created_at", null: false
t.index ["job_id"], name: "index_solid_queue_failed_executions_on_job_id", unique: true
end

create_table "solid_queue_jobs", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
create_table "solid_queue_jobs", charset: "utf8mb3", force: :cascade do |t|
t.string "queue_name", null: false
t.string "class_name", null: false
t.text "arguments"
Expand All @@ -682,13 +682,13 @@
t.index ["scheduled_at", "finished_at"], name: "index_solid_queue_jobs_for_alerting"
end

create_table "solid_queue_pauses", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
create_table "solid_queue_pauses", charset: "utf8mb3", force: :cascade do |t|
t.string "queue_name", null: false
t.datetime "created_at", null: false
t.index ["queue_name"], name: "index_solid_queue_pauses_on_queue_name", unique: true
end

create_table "solid_queue_processes", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
create_table "solid_queue_processes", charset: "utf8mb3", force: :cascade do |t|
t.string "kind", null: false
t.datetime "last_heartbeat_at", null: false
t.bigint "supervisor_id"
Expand All @@ -700,7 +700,7 @@
t.index ["supervisor_id"], name: "index_solid_queue_processes_on_supervisor_id"
end

create_table "solid_queue_ready_executions", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
create_table "solid_queue_ready_executions", charset: "utf8mb3", force: :cascade do |t|
t.bigint "job_id", null: false
t.string "queue_name", null: false
t.integer "priority", default: 0, null: false
Expand All @@ -710,7 +710,7 @@
t.index ["queue_name", "priority", "job_id"], name: "index_solid_queue_poll_by_queue"
end

create_table "solid_queue_recurring_executions", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
create_table "solid_queue_recurring_executions", charset: "utf8mb3", force: :cascade do |t|
t.bigint "job_id", null: false
t.string "task_key", null: false
t.datetime "run_at", null: false
Expand All @@ -719,7 +719,7 @@
t.index ["task_key", "run_at"], name: "index_solid_queue_recurring_executions_on_task_key_and_run_at", unique: true
end

create_table "solid_queue_scheduled_executions", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
create_table "solid_queue_scheduled_executions", charset: "utf8mb3", force: :cascade do |t|
t.bigint "job_id", null: false
t.string "queue_name", null: false
t.integer "priority", default: 0, null: false
Expand All @@ -729,7 +729,7 @@
t.index ["scheduled_at", "priority", "job_id"], name: "index_solid_queue_dispatch_all"
end

create_table "solid_queue_semaphores", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
create_table "solid_queue_semaphores", charset: "utf8mb3", force: :cascade do |t|
t.string "key", null: false
t.integer "value", default: 1, null: false
t.datetime "expires_at", null: false
Expand Down

0 comments on commit f392f20

Please sign in to comment.