Reputation: 1114
I have an issue when trying to create tags/segments with Gibbon, the MailChimp wrapper for Ruby.
I've been using Gibbon successfully for a large production site for 2 years, but need to move away from Merge Fields to tagging subscribers.
I can successfully add tags to a subscriber via Gibbon, but there are over 85,000 users that need to be transitioned and I really don't want to have to make 85,000 individual requests. There are 200+ possible tags, so I wanted to find all the users for each possible tag and then post the email addresses to the segment endpoint... so 200 requests, rather than 85,000.
The issue is that If a tag already exists (i.e. a test tag I created) in the list, then I get 400 error, Tag already exists
and if the tag doesn't exist, I get 404 Resource Not Found
. This is the same if I supply email addresses within static_segment
or not.
I would expect the following to create a tag within MailChimp:
request = Gibbon::Request.lists(<list_id>)
request.tags.create(body: { name: "testing3", static_segment: [] })
However, I recieve the following error (which is the same if I use tags
or segments
in the request):
I, [2019-06-06T10:33:59.711436 #88340] INFO -- request: POST https://us15.api.mailchimp.com/3.0/lists/<list_id>/tags
D, [2019-06-06T10:33:59.711556 #88340] DEBUG -- request: User-Agent: "Faraday v0.15.4"
Authorization: "Basic <key>"
Content-Type: "application/json"
D, [2019-06-06T10:33:59.711605 #88340] DEBUG -- request: {"type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/","title":"Resource Not Found","status":404,"detail":"Invalid path","instance":"<instance_id>"}
I, [2019-06-06T10:33:59.711779 #88340] INFO -- response: Status 404
D, [2019-06-06T10:33:59.711839 #88340] DEBUG -- response: server: "openresty"
content-type: "application/problem+json; charset=utf-8"
x-request-id: "<instance_id>"
link: "<https://us15.api.mailchimp.com/schema/3.0/ProblemDetailDocument.json>; rel=\"describedBy\""
vary: "Accept-Encoding"
date: "Thu, 06 Jun 2019 09:33:59 GMT"
content-length: "204"
connection: "close"
set-cookie: "_AVESTA_ENVIRONMENT=prod; path=/, _mcid=1.b9640bf1122e4a9b277bb19e3d72caf6; expires=Fri, 05-Jun-2020 09:33:59 GMT; Max-Age=31536000; path=/; domain=.mailchimp.com"
D, [2019-06-06T10:33:59.711878 #88340] DEBUG -- response: {"type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/","title":"Resource Not Found","status":404,"detail":"Invalid path","instance":"<instance_id>"}
Gibbon::MailChimpError: the server responded with status 404 @title="Resource Not Found", @detail="Invalid path", @body={:type=>"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/", :title=>"Resource Not Found", :status=>404, :detail=>"Invalid path", :instance=>"<instance_id>"}, @raw_body="{\"type\":\"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/\",\"title\":\"Resource Not Found\",\"status\":404,\"detail\":\"Invalid path\",\"instance\":\"<instance_id>\"}", @status_code=404
from /Users/Paul/.rvm/gems/<gemset>/gems/gibbon-3.2.0/lib/gibbon/api_request.rb:134:in `handle_error'
Caused by Faraday::ResourceNotFound: the server responded with status 404
from /Users/Paul/.rvm/gems/<gemset>/gems/faraday-0.15.4/lib/faraday/response/raise_error.rb:8:in `on_complete'
I'm using the latest version of Gibbon 3.2.0
.
When I byebug'ed inside of the Gibbon::APIRequest.post
to see the path that was being generated I see the following:
(byebug) base_api_url
"https://us15.api.mailchimp.com/3.0/"
# For request.segments
(byebug) api_url
"https://us15.api.mailchimp.com/3.0/lists/<list_id>/segments"
# For request.tags
(byebug) api_url
"https://us15.api.mailchimp.com/3.0/lists/<list_id>/tags"
Any help would be much appreciated, as I'm really not sure what I'm doing wrong.
Thank you in advance. Paul. :)
Upvotes: 0
Views: 591
Reputation: 1114
So for anyone else that might struggle with this, the Gibbon docs don't talk about Segments at all, but do reference tags, which I think is where some of my confusion make from.
request.tags
should have been request.segments
.
The other issue is that the request cannot be reused, so because I was performing the action inside a loop, by assigning request1 = Gibbon::Request.lists(<list_id>)
and trying to use that for the second iteration, Gibbon forwarded the request to the wrong path and returned a 404 error.
Each request needs to be its own instance of Gibbon Request Gibbon::Request
. So the following would work:
request = Gibbon::Request
#loop start
request.lists(<list_id>).segments.create(body: { name: "testing", static_segment: [] })
# loop end
Hope that helps someone else.
Upvotes: 2
Reputation: 335
Gibbon::Request.lists(LIST_ID).segments.create(body: { name: "NAME", static_segment: [] })
Upvotes: 1