Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
Vlad Bukhanets
/
immoscout24-api-php
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Merge Requests
0
Network
Create a new issue
Builds
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
c51ee11c
authored
2019-11-05 17:03:54 +0100
by
fehrlich
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
invalid response improvements
1 parent
9b1a0216
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
50 additions
and
9 deletions
src/ImmoScoutAPI.php
src/exceptions/InvalidResponse.php
src/ImmoScoutAPI.php
View file @
c51ee11
...
...
@@ -4,16 +4,15 @@ namespace fehrlich\ImmoScoutAPI;
use
function
GuzzleHttp
\json_decode
;
use
function
GuzzleHttp
\json_encode
;
use
fehrlich\ScoutAPI\exceptions\AuthException
;
use
fehrlich\ScoutAPI\exceptions\InvalidResponse
;
use
fehrlich\ScoutAPI\exceptions\InvalidTokenException
;
use
fehrlich\
Immo
ScoutAPI\exceptions\AuthException
;
use
fehrlich\
Immo
ScoutAPI\exceptions\InvalidResponse
;
use
fehrlich\
Immo
ScoutAPI\exceptions\InvalidTokenException
;
use
GuzzleHttp\Client
;
use
GuzzleHttp\Command\Guzzle\Description
;
use
GuzzleHttp\Command\Guzzle\GuzzleClient
;
use
GuzzleHttp\Exception\ClientException
;
use
GuzzleHttp\HandlerStack
;
use
GuzzleHttp\Subscriber\Oauth\Oauth1
;
use
InvalidArgumentException
;
use
Psr\Http\Message\ResponseInterface
;
/**
...
...
@@ -377,7 +376,16 @@ abstract class ImmoScoutAPI extends GuzzleClient
if
(
isset
(
$json
[
'common.messages'
])
&&
is_array
(
$json
[
'common.messages'
]))
{
foreach
(
$json
[
'common.messages'
]
as
$message
)
{
if
(
isset
(
$message
[
'message'
])
&&
isset
(
$message
[
'message'
][
'messageCode'
]))
{
if
(
isset
(
$message
[
'message'
])
&&
isset
(
$message
[
'message'
][
0
]))
{
foreach
(
$message
[
'message'
]
as
$msg
)
{
if
(
isset
(
$msg
[
'messageCode'
]))
{
if
(
!
isset
(
$return
[
$msg
[
'messageCode'
]]))
{
$return
[
$msg
[
'messageCode'
]]
=
[];
}
$return
[
$msg
[
'messageCode'
]][]
=
$msg
[
'message'
];
}
}
}
elseif
(
isset
(
$message
[
'message'
])
&&
isset
(
$message
[
'message'
][
'messageCode'
]))
{
if
(
!
isset
(
$return
[
$message
[
'message'
][
'messageCode'
]]))
{
$return
[
$message
[
'message'
][
'messageCode'
]]
=
[];
}
...
...
@@ -387,15 +395,23 @@ abstract class ImmoScoutAPI extends GuzzleClient
}
$parsedErrors
=
[];
foreach
(
$return
as
$errorCode
=>
$msgArr
)
{
foreach
(
$msgArr
as
$msg
)
{
$add
=
null
;
switch
(
$errorCode
)
{
case
'ERROR_RESOURCE_VALIDATION'
:
preg_match
(
'/MESSAGE: (.*?) :/'
,
$msg
,
$matches
);
if
(
isset
(
$matches
[
1
]))
{
$add
=
$matches
[
1
];
}
else
{
preg_match
(
'/MESSAGE: (.*?)\]/'
,
$msg
,
$matches
);
if
(
isset
(
$matches
[
1
]))
{
$add
=
$matches
[
1
];
if
(
$matches
[
1
]
==
'RealEstate already exists for external id.'
)
{
$parsedErrors
[
'ERROR_RESOURCE_DUPLICATE'
]
=
true
;
}
}
}
break
;
case
'MESSAGE_RESOURCE_CREATED'
:
...
...
@@ -480,7 +496,7 @@ abstract class ImmoScoutAPI extends GuzzleClient
$msgs
=
$this
->
parseMessages
(
$res
);
if
(
!
isset
(
$msgs
[
'parsed'
][
$expectedResponse
]))
{
$code
=
isset
(
$msgs
[
'parsed'
][
'ERROR_RESOURCE_NOT_FOUND'
])
?
404
:
null
;
throw
new
InvalidResponse
(
'Did not get expected response: '
.
$res
->
getBody
(),
$code
);
throw
new
InvalidResponse
(
'Did not get expected response: '
.
$res
->
getBody
(),
$code
,
null
,
$res
,
$msgs
);
}
if
(
$expectedResponse
==
'MESSAGE_RESOURCE_CREATED'
)
{
return
$msgs
[
'parsed'
][
$expectedResponse
][
0
];
...
...
@@ -506,7 +522,11 @@ abstract class ImmoScoutAPI extends GuzzleClient
private
function
parseGetResponse
(
$res
)
{
return
json_decode
((
string
)
$res
->
getBody
(),
true
);
try
{
return
json_decode
((
string
)
$res
->
getBody
(),
true
);
}
catch
(
Exception
$ex
)
{
throw
new
InvalidResponse
(
'invalid json response'
);
}
}
/**
...
...
src/exceptions/InvalidResponse.php
View file @
c51ee11
<?php
namespace
fehrlich\ScoutAPI\exceptions
;
namespace
fehrlich\ImmoScoutAPI\exceptions
;
use
Exception
;
class
InvalidResponse
extends
Exception
{
private
$response
;
private
$msgs
;
public
function
__construct
(
$msg
,
$code
=
0
,
$prev
=
null
,
$res
=
null
,
$msgs
=
null
)
{
parent
::
__construct
(
$msg
,
$code
,
$prev
);
$this
->
msgs
=
$msgs
;
$this
->
response
=
$res
;
}
public
function
getResponse
()
{
return
$this
->
response
;
}
public
function
getMessages
()
{
return
$this
->
msgs
;
}
}
...
...
Write
Preview
Styling with
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment