From f392f2067d534da0636738f3dce38cada7dacea3 Mon Sep 17 00:00:00 2001 From: andrew nimmo Date: Wed, 14 Aug 2024 13:19:55 -0700 Subject: [PATCH] Fix RestClient.get, add URI.encode_www_form(query_args) --- .../observations/inat_imports_controller.rb | 28 +++++++++++-------- db/schema.rb | 22 +++++++-------- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/app/controllers/observations/inat_imports_controller.rb b/app/controllers/observations/inat_imports_controller.rb index 7ea7724c66..277de1f95c 100644 --- a/app/controllers/observations/inat_imports_controller.rb +++ b/app/controllers/observations/inat_imports_controller.rb @@ -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"] @@ -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) diff --git a/db/schema.rb b/db/schema.rb index dae6b6a299..8b8f4d1552 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" @@ -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 @@ -649,7 +649,7 @@ 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 @@ -657,14 +657,14 @@ 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" @@ -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" @@ -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 @@ -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 @@ -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 @@ -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