Skip to content
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

Unable to use Mailjet::Listrecipient #25

Closed
zedalaye opened this issue Aug 24, 2014 · 1 comment
Closed

Unable to use Mailjet::Listrecipient #25

zedalaye opened this issue Aug 24, 2014 · 1 comment

Comments

@zedalaye
Copy link
Contributor

The default Mailjet::Listrecipient API is not usable.

require 'mailjet'
require 'active_support/all'

Mailjet.configure do |config|
  config.api_key = 'xxxxx'
  config.secret_key = 'xxxxx'
end

c = Mailjet::Contact.all(limit: 0)
l = Mailjet::Contactslist.all(limit: 0)

Mailjet::ListRecipient.create(:contact =>  c.first.id, :list => l.first.id)

Returns the following error:

'handle_exeception': error 400 while sending #<RestClient::Resource:0x00000003f39730 @url="https://api.mailjet.com//v3/REST/listrecipient", @block=nil, @options={:public_operations=>[:get, :put, :post, :delete], :read_only=>nil, :user=>"xxxxx", :password=>"xxxxx"}> to https://api.mailjet.com//v3/REST/listrecipient with {"Contact"=>1, "List"=>1} (Mailjet::ApiError)

"{ \"ErrorInfo\" : \"\", \"ErrorMessage\" : \"Invalid json input: expected \\\",\\\", but found \\\"\\\"\\\" at stream position 15 ...ntact\\\" : \\\"|1\\\", \\\"List\\\"...\", \"StatusCode\" : 400 }"

Please see http://api.mailjet.com/0.1/HelpStatus for more informations on error numbers.

According to the official APIv3 documentation the Contact and List fields are both mandatory and requires respectively a ReadOnly (?!?) Type Contact and a Type List. These types are not documented.

I found a hint here : https://github.com/mailjet/wordpress-mailjet-plugin-apiv3/blob/master/wp-mailjet.php#L198

The mandatory fields seems to be ContactID and TypeID but those are not accessible with the Ruby API that filters properties based on symbols and according to the Naming Conventions, there is no way to write a symbol that will be turned into ContactID.

My workaround is to write a ListRecipient (note the capital R...) class that allows the ContactID and TypeID keys for params :

module Mailjet
  class ListRecipient
    include Mailjet::Resource
    self.resource_path = '/v3/REST/listrecipient'
    self.public_operations = [:get, :put, :post, :delete]
    self.filters = [:active, :blocked, :contact, :contact_email, :contacts_list, :last_activity_at, :list_name, :opened, :status, :unsub]
    self.properties = [:contact, :id, :is_active, :is_unsubscribed, :list, :unsubscribed_at, 'ContactID', 'ListID']
  end
end

and now, this code works as expected :

Mailjet::ListRecipient.create('ContactID' =>  c.first.id, 'ListID' => l.first.id)
@tylerjnap
Copy link
Contributor

Added this functionality into most recent update. Thanks for pointing this out!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants