How to perform map, when mapping relies on previous map config? - automapper

The below mapping between Volunteer & IVolunteerViewDTO works perfectly fine. I can call a REST api and receive a list of IVolunteerViewDTO mapped using AutoMapper. However, I can't figure out how to get Registration working with IRegistrationDTO. AutoMapper throws an exception and says: InvalidCastException: Unable to cast object of type 'Proxy<RSMuseum.Services.DTOs.IVolunteerViewDTO_RSMuseum.Services_Version=1.0.0.0_Culture=neutral_PublicKeyToken=null>' to type 'RSMuseum.Services.DTOs.VolunteerViewDTO'.
public IMapper GetMapper()
{
var mc = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Volunteer, IVolunteerViewDTO>()
.ForMember(dest => dest.GuildName,
opts => opts.MapFrom(src => src.Guilds.Select(x => x.GuildName)))
.ForMember(dest => dest.FirstName,
opts => opts.MapFrom(src => src.Person.FirstName))
.ForMember(dest => dest.LastName,
opts => opts.MapFrom(src => src.Person.LastName));
cfg.CreateMap<Guild, IGuildDTO>();
cfg.CreateMap<Repository.Entities.Registration, IRegistrationDTO>()
.ForMember(dest => dest.Guild,
opts => opts.MapFrom(src => src.Guild))
.ForMember(dest => dest.Volunteer,
opts => opts.MapFrom(src => src.Volunteer));
});
mc.AssertConfigurationIsValid();
IMapper m = new Mapper(mc, t => _container.GetInstance(t));
return m;
}
The dest.Volunteer is the DTO and the src.Volunteer is the entity itself. When I comment out the need for the VolunteerDTO on my RegistrationDTO, everything is perfectly fine and no exceptions at all. Question is how to fix.

Related

Sensu-Go ::: Set handler for keepalive check with puppet

I want to be able to get notified when a server is down.
Puppet: sensu/sensu-puppet v5.9.0
Based on https://github.com/sensu/sensu-go/issues/1960, I tried this code without success.
Since there is a special static handler called "keepalive", I create a set handler "keepalive" and include my telegram handler (telegram_ops) in it.
BACKEND Code
class { 'sensu':
api_host => 'sensu3.mydomain.com',
password => '****',
agent_password => '****',
agent_entity_config_password => '****',
ssl_ca_source => 'puppet:///modules/common/ssl/ca.crt',
}
include sensu::cli
class { 'sensu::backend':
ssl_cert_source => 'puppet:///modules/common/ssl/my.crt',
ssl_key_source => 'puppet:///modules/common/ssl/my.key',
config_hash => {
'deregistration-handler' => 'deregistration',
'event-log-file' => '/var/log/sensu/events.log'
}
}
sensu_bonsai_asset { 'sensu/check-cpu-usage':
ensure => 'present',
version => 'latest',
}
sensu_check { 'check-cpu':
ensure => 'present',
labels => {'contacts' => 'ops'},
handlers => ['telegram_ops'],
command => 'check-cpu-usage -w 75 -c 85',
interval => 60,
subscriptions => 'linux',
publish => true,
runtime_assets => ['sensu/check-cpu-usage']
}
sensu_bonsai_asset { 'sensu/sensu-go-has-contact-filter':
ensure => 'present',
version => '0.2.0',
}
sensu_filter { 'contact_ops':
ensure => 'present',
action => 'allow',
runtime_assets => ['sensu/sensu-go-has-contact-filter'],
expressions => ['has_contact(event, "ops")'],
}
sensu_filter { 'first_occurrence':
ensure => 'present',
action => 'allow',
expressions => ['event.check.occurrences == 1'],
}
sensu_bonsai_asset { 'Thor77/sensu-telegram-handler':
ensure => 'present'
}
sensu_handler { 'telegram_ops':
ensure => 'present',
type => 'pipe',
command => 'sensu-telegram-handler --api-token **** --chatid -****',
timeout => 10,
runtime_assets => ['Thor77/sensu-telegram-handler'],
filters => [
'is_incident',
'not_silenced',
'contact_ops',
'first_occurrence'
],
}
sensu_handler { 'keepalive':
ensure => 'present',
type => 'set',
handlers => ['telegram_ops'],
}
AGENT Code (Very simple code.)
class { 'sensu::agent':
subscriptions => ['default', 'linux', $hostname, 'nuc']
}
It does not work. If I suddenly shutdown a server, nothing happeds.
What is the proper way to do this?
It is posible any other aproach?
Long time ago there was another solution, class sensu had the parameter client_keepalive but it is not available anymore.
Thanks.

I am only getting a single repository back

I just started learning how to use the GitHub API. I wrote a sample program, to list my repositories. I have 5 repos, and I am only getting one. Any ideas?
My code:
<? php
DEFINE ('PERSONAL_TOKEN', 'xxxxxxxxxxxxxxxxx');
$headers = [
'Accept: application/vnd.github.v3+json',
'User-Agent: Some Text',
'Authorization: token '. PERSONAL_TOKEN
];
$res = list_repositories($headers);
print "<pre>";
print_r($res);
print "</pre>";
function list_repositories($headers)
{
$curl_url = GITHUB_REST_URL.'myusernameful'.'/repos';
$ch = curl_init($curl_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$res = json_decode(curl_exec($ch), TRUE);
curl_close($ch);
return $output;
}
I was expecting to get all 5 of my repos, instead, I am only getting one back:
Array
(
[0] => Array
(
[id] => 168594503
[node_id] => MDEwOlJlcG9zaXRvcnkxNjg1OTQ1MDM=
[name] => repo_one
[full_name] => myusername/repo_one
[private] =>
[owner] => Array
(
[login] => myusername
[id] => 45577729
[node_id] => MDQ6VXNlcjQ1NTc3NzI5
[avatar_url] => https://avatars1.githubusercontent.com/u/45577729?v=4
[gravatar_id] =>
[url] => https://api.github.com/users/myusername
[html_url] => https://github.com/myusername
[followers_url] => https://api.github.com/users/myusername/followers
[following_url] => https://api.github.com/users/myusername/following{/other_user}
[gists_url] => https://api.github.com/users/myusername/gists{/gist_id}
[starred_url] => https://api.github.com/users/myusername/starred{/owner}{/repo}
[subscriptions_url] => https://api.github.com/users/myusername/subscriptions
[organizations_url] => https://api.github.com/users/myusername/orgs
[repos_url] => https://api.github.com/users/myusername/repos
[events_url] => https://api.github.com/users/myusername/events{/privacy}
[received_events_url] => https://api.github.com/users/myusername/received_events
[type] => User
[site_admin] =>
)
[html_url] => https://github.com/myusername/repo_one
[description] => repo_one makes it easy and fast to build beautiful mobile apps.
[fork] => 1
[url] => https://api.github.com/repos/myusername/repo_one
[forks_url] => https://api.github.com/repos/myusername/repo_one/forks
[keys_url] => https://api.github.com/repos/myusername/repo_one/keys{/key_id}
[collaborators_url] => https://api.github.com/repos/myusername/repo_one/collaborators{/collaborator}
[teams_url] => https://api.github.com/repos/myusername/repo_one/teams
[hooks_url] => https://api.github.com/repos/myusername/repo_one/hooks
[issue_events_url] => https://api.github.com/repos/myusername/repo_one/issues/events{/number}
[events_url] => https://api.github.com/repos/myusername/repo_one/events
[assignees_url] => https://api.github.com/repos/myusername/repo_one/assignees{/user}
[branches_url] => https://api.github.com/repos/myusername/repo_one/branches{/branch}
[tags_url] => https://api.github.com/repos/myusername/repo_one/tags
[blobs_url] => https://api.github.com/repos/myusername/repo_one/git/blobs{/sha}
[git_tags_url] => https://api.github.com/repos/myusername/repo_one/git/tags{/sha}
[git_refs_url] => https://api.github.com/repos/myusername/repo_one/git/refs{/sha}
[trees_url] => https://api.github.com/repos/myusername/repo_one/git/trees{/sha}
[statuses_url] => https://api.github.com/repos/myusername/repo_one/statuses/{sha}
[languages_url] => https://api.github.com/repos/myusername/repo_one/languages
[stargazers_url] => https://api.github.com/repos/myusername/repo_one/stargazers
[contributors_url] => https://api.github.com/repos/myusername/repo_one/contributors
[subscribers_url] => https://api.github.com/repos/myusername/repo_one/subscribers
[subscription_url] => https://api.github.com/repos/myusername/repo_one/subscription
[commits_url] => https://api.github.com/repos/myusername/repo_one/commits{/sha}
[git_commits_url] => https://api.github.com/repos/myusername/repo_one/git/commits{/sha}
[comments_url] => https://api.github.com/repos/myusername/repo_one/comments{/number}
[issue_comment_url] => https://api.github.com/repos/myusername/repo_one/issues/comments{/number}
[contents_url] => https://api.github.com/repos/myusername/repo_one/contents/{+path}
[compare_url] => https://api.github.com/repos/myusername/repo_one/compare/{base}...{head}
[merges_url] => https://api.github.com/repos/myusername/repo_one/merges
[archive_url] => https://api.github.com/repos/myusername/repo_one/{archive_format}{/ref}
[downloads_url] => https://api.github.com/repos/myusername/repo_one/downloads
[issues_url] => https://api.github.com/repos/myusername/repo_one/issues{/number}
[pulls_url] => https://api.github.com/repos/myusername/repo_one/pulls{/number}
[milestones_url] => https://api.github.com/repos/myusername/repo_one/milestones{/number}
[notifications_url] => https://api.github.com/repos/myusername/repo_one/notifications{?since,all,participating}
[labels_url] => https://api.github.com/repos/myusername/repo_one/labels{/name}
[releases_url] => https://api.github.com/repos/myusername/repo_one/releases{/id}
[deployments_url] => https://api.github.com/repos/myusername/repo_one/deployments
[created_at] => 2019-01-31T20:50:56Z
[updated_at] => 2019-02-01T22:17:23Z
[pushed_at] => 2019-01-31T20:48:11Z
[git_url] => git://github.com/myusername/repo_one.git
[ssh_url] => git#github.com:myusername/repo_one.git
[clone_url] => https://github.com/myusername/repo_one.git
[svn_url] => https://github.com/myusername/repo_one
[homepage] => https://repo_one.io
[size] => 52581
[stargazers_count] => 0
[watchers_count] => 0
[language] => Dart
[has_issues] =>
[has_projects] => 1
[has_downloads] => 1
[has_wiki] => 1
[has_pages] =>
[forks_count] => 0
[mirror_url] =>
[archived] =>
[open_issues_count] => 0
[license] => Array
(
[key] => other
[name] => Other
[spdx_id] => NOASSERTION
[url] =>
[node_id] => MDc6TGljZW5zZTA=
)
[forks] => 0
[open_issues] => 0
[watchers] => 0
[default_branch] => master
[permissions] => Array
(
[admin] => 1
[push] => 1
[pull] => 1
)
)
)
The "List your Repositories" is /user/repos, not /users/:username/repos.
If you use your GitHub username in the URL (as in the latter case), you would get only your public repositories, not all your repos (public and private)

How can i access the braintree response array?

Dear Brantree Developer(s) , I am trying to implement the braintree payments in my website and braintree has given Response in object of array with nested object How can i access it?. i want to get id of customer from bellow response.
Braintree_Result_Successful Object
(
[success] => 1
[_returnObjectNames:private] => Array
(
[0] => customer
)
[_attributes] => Array
(
)
[customer] => Braintree_Customer Object
(
[_attributes:protected] => Array
(
[id] => 22635830
[merchantId] => h9xhpt9b5zjgbkpj
[firstName] => Abhishek
[lastName] => Sonawane
[company] => 1
[email] => abhishek#iarianatech.com
[phone] =>
[fax] =>
[website] =>
[createdAt] => DateTime Object
(
)
[updatedAt] => DateTime Object
(
)
[customFields] =>
[creditCards] => Array
(
[0] => Braintree_CreditCard Object
(
[_attributes] => Array
(
[bin] => 510510
[expirationMonth] => 12
[expirationYear] => 2015
[last4] => 5100
[cardType] => MasterCard
[cardholderName] =>
[commercial] => Unknown
[countryOfIssuance] => Unknown
[createdAt] => DateTime Object
(
)
[customerId] => 22635830
[customerLocation] => US
[debit] => Unknown
[default] => 1
[durbinRegulated] => Unknown
[expired] =>
[healthcare] => Unknown
[imageUrl] => https://assets.braintreegateway.com/payment_method_logo/mastercard.png?environment=sandbox
[issuingBank] => Unknown
[payroll] => Unknown
[prepaid] => Unknown
[subscriptions] => Array
(
)
[token] => 6k2mk2
[uniqueNumberIdentifier] => 0ec6bdfe18781d1bccaea74c4fe55897
[updatedAt] => DateTime Object
(
)
[venmoSdk] =>
[verifications] => Array
(
)
[billingAddress] =>
[expirationDate] => 12/2015
[maskedNumber] => 510510******5100
)
)
)
[addresses] => Array
(
)
[coinbaseAccounts] => Array
(
)
[paypalAccounts] => Array
(
)
[applePayCards] => Array
(
)
)
)
)
Full disclosure: I work at Braintree. If you have any further questions, feel free to contact support.
It looks like that response is coming from a Customer create call. To specifically retrieve the id -
$result = Braintree_Customer::create([
'firstName' => 'Mike',
'lastName' => 'Jones',
'company' => 'Jones Co.',
'email' => 'mike.jones#example.com',
'phone' => '281.330.8004',
'fax' => '419.555.1235',
'website' => 'http://example.com'
]);
$result->success;
# true
$id = $result->customer->id;
Check the API Response section of the guides for getting other attributes.

Silex Security success_handler

How i can set a success_handler (and failure_handler) for the form authentication provider?
Silex ignores me with this config:
<?php
use WebFactory\Security\UserProvider;
$app->register(new Silex\Provider\SecurityServiceProvider(), array(
'security.firewalls' => array(
'dev' => array(
'pattern' => '^/(_(profiler|wdt)|css|images|js)/',
'security' => false
),
'default' => array(
'pattern' => '^/.*$',
'anonymous' => true,
'form' => array(
'login_path' => '/login',
'check_path' => '/login_check',
'success_handler' => 'authentication_handler', //<-- here
'failure_handler' => 'authentication_handler', //<-- here
),
'logout' => array('logout_path' => '/logout'),
'users' => $app->share(function () use ($app) {
return new UserProvider($app['db']);
}),
),
),
'security.access_rules' => array(
array('^/login', 'IS_AUTHENTICATED_ANONYMOUSLY'),
array('^/private$', 'ROLE_ADMIN'),
),
'security.role_hierarchy' => array(
'ROLE_SIMPLE_USER' => array('ROLE_USER'),
'ROLE_ASSOCIATE' => array('ROLE_USER'),
)
));
And this my custom (never invoked)
$app['authentication_handler'] = $app->share(function ($app) {
return new \WebFactory\Security\AuthenticationHandler($app['url_generator']);
});
It is a bug?
The way you set success and failure handlers is by defining a service called security.authentication.success_handler.$name or security.authentication.failure_handler.$name, where $name is the name of the firewall.
For example:
$app->register(new Silex\Provider\SecurityServiceProvider(), array(
'security.firewalls' => array(
'foo' => ...,
),
));
$app['security.authentication.success_handler.foo'] = $app->share(function ($app) {
return new Your\Own\SuccessHandler();
});
The security service provider will then detect the handlers by convention.

Alter a hardcoded form of options based on $user already saved

What I'm trying to accomplish
I'm building a favorites module and I need the ability to:
Select from a dropdown, hardcoded list of options
Have it save to the database
Upon refreshing the page, remove the already saved option from the list of options so it may not be added again
The third part is where I am unsure of how to proceed.
How my code is set up
This is my form:
/*
* Implentation of hook_form().
*/
function f25_favorites_form() {
$listOfPaths = f25_favorites_listOfPaths();
$form['path_options'] = array(
'#type' => 'value',
'#value' => array(
'default' => $listOfPaths['default']['#title'],
'concierge' => $listOfPaths['concierge']['#title'],
'concierge/add' => $listOfPaths['concierge/add']['#title'],
'survey-questions' => $listOfPaths['survey-questions']['#title'],
'survey-questions/add' => $listOfPaths['survey-questions/add']['#title'],
'profiles' => $listOfPaths['profiles']['#title'],
'profiles/add' => $listOfPaths['profiles/add']['#title'],
'statistics' => $listOfPaths['statistics']['#title'],
)
);
$form['path'] = array(
'#type' => 'select',
'#title' => t('Select Page'),
'#required' => TRUE,
'#weight' => '11',
'#options' => $form['path_options']['#value'],
);
$form[submit] = array(
'#type' => 'submit',
'#weight' => '1000000',
'#value' => t('Add')
);
return $form;
}
The name of the paths/options are called via a reference array:
/*
* List of Paths to add to favorites
*/
function f25_favorites_listOfPaths() {
$list = array();
$list = array(
'default' => array(
'#title' => t('Add to favorites'),
),
'concierge' => array(
'#title' => t('Concierge'),
'#image' => drupal_get_path('module', 'f25_favorites').'/img/concierge.png',
'#desc' => t('Concierge'),
),
'concierge/add' => array(
'#title' => t('New Concierge'),
'#image' => drupal_get_path('module', 'f25_favorites').'/img/concierge.png',
'#desc' => t('Concierge > Add'),
),
'survey-questions' => array(
'#title' => t('Survey Questions'),
'#image' => drupal_get_path('module', 'f25_favorites').'/img/survey-questions.png',
'#desc' => t('Current Survey Questions'),
),
'survey-questions/add' => array(
'#title' => t('New Survey Question'),
'#image' => drupal_get_path('module', 'f25_favorites').'/img/survey-questions.png',
'#desc' => t('Survery Question > Add'),
),
'profiles' => array(
'#title' => t('Profiles'),
'#image' => drupal_get_path('module', 'f25_favorites').'/img/profiles.png',
'#desc' => t('User Profiles'),
),
'profiles/add' => array(
'#title' => t('Add Profile'),
'#image' => drupal_get_path('module', 'f25_favorites').'/img/profiles.png',
'#desc' => t('Profiles > Add'),
),
'statistics' => array(
'#title' => t('Statistics'),
'#image' => drupal_get_path('module', 'f25_favorites').'/img/statistics.png',
'#desc' => t('Performance Stats'),
),
);
return $list;
}
And all this is what grabs the data on the databse:
/*
* Write Form data to database
*/
function f25_favorites_form_submit($form, &$form_state){
global $user;
$listOfPaths = f25_favorites_listOfPaths();
$selected = $form_state['values']['path'];
$data = array(
'uid' => $user->uid,
'path' => $selected,
'title' => $listOfPaths[$selected]['#title'],
'weight' => 10,
'timestamp' => time(),
);
drupal_write_record(f25_favorites, $data);
}
Possible Solutions
I've been told that I could used hook_form_alter() in order to modify my array but I am unsure as to when I should be comparing the db_query to my array and how to modify the differences accordingly.
I hope I've done a good job explaining what I'm try to do.
What would be the best way to accomplish this?
Instead of writing every response in f25_favorites_listOfPaths(), shouldn't you get them from the database?
You can then change whatever you want in the submit function to the database so that you don't fetch again the previously selected answer.
Example :
function f25_favorites_listOfPaths() {
return variable_get('f25_favorites_array_' . $user->uid, array(
// your $list array
));
}
function f25_favorites_submit_form($form, &$form_state) {
// your stuff already
drupal_write_record(f25_favorites, $data);
// Now what I propose you to do :)
variable_set('f25_favorites_array_' . $user->uid, array(
// new $list array without the favorite selected
));
}
The use of variable_get/set() should of course be replaced by your own table if you have too much datas.
P.S. : hook_form() does not exist :)

Resources