HTTParty для пакетного обновления пользователей в MailChimp 3.0 API - PullRequest
0 голосов
/ 25 апреля 2018

Я пытаюсь использовать пакетный API, чтобы добавить новое поле в список участников.Я могу отправить пакетный запрос в API и получить пакетный идентификатор обратно.Пакетный запрос успешно принят, но когда я проверяю состояние этого задания, я вижу, что оно не выполнено:

[{"status_code":404,"operation_id":null,
"response":"{\"type\":\"http:\/\/developer.mailchimp.com\/documentation\/
mailchimp\/guides\/error-glossary\/\",
\"title\":\"Resource Not Found\",
\"status\":404,
\"detail\":\"The requested resource could not be found.\",\"instance\":\"\"}"}]

код Ruby

 98     def update_member_count_for_all_users
 99       # https://developer.mailchimp.com/documentation/mailchimp/reference/batches/
100       operations = []
101       User.all.each do |user|
102         next unless user_valid?(user) and user.email.present?
103         subscriber_hash = Digest::MD5.hexdigest user.email.downcase
104
105         operations << {
106           method: "PUT",
107           path: "lists/#{list_id}/members/#{subscriber_hash}",
108           body: {
109             merge_fields: {
110               M_COUNT: user.family&.users&.count || 0
111             }
112           }
113         }
114       end
115
116       body = { operations: operations }
117       url = "#{base_url}/batches"
118       response = HTTParty.post(
119                   url,
120                   body: body.to_json,
121                   basic_auth: { username: 'whatever', password: @api_key }
122                  )
123
124       puts "POSTING: \n\n"
125       puts "#{base_url}/batches \n"  +
126            "body: #{body} \n" +
127            "basic_auth: { username: 'whatever', password: #{@api_key} } \n"
128       puts "=========== \n"
129
130       puts "response is \n\n #{response} \n\n"
131
132     end

Выход RSpec

    [{"id":"BATCH_ID","status":"pending","total_operations":0,
"finished_operations":0,"errored_operations":0,
"submitted_at":"2018-04-25T12:34:27+00:00","completed_at":"","response_body_url":"",
"_links":\[{"rel":"parent","href":"https://us17.api.mailchimp.com/3.0/batches",
"method":"GET","targetSchema":"https://us17.api.mailchimp.com/schema/
3.0/Definitions/Batches/CollectionResponse.json",
 "schema":"https://us17.api.mailchimp.com/schema/3.0/CollectionLinks/Batches.json"},
{"rel":"self","href":"https://us17.api.mailchimp.com/3.0/batches/c2b6dc406e","method":"GET",
 "targetSchema":"https://us17.api.mailchimp.com/schema/3.0/Definitions/Batches/Response.json"},{"rel":"delete",  "href":"https://us17.api.mailchimp.com/3.0/batches/c2b6dc406e","method":"DELETE"}\]}][1]

Проверка пакетного запроса

➜  ~ curl https://us17.api.mailchimp.com/3.0/batches/BATCH_ID  \
     --user 'anystring:API-KEY' 

    "_links": [
        {
            "href": "https://us17.api.mailchimp.com/3.0/batches",
            "method": "GET",
            "rel": "parent",
            "schema": "https://us17.api.mailchimp.com/schema/3.0/CollectionLinks/Batches.json",
            "targetSchema": "https://us17.api.mailchimp.com/schema/3.0/Definitions/Batches/CollectionResponse.json"
        },
        {
            "href": "https://us17.api.mailchimp.com/3.0/batches/6788b3cca5",
            "method": "GET",
            "rel": "self",
            "targetSchema": "https://us17.api.mailchimp.com/schema/3.0/Definitions/Batches/Response.json"
        },
        {
            "href": "https://us17.api.mailchimp.com/3.0/batches/6788b3cca5",
            "method": "DELETE",
            "rel": "delete"
        }
    ],
    "completed_at": "2018-04-25T12:25:20+00:00",
    "errored_operations": 1,
    "finished_operations": 1,
    "id": "6788b3cca5",
    "response_body_url": "https://mailchimp-api-batch.s3.amazonaws.com/6788b3cca5-response.tar.gz?AWSAccessKeyId=KEY&Expires=1524727875&Signature=SIG",
    "status": "finished",
    "submitted_at": "2018-04-25T12:25:13+00:00",
    "total_operations": 1
}

Это содержимое response_body_url:

[{"status_code":404,"operation_id":null,"response":"{\"type\":\"http:\/\/developer.mailchimp.com\/documentation\/mailchimp\/guides\/error-glossary\/\",\"title\":\"Resource Not Found\",\"status\":404,\"detail\":\"The requested resource could not be found.\",\"instance\":\"\"}"}]

1 Ответ

0 голосов
/ 26 апреля 2018

Я использовал неправильный идентификатор списка. Это не идентификатор, который вы видите в URL. На странице, где вы можете увидеть список, нажмите Настройки, Имена списков и значение по умолчанию . Вот где находится идентификатор списка. enter image description here

...