Toggle navigation
Toggle navigation
This project
Loading...
Sign in
open-source
/
warply_sdk_framework
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Graphs
Network
Create a new issue
Commits
Issue Boards
Authored by
Manos Chorianopoulos
2025-06-27 17:35:47 +0300
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
59fd7b76de0756c301ad675a3c0d51cf877f126e
59fd7b76
1 parent
7e522794
fix context response parsing
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
117 additions
and
1 deletions
FRAMEWORK_MIGRATION_CHANGELOG.md
SwiftWarplyFramework/SwiftWarplyFramework/Network/NetworkService.swift
FRAMEWORK_MIGRATION_CHANGELOG.md
View file @
59fd7b7
...
...
@@ -1156,6 +1156,86 @@ if self.isUserAuthenticated() {
**Result**
: ✅
**No more ERROR: -1**
- campaigns load successfully for both authenticated and non-authenticated users
---
#### **Issue #8: Central Context Response Parsing (FINAL SOLUTION)**
**File**
:
`SwiftWarplyFramework/SwiftWarplyFramework/Network/NetworkService.swift`
**Problem**
: All context endpoints failed due to incorrect response structure parsing - looking for data at wrong level
**Impact**
: ❌
**CRITICAL**
- Multiple endpoints failing with ERROR: -1 due to response parsing issues
**Root Cause**
: The server returns responses in format
`{"status": "1", "context": {...}}`
but the code expected data at top level.
**Example Response Structure**
:
```
json
{
"status"
:
"1"
,
"context"
:
{
"MAPP_CAMPAIGNING-status"
:
1
,
"MAPP_CAMPAIGNING"
:
{
"campaigns"
:
[
...
]
},
"events_processed"
:
1
}
}
```
**Solution**
:
**Central Response Transformation in NetworkService**
```
swift
/// Transform context-wrapped responses to flatten structure for backward compatibility
private
func
transformContextResponse
(
_
response
:
[
String
:
Any
])
->
[
String
:
Any
]
{
// Detect context response pattern: {"status": "1", "context": {...}}
if
let
statusString
=
response
[
"status"
]
as?
String
,
let
context
=
response
[
"context"
]
as?
[
String
:
Any
]
{
print
(
"🔄 [NetworkService] Detected context response - transforming structure"
)
// Start with context content
var
transformedResponse
=
context
// Convert string status to integer for backward compatibility
transformedResponse
[
"status"
]
=
(
statusString
==
"1"
)
?
1
:
0
// Preserve any other top-level fields (like events_processed)
for
(
key
,
value
)
in
response
{
if
key
!=
"context"
&&
key
!=
"status"
{
transformedResponse
[
key
]
=
value
}
}
return
transformedResponse
}
// Return unchanged if not a context response
return
response
}
```
**Integration**
: Added to
`requestRaw`
method in NetworkService:
```
swift
public
func
requestRaw
(
_
endpoint
:
Endpoint
)
async
throws
->
[
String
:
Any
]
{
let
data
=
try
await
performRequest
(
endpoint
)
do
{
guard
let
json
=
try
JSONSerialization
.
jsonObject
(
with
:
data
,
options
:
[])
as?
[
String
:
Any
]
else
{
throw
NetworkError
.
invalidResponse
}
// Transform context-wrapped responses for backward compatibility
let
transformedResponse
=
transformContextResponse
(
json
)
return
transformedResponse
}
catch
{
throw
NetworkError
.
decodingError
(
error
)
}
}
```
**Key Benefits**
:
1.
**Central Fix**
- One solution fixes all context endpoints automatically
2.
**Smart Detection**
- Automatically detects context responses vs regular responses
3.
**Backward Compatible**
- Non-context responses pass through unchanged
4.
**Future-Proof**
- New context endpoints work automatically
5.
**Zero Breaking Changes**
- All existing code continues to work
**Result**
: ✅
**Universal fix for all context response parsing issues**
- getCampaigns and all other context endpoints now work correctly
---
### **🔧 Additional Safety Fixes - June 27, 2025 (Session 2)**
After the initial legacy credential removal, a comprehensive search revealed
**3 additional problematic checks**
that could still cause issues:
...
...
SwiftWarplyFramework/SwiftWarplyFramework/Network/NetworkService.swift
View file @
59fd7b7
...
...
@@ -141,7 +141,10 @@ public final class NetworkService: NetworkServiceProtocol {
guard
let
json
=
try
JSONSerialization
.
jsonObject
(
with
:
data
,
options
:
[])
as?
[
String
:
Any
]
else
{
throw
NetworkError
.
invalidResponse
}
return
json
// Transform context-wrapped responses for backward compatibility
let
transformedResponse
=
transformContextResponse
(
json
)
return
transformedResponse
}
catch
{
throw
NetworkError
.
decodingError
(
error
)
}
...
...
@@ -177,6 +180,39 @@ public final class NetworkService: NetworkServiceProtocol {
// MARK: - Private Methods
/// Transform context-wrapped responses to flatten structure for backward compatibility
/// Detects responses with pattern: {"status": "1", "context": {...}} and flattens them
private
func
transformContextResponse
(
_
response
:
[
String
:
Any
])
->
[
String
:
Any
]
{
// Detect context response pattern: {"status": "1", "context": {...}}
if
let
statusString
=
response
[
"status"
]
as?
String
,
let
context
=
response
[
"context"
]
as?
[
String
:
Any
]
{
print
(
"🔄 [NetworkService] Detected context response - transforming structure"
)
// Start with context content
var
transformedResponse
=
context
// Convert string status to integer for backward compatibility
transformedResponse
[
"status"
]
=
(
statusString
==
"1"
)
?
1
:
0
// Preserve any other top-level fields (like events_processed)
for
(
key
,
value
)
in
response
{
if
key
!=
"context"
&&
key
!=
"status"
{
transformedResponse
[
key
]
=
value
}
}
print
(
"✅ [NetworkService] Context response transformed successfully"
)
print
(
" Original keys:
\(
Array
(
response
.
keys
)
.
sorted
()
)
"
)
print
(
" Transformed keys:
\(
Array
(
transformedResponse
.
keys
)
.
sorted
()
)
"
)
return
transformedResponse
}
// Return unchanged if not a context response
return
response
}
private
func
performRequest
(
_
endpoint
:
Endpoint
)
async
throws
->
Data
{
// Check network connectivity
guard
isConnected
else
{
...
...
Please
register
or
login
to post a comment