<commit_before>package Open311;

use URI;
use Moose;
use XML::Simple;
use LWP::Simple;
use LWP::UserAgent;
use HTTP::Request::Common qw(POST);

has jurisdiction => ( is => 'ro', isa => 'Str' );;
has api_key => ( is => 'ro', isa => 'Str' );
has endpoint => ( is => 'ro', isa => 'Str' );

sub get_service_list {
    my $self = shift;

    my $service_list_xml = $self->_get( 'services.xml' );
    my $xml = XML::Simple->new();
    my $obj = $xml->XMLin( $service_list_xml );

    return $obj;
}

sub get_service_meta_info {
    my $self = shift;
    my $service_id = shift;

    my $service_meta_xml = $self->_get( "services/$service_id.xml" );
    my $xml = XML::Simple->new();
    my $obj = $xml->XMLin( $service_meta_xml );

    return $obj;
}

sub send_service_request {
    my $self = shift;
    my $problem = shift;

    my $description = <<EOT;
title:  @{[$problem->title()]}

detail: @{[$problem->detail()]}

Submitted via FixMyStreet
EOT
;

    my $params = {
        lat => $problem->latitude,
        long => $problem->longitude,
        email => $problem->user->email,
        description => $description,
    };

    if ( $problem->user->phone ) {
        $params->{ phone } = $problem->user->phone;
    }

    if ( $problem->extra ) {
        my $extras = $problem->extra;

        for my $attr ( keys %{ $extras } ) {
            my $name = sprintf( 'attribute[%s]', $attr );
            $params->{ $name } = $extras->{ $attr };
        }
    }

    my $response = $self->_post( 'requests.xml', $params );

    my $xml = XML::Simple->new();
    my $obj = $xml->XMLin( $response );

    if ( $obj->{ request }->{ service_request_id } ) {
        return $obj->{ request }->{ service_request_id };
    } else {
        my $token = $obj->{ request }->{ token };
        return $self->get_service_request_id_from_token( $token );
    }
}

sub get_service_requests {
    my $self = shift;

    my $service_request_xml = $self->_get( 'requests.xml' );
}

sub get_service_request_id_from_token {
    my $self = shift;
    my $token = shift;

    my $service_token_xml = $self->_get( "tokens/$token.xml" );

    my $xml = XML::Simple->new();
    my $obj = $xml->XMLin( $service_token_xml );

    return $obj->{ request }->{ service_request_id };
}

sub _get {
    my $self   = shift;
    my $path   = shift;
    my $params = shift;

    my $uri = URI->new( $self->endpoint );
    $uri->path( $uri->path . $path );
    $uri->query_form( jurisdiction_id => $self->jurisdiction );

    my $content = get( $uri->as_string );

    return $content;
}

sub _post {
    my $self = shift;
    my $path   = shift;
    my $params = shift;

    my $uri = URI->new( $self->endpoint );
    $uri->path( $uri->path . $path );

    use Data::Dumper;
    my $req = POST $uri->as_string,
    [
        jurisdiction_id => $self->jurisdiction,
        api_key => $self->api_key,
        %{ $params }
    ];

    my $ua = LWP::UserAgent->new();
    my $res = $ua->request( $req );

    if ( $res->is_success ) {
        return $res->decoded_content;
    } else {
        return 0;
    }
}
1;<commit_msg>improvments and fixes to service request submission<commit_after>package Open311;

use URI;
use Moose;
use XML::Simple;
use LWP::Simple;
use LWP::UserAgent;
use HTTP::Request::Common qw(POST);

has jurisdiction => ( is => 'ro', isa => 'Str' );;
has api_key => ( is => 'ro', isa => 'Str' );
has endpoint => ( is => 'ro', isa => 'Str' );

sub get_service_list {
    my $self = shift;

    my $service_list_xml = $self->_get( 'services.xml' );
    my $xml = XML::Simple->new();
    my $obj = $xml->XMLin( $service_list_xml );

    return $obj;
}

sub get_service_meta_info {
    my $self = shift;
    my $service_id = shift;

    my $service_meta_xml = $self->_get( "services/$service_id.xml" );
    my $xml = XML::Simple->new();
    my $obj = $xml->XMLin( $service_meta_xml );

    return $obj;
}

sub send_service_request {
    my $self = shift;
    my $problem = shift;
    my $service_code = shift;

    my $description = <<EOT;
title:  @{[$problem->title()]}

detail: @{[$problem->detail()]}

Submitted via FixMyStreet
EOT
;

    my $params = {
        lat => $problem->latitude,
        long => $problem->longitude,
        email => $problem->user->email,
        description => $description,
        service_code => $service_code,
    };

    if ( $problem->user->phone ) {
        $params->{ phone } = $problem->user->phone;
    }

    if ( $problem->extra ) {
        my $extras = $problem->extra;

        for my $attr ( @$extras ) {
            my $name = sprintf( 'attribute[%s]', $attr->{name} );
            $params->{ $name } = $attr->{value};
        }
    }

    my $response = $self->_post( 'requests.xml', $params );

    if ( $response ) {
        my $xml = XML::Simple->new();
        my $obj = $xml->XMLin( $response );

        if ( $obj->{ request }->{ service_request_id } ) {
            return $obj->{ request }->{ service_request_id };
        } else {
            my $token = $obj->{ request }->{ token };
            return $self->get_service_request_id_from_token( $token );
        }
    }
}

sub get_service_requests {
    my $self = shift;

    my $service_request_xml = $self->_get( 'requests.xml' );
}

sub get_service_request_id_from_token {
    my $self = shift;
    my $token = shift;

    my $service_token_xml = $self->_get( "tokens/$token.xml" );

    my $xml = XML::Simple->new();
    my $obj = $xml->XMLin( $service_token_xml );

    if ( $obj->{ request }->{ service_request_id } ) {
        return $obj->{ request }->{ service_request_id };
    } else {
        return 0;
    }
}

sub _get {
    my $self   = shift;
    my $path   = shift;
    my $params = shift;

    my $uri = URI->new( $self->endpoint );
    $uri->path( $uri->path . $path );
    $uri->query_form( jurisdiction_id => $self->jurisdiction );

    my $content = get( $uri->as_string );

    return $content;
}

sub _post {
    my $self = shift;
    my $path   = shift;
    my $params = shift;

    my $uri = URI->new( $self->endpoint );
    $uri->path( $uri->path . $path );

    use Data::Dumper;
    my $req = POST $uri->as_string,
    [
        jurisdiction_id => $self->jurisdiction,
        api_key => $self->api_key,
        %{ $params }
    ];

    my $ua = LWP::UserAgent->new();
    my $res = $ua->request( $req );

    if ( $res->is_success ) {
        return $res->decoded_content;
    } else {
        warn "request failed: " . $res->status_line;
        warn $res->decoded_content;
        return 0;
    }
}
1;
{'fraction_non_alphanumeric': 0.178735808530224, 'fraction_numerical': 0.0016876342436330162, 'mean_word_length': 2.4492063492063494, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 151, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 40}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1830467', 'n_tokens_mistral': 2352, 'n_tokens_neox': 2255, 'n_words': 537}