Skip to content

Instantly share code, notes, and snippets.

@qizwiz
Created January 14, 2016 20:17

Revisions

  1. qizwiz created this gist Jan 14, 2016.
    265 changes: 265 additions & 0 deletions *magit-diff: ecl.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,265 @@
    modified code/lib/eCarList/Bridge/eCarList/V1/REST.pm
    @@ -47,6 +47,7 @@ Returns a new eCarList::Bridge::eCarList::V1::REST object.

    Returns:
    $self -- a blessed eCarList::Bridge::eCarList::V1::REST object
    +
    =cut
    sub new {
    my ($class, %args) = @_;
    @@ -56,11 +57,10 @@ sub new {
    dl => $args{dl},
    inv_id => $args{inv_id},
    dealer_eid => $args{dealer_eid} || $args{eid},
    - hostname => $args{hostname},
    - };

    + };
    bless $self, $class;
    -
    + $self->{hostname} = $args{hostname} || $self->_hostname();
    return $self;
    }

    @@ -92,7 +92,7 @@ sub messages {
    my $response = from_json($self->{response}->{_content});

    if ($response->{messages}) {
    - if ('ARRAY' eq ref $response->{messages}) {
    + if (ref $response->{messages} eq 'ARRAY') {
    $messages = $response->{messages};
    } else {
    $messages = [$response->{messages}];
    @@ -111,6 +111,7 @@ Given a inv_id, find and set the api_key

    Returns:
    $self
    +
    =cut
    sub for_inv_id {
    my ($self, $inv_id) = @_;
    @@ -140,6 +141,7 @@ Sets the api_key for this interface

    Returns:
    $self
    +
    =cut
    sub with_api_key {
    my ($self, $api_key) = @_;
    @@ -152,6 +154,7 @@ sub with_api_key {
    Returns the api_key if set, or tries to figure it out if a inv_id is set
    Returns:
    $self->{api_key} - api_key as string if we can figure it out.
    +
    =cut
    sub _api_key {
    my ($self) = @_;
    @@ -174,6 +177,7 @@ sub _api_key {
    Returns the request headers
    Returns:
    $headers - as hash
    +
    =cut
    sub _headers {
    my ($self) = @_;
    @@ -193,6 +197,7 @@ Given a route_name, return the full path for that route

    Returns:
    $path - full route path as string
    +
    =cut
    sub route {
    my ($self, $route_name, $route_param) = @_;
    @@ -201,7 +206,7 @@ sub route {
    throw eCarList::Exception::InvalidArguments("Route `$route_name` not a valid API route.");
    }

    - return $self->_hostname() . '/api/v1' . $routes{$route_name} . ($route_param ? '/' . $route_param : '');
    + return $self->{hostname} . '/api/v1' . $routes{$route_name} . ($route_param ? '/' . $route_param : '');
    }


    @@ -213,6 +218,7 @@ Given a path and a hash of params, return a fully encoded uri string

    Returns:
    encoded uri as string
    +
    =cut
    sub _uri {
    my ($self, $path, $params) = @_;
    @@ -226,19 +232,28 @@ sub _uri {
    Return the host name
    Returns:
    $hostname - as a string including http/https based on environment settings
    +
    =cut
    sub _hostname {
    my ($self) = @_;

    - unless ($self->{hostname}) {
    - my $app_config = eCarList::BLL::Config::Configuration->get_instance();
    - $self->{hostname} = ($app_config->{http_settings}->{https} ? 'https://' : 'http://');
    - $self->{hostname} .= $app_config->{hostname};
    - }
    + my $config = eCarList::BLL::Config::Configuration->get_instance();
    + my $protocol = $self->_protocol($config->{http_settings}->{https});
    + print STDERR "protocol: $ {\(Dumper $protocol)}";
    + my $internal = $config->{internal};
    + my $host = defined $internal ?
    + $internal->{hostname} . ':' . $internal->{port} :
    + $config->{hostname};
    + $self->{hostname} = $protocol . $host;
    + return $self->{hostname};

    - return $self->{hostname}
    }

    +sub _protocol {
    + my ($self, $https) = @_;
    +
    + return ($https) ? 'https://' : 'http://';
    +}

    #####################
    #### Request methods
    @@ -254,6 +269,7 @@ Sends get appraisals request

    Returns:
    $response - HTTP::Response
    +
    =cut
    sub get_appraisals {
    my ($self, %args) = @_;
    @@ -274,8 +290,7 @@ sub get_appraisals {
    my $params = {};
    if ($self->{inv_id}) {
    $params->{inv_id} = $self->{inv_id} ;
    - }
    - elsif($self->{dealer_eid}){
    + } elsif ($self->{dealer_eid}) {
    $params->{dealer_eid} = $self->{dealer_eid};
    }

    @@ -298,12 +313,12 @@ Sends get users request

    Returns:
    $response - HTTP::Response
    +
    =cut
    sub get_users {
    my ($self, %args) = @_;

    my $path = $self->route('get_users');
    -
    # If we already know the inv_id, lets use the one we have.
    $args{inv_id} = $self->{inv_id} if ($self->{inv_id});
    unless ($args{inv_id} || $self->{dealer_eid}) {
    @@ -314,12 +329,14 @@ sub get_users {
    my $params = {};
    if ($self->{inv_id}) {
    $params->{inv_id} = $self->{inv_id};
    - }
    - elsif($self->{dealer_eid}){
    + } elsif ($self->{dealer_eid}) {
    $params->{dealer_eid} = $self->{dealer_eid};
    }
    - $self->{response} = $self->get($self->_uri($path, $params), $self->_headers());
    - return from_json($self->{response}->{_content})->{content};
    + my $uri = $self->_uri($path, $params);
    + print STDERR "uri: $ {\(Dumper $uri)}";
    + my $headers = $self->_headers();
    + $self->{response} = $self->get($uri, $headers);
    + my $json = from_json($self->{response}->{_content})->{content};
    }

    =item C<add_appraisal>
    @@ -331,6 +348,7 @@ Post new appraisal data

    Returns:
    $response - HTTP::Response
    +
    =cut
    sub add_appraisal {
    my ($self, %args) = @_;
    @@ -348,8 +366,7 @@ sub add_appraisal {
    my $params = {};
    if ($self->{inv_id}) {
    $params->{inv_id} = $self->{inv_id} ;
    - }
    - elsif($self->{dealer_eid}){
    + } elsif ($self->{dealer_eid}) {
    $params->{dealer_eid} = $self->{dealer_eid};
    }
    $self->{response} = $self->post($self->_uri($path, $params), $self->_headers(), $content);
    @@ -365,6 +382,7 @@ Post updated appraisal data

    Returns:
    $response - HTTP::Response
    +
    =cut
    sub update_appraisal {
    my ($self, %args) = @_;
    @@ -387,8 +405,7 @@ sub update_appraisal {
    my $params = {};
    if ($self->{inv_id}) {
    $params->{inv_id} = $self->{inv_id} ;
    - }
    - elsif($self->{dealer_eid}){
    + } elsif ($self->{dealer_eid}) {
    $params->{dealer_eid} = $self->{dealer_eid};
    }
    $self->{response} = $self->post($self->_uri($path, $params), $self->_headers(), $content);
    @@ -404,6 +421,7 @@ get appraisal value

    Returns:
    $response - HTTP::Response
    +
    =cut
    sub get_appraisal_value {
    my ($self, %args) = @_;
    @@ -423,8 +441,7 @@ sub get_appraisal_value {
    my $params = {};
    if ($self->{inv_id}) {
    $params->{inv_id} = $self->{inv_id} ;
    - }
    - elsif($self->{dealer_eid}){
    + } elsif ($self->{dealer_eid}) {
    $params->{dealer_eid} = $self->{dealer_eid};
    }
    $params->{eid} = $args{trade_eid} ;
    @@ -443,6 +460,7 @@ get user existance status
    user_id: user eid of the INV+ system (Optional when first_name & last_name is given)
    Returns:
    $response - HTTP::Response
    +
    =cut
    sub check_user {
    my ($self, %args) = @_;
    @@ -460,8 +478,7 @@ sub check_user {
    my $params = {};
    if ($self->{inv_id}) {
    $params->{inv_id} = $self->{inv_id} ;
    - }
    - elsif($self->{dealer_eid}){
    + } elsif ($self->{dealer_eid}) {
    $params->{dealer_eid} = $self->{dealer_eid};
    }
    my $path = $self->route('check_user');
    @@ -479,6 +496,7 @@ Add/Update Staff person to INV+
    user_id: user eid of the INV+ system (Optional when first_name & last_name is given)
    Returns:
    $response - HTTP::Response
    +
    =cut
    sub upsert_salesperson {
    my ($self, %args) = @_;
    @@ -498,8 +516,7 @@ sub upsert_salesperson {
    my $params = {};
    if ($self->{inv_id}) {
    $params->{inv_id} = $self->{inv_id} ;
    - }
    - elsif($self->{dealer_eid}){
    + } elsif ($self->{dealer_eid}) {
    $params->{dealer_eid} = $self->{dealer_eid};
    }
    my $path = $self->route('upsert_salesperson');