FBPaginatedArray pages are formatted correctly for being used with FBClient

This commit is contained in:
Austin Diviness 2016-09-23 16:18:16 -07:00
Родитель d8b47f49d8
Коммит 1c11ab407c
4 изменённых файлов: 75 добавлений и 4 удалений

Просмотреть файл

@ -403,6 +403,13 @@ Uri^ FBClient::PrepareRequestUri(
dataWriter->UnicodeEncoding = UnicodeEncoding::Utf8;
dataWriter->ByteOrder = ByteOrder::LittleEndian;
if (parameters == nullptr)
{
parameters = ref new PropertySet();
}
// remove any query string parameters from path and put them in parameters
path = MovePathQueryStringToParameters(path, parameters);
PropertySet^ mediaObjects = ref new PropertySet();
PropertySet^ mediaStreams = ref new PropertySet();
PropertySet^ parametersWithoutMediaObjects = ToDictionary(parameters, mediaObjects, mediaStreams);
@ -749,3 +756,21 @@ task<String^> FBClient::TryReceiveHttpResponse(
return result;
});
}
String^ FBClient::MovePathQueryStringToParameters(String^ path, PropertySet^ parameters)
{
Uri^ uri = ref new Uri(L"http://blah.com/" + path);
WwwFormUrlDecoder^ query = uri->QueryParsed;
if (query->Size > 0)
{
IIterator<IWwwFormUrlDecoderEntry^>^ it = query->First();
while (it->HasCurrent)
{
IWwwFormUrlDecoderEntry^ current = it->Current;
parameters->Insert(current->Name, current->Value);
it->MoveNext();
}
}
return uri->Path;
}

Просмотреть файл

@ -228,6 +228,11 @@ namespace winsdkfb
Windows::Foundation::Collections::PropertySet^ Streams
);
Platform::String^ FBClient::MovePathQueryStringToParameters(
Platform::String^ path,
Windows::Foundation::Collections::PropertySet^ parameters
);
concurrency::task<Platform::String^> TryReceiveHttpResponse(
concurrency::task<Windows::Web::Http::HttpResponseMessage^> httpRequestTask,
concurrency::cancellation_token_source cancellationTokenSource

Просмотреть файл

@ -21,6 +21,8 @@
#include "FacebookSession.h"
#include "HttpManager.h"
#include <regex>
using namespace concurrency;
using namespace winsdkfb;
using namespace winsdkfb::Graph;
@ -128,7 +130,8 @@ IVectorView<Object^>^ FBPaginatedArray::ObjectArrayFromJsonArray(
}
FBResult^ FBPaginatedArray::ConsumePagedResponse(
String^ JsonText
String^ JsonText,
String^ OriginalPath
)
{
FBResult^ result = nullptr;
@ -160,6 +163,7 @@ FBResult^ FBPaginatedArray::ConsumePagedResponse(
if (_paging)
{
foundPaging = true;
FormatPagingPaths(_paging, OriginalPath);
}
}
else if (!String::CompareOrdinal(it->Current->Key, L"data"))
@ -231,7 +235,7 @@ Windows::Foundation::IAsyncOperation<FBResult^>^ FBPaginatedArray::GetPage(
return create_async([this, path]() -> task<FBResult^>
{
return create_task(HttpManager::Instance->GetTaskAsync(path, _parameters))
.then([this](String^ responseString)
.then([this, path](String^ responseString)
{
if (responseString == nullptr)
{
@ -239,8 +243,39 @@ Windows::Foundation::IAsyncOperation<FBResult^>^ FBPaginatedArray::GetPage(
}
else
{
return ConsumePagedResponse(responseString);
return ConsumePagedResponse(responseString, path);
}
});
});
}
void FBPaginatedArray::FormatPagingPaths(
FBPaging^ paging,
String^ path
)
{
String^ regexString = L"(?:.*?)(" + path + L".*)";
std::wstring stdRegexString = std::wstring{ regexString->Data() };
std::wregex relativePathRegex{ stdRegexString };
std::wsmatch match;
if (paging->Next)
{
std::wstring nextUrl = std::wstring{ paging->Next->Data() };
std::regex_match(nextUrl, match, relativePathRegex);
if (match.size() >= 2)
{
std::wstring matchText = match[1].str();
paging->Next = ref new String(matchText.c_str());
}
}
if (paging->Previous)
{
std::wstring previousUrl = std::wstring{ paging->Previous->Data() };
std::regex_match(previousUrl, match, relativePathRegex);
if (match.size() >= 2)
{
std::wstring matchText = match[1].str();
paging->Next = ref new String(matchText.c_str());
}
}
}

Просмотреть файл

@ -143,13 +143,19 @@ namespace winsdkfb
);
FBResult^ ConsumePagedResponse(
Platform::String^ JsonText
Platform::String^ JsonText,
Platform::String^ OriginalPath
);
Windows::Foundation::IAsyncOperation<FBResult^>^ GetPage(
Platform::String^ path
);
void FormatPagingPaths(
FBPaging^ paging,
Platform::String^ path
);
Windows::Foundation::Collections::IVectorView<Object^>^ _current;
FBPaging^ _paging;
Platform::String^ _request;