| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | package WWW::Shopify::URLHandler; | ||
| 7 | use JSON qw(from_json to_json encode_json decode_json); | ||
| 8 | use Data::Dumper; | ||
| 9 | use Scalar::Util qw(weaken); | ||
| 10 | use DateTime; | ||
| 11 | use Encode; | ||
| 12 | |||
| 13 | sub new { | ||
| 14 | my ($package, $parent, $headers) = @_; | ||
| 15 | my $self = bless {_parent => $parent, _default_headers => ($headers || {})}, $package; | ||
| 16 | weaken($self->{_parent}); | ||
| 17 | return $self; | ||
| 18 | } | ||
| 19 | sub ua { return $_[0]->parent->ua; } | ||
| 20 | |||
| 21 | sub default_header { | ||
| 22 | my ($self, $key, $value) = @_; | ||
| 23 | $self->{_default_headers}->{$key} = $value if int(@_) >= 3; | ||
| 24 | return $self->{_default_headers}->{$key}; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub parent { $_[0]->{_parent} = $_[1] if defined $_[1]; return $_[0]->{_parent}; } | ||
| 28 | |||
| 29 | sub get_url { | ||
| 30 | my ($self, $url, $parameters, $type) = @_; | ||
| 31 | $type = "application/json" unless $type; | ||
| 32 | my $uri = URI->new($url); | ||
| 33 | my %filtered = (); | ||
| 34 | if ($parameters->{fields} && ref($parameters->{fields}) && ref($parameters->{fields}) eq "ARRAY") { | ||
| 35 | $parameters->{fields} = join(",", @{$parameters->{fields}}); | ||
| 36 | } | ||
| 37 | for (keys(%$parameters)) { | ||
| 38 | if ($parameters->{$_} && ref($parameters->{$_}) eq "DateTime") { | ||
| 39 | $filtered{$_} = $parameters->{$_}->strftime('%Y-%m-%d %H:%M:%S%z') | ||
| 40 | } | ||
| 41 | elsif ($_ ne "parent") { | ||
| 42 | $filtered{$_} = $parameters->{$_}; | ||
| 43 | } | ||
| 44 | } | ||
| 45 | $uri->query_form(\%filtered); | ||
| 46 | my $request = HTTP::Request->new("GET", $uri); | ||
| 47 | $request->header("Accept" => $type); | ||
| 48 | $request->header("Accept-Encoding" => "gzip") if !$ENV{"SHOPIFY_LOG"} || $ENV{"SHOPIFY_LOG"} != 2; | ||
| 49 | $request->header($_ => $self->{_default_headers}->{$_}) for (keys(%{$self->{_default_headers}})); | ||
| 50 | return $self->handle_response($self->request($request)); | ||
| 51 | |||
| 52 | } | ||
| 53 | |||
| 54 | sub request { | ||
| 55 | my ($self, $request) = @_; | ||
| 56 | return $self->ua->request($request); | ||
| 57 | } | ||
| 58 | |||
| 59 | sub handle_response { | ||
| 60 | my ($self, $response) = @_; | ||
| 61 | if (!$response->is_success) { | ||
| 62 | die WWW::Shopify::Exception::CallLimit->new($response) if $response->code() == 429; | ||
| 63 | die WWW::Shopify::Exception::InvalidKey->new($response) if $response->code() == 401; | ||
| 64 | die WWW::Shopify::Exception::NotFound->new($response) if $response->code() == 404; | ||
| 65 | die WWW::Shopify::Exception->new($response); | ||
| 66 | } | ||
| 67 | my $limit = $response->header('x-shopify-shop-api-call-limit'); | ||
| 68 | if ($limit) { | ||
| 69 | die new WWW::Shopify::Exception("Unrecognized limit.") unless $limit =~ m/(\d+)\/(\d+)/; | ||
| 70 | $self->parent->api_calls($1); | ||
| 71 | $self->parent->max_api_calls($2); | ||
| 72 | } | ||
| 73 | my $content = $response->decoded_content; | ||
| 74 | # From JSON because decodec content is already a perl internal string. | ||
| 75 | # Sigh. No. It's not. As per https://rt.cpan.org/Public/Bug/Display.html?id=82963; decoded_content doesn't actually do anything. | ||
| 76 | $content = decode("UTF-8", $content) if ($response->header('Content-Type') =~ m/application\/json/); | ||
| 77 | my $decoded = length($content) >= 2 && (!$response->header('Content-Type') || $response->header('Content-Type') =~ /json/) ? from_json($content) : $content; | ||
| 78 | return ($decoded, $response); | ||
| 79 | } | ||
| 80 | |||
| 81 | use URI::Escape; | ||
| 82 | use JSON qw(encode_json); | ||
| 83 | use Scalar::Util qw(reftype); | ||
| 84 | |||
| 85 | |||
| 86 | sub flatten_object { | ||
| 87 | my ($prefix, $object) = @_; | ||
| 88 | return map { | ||
| 89 | my $key = $_; | ||
| 90 | my @items; | ||
| 91 | @items = $object->{$key}->iso8601 if (ref($object) || "") eq "DateTime"; | ||
| 92 | @items = flatten_object($prefix . $key, $object->{$key}) if reftype($object->{$key}) && reftype($object->{$key}) eq "HASH"; | ||
| 93 | @items = (map { flatten_object($prefix . "[" . $key . "][]", $_) } @{$object->{$key}}) if reftype($object->{$key}) && reftype($object->{$key}) eq "ARRAY"; | ||
| 94 | @items = ($prefix . "[" . $key . "]=" . uri_escape_utf8(defined $object->{$key} ? $object->{$key} : '')) if !reftype($object->{$key}); | ||
| 95 | |||
| 96 | @items; | ||
| 97 | } grep { $_ ne "associated_parent" && $_ ne "associated_sa" } keys(%$object); | ||
| 98 | } | ||
| 99 | |||
| 100 | sub use_url{ | ||
| 101 | my ($self, $method, $url, $hash, $needs_login, $type, $accept) = @_; | ||
| 102 | $type = "application/json" unless $type; | ||
| 103 | $accept = "application/json" unless $accept; | ||
| 104 | my $request = HTTP::Request->new($method, $url); | ||
| 105 | $request->header("Accept" => $accept, "Content-Type" => $type); | ||
| 106 | $request->header("Accept-Encoding" => "gzip") if !$ENV{"SHOPIFY_LOG"} || $ENV{"SHOPIFY_LOG"} != 2; | ||
| 107 | $request->header($_ => $self->{_default_headers}->{$_}) for (keys(%{$self->{_default_headers}})); | ||
| 108 | if ($type =~ m/json/) { | ||
| 109 | $request->content($hash ? encode_json($hash) : undef); | ||
| 110 | } else { | ||
| 111 | if ($hash) { | ||
| 112 | $request->content(join("&", map { | ||
| 113 | if (reftype($hash->{$_}) && reftype($hash->{$_}) eq "HASH") { | ||
| 114 | flatten_object($_, $hash->{$_}); | ||
| 115 | } else { | ||
| 116 | my $name = uri_escape_utf8($_); | ||
| 117 | map { $name . "=" . uri_escape_utf8($_) } ($hash->{$_} && ref($hash->{$_}) eq "ARRAY" ? @{$hash->{$_}} : ($hash->{$_})) | ||
| 118 | } | ||
| 119 | } keys(%$hash) )); | ||
| 120 | } | ||
| 121 | } | ||
| 122 | my $response = $self->request($request); | ||
| 123 | if ($type =~ m/json/) { | ||
| 124 | return $self->handle_response($response); | ||
| 125 | } else { | ||
| 126 | if ($response->is_redirect) { | ||
| 127 | |||
| 128 | } | ||
| 129 | return (undef, $response); | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | sub upload_url { | ||
| 134 | my ($self, $method, $url, $name, $filename, $mime, $contents) = @_; | ||
| 135 | my $request = HTTP::Request->new($method, $url); | ||
| 136 | my $boundary = '----WebKitFormBoundaryePkpFF7tjBAqx29L'; | ||
| 137 | $request->header('Content-Type' => 'multipart/form-data; boundary=' . $boundary); | ||
| 138 | $request->header('Accept' => 'Accept:application/json, text/javascript, */*; q=0.01'); | ||
| 139 | $request->content("--" . $boundary . "\r\n" . | ||
| 140 | "Content-Disposition: form-data; name=\"$name\"; filename=\"$filename\"\r\n" . | ||
| 141 | "Content-Type: $mime\r\n\r\n$contents\r\n--$boundary--"); | ||
| 142 | return $self->ua->request($request); | ||
| 143 | } | ||
| 144 | |||
| 145 | sub put_url { return shift->use_url("PUT", @_); } | ||
| 146 | sub post_url { return shift->use_url("POST", @_); } | ||
| 147 | sub delete_url { return shift->use_url("DELETE", @_); } | ||
| 148 | |||
| 149 | 1; |