1 /***************************************************************************
2     qgsoapifutils.cpp
3     ---------------------
4     begin                : October 2019
5     copyright            : (C) 2019 by Even Rouault
6     email                : even.rouault at spatialys.com
7  ***************************************************************************
8  *                                                                         *
9  *   This program is free software; you can redistribute it and/or modify  *
10  *   it under the terms of the GNU General Public License as published by  *
11  *   the Free Software Foundation; either version 2 of the License, or     *
12  *   (at your option) any later version.                                   *
13  *                                                                         *
14  ***************************************************************************/
15 
16 #include "qgsoapifutils.h"
17 
18 #include <limits>
19 
parseLinks(const json & jParent)20 std::vector<QgsOAPIFJson::Link> QgsOAPIFJson::parseLinks( const json &jParent )
21 {
22   std::vector<Link> links;
23   if ( jParent.is_object() && jParent.contains( "links" ) )
24   {
25     const auto jLinks = jParent["links"];
26     if ( jLinks.is_array() )
27     {
28       for ( const auto &jLink : jLinks )
29       {
30         if ( jLink.is_object() &&
31              jLink.contains( "href" ) &&
32              jLink.contains( "rel" ) )
33         {
34           const auto href = jLink["href"];
35           const auto rel = jLink["rel"];
36           if ( href.is_string() && rel.is_string() )
37           {
38             Link link;
39             link.href = QString::fromStdString( href.get<std::string>() );
40             link.rel = QString::fromStdString( rel.get<std::string>() );
41             if ( jLink.contains( "type" ) )
42             {
43               const auto type = jLink["type"];
44               if ( type.is_string() )
45               {
46                 link.type = QString::fromStdString( type.get<std::string>() );
47               }
48             }
49             if ( jLink.contains( "title" ) )
50             {
51               const auto title = jLink["title"];
52               if ( title.is_string() )
53               {
54                 link.title = QString::fromStdString( title.get<std::string>() );
55               }
56             }
57             if ( jLink.contains( "length" ) )
58             {
59               const auto length = jLink["length"];
60               if ( length.is_number_integer() )
61               {
62                 link.length = length.get<qint64>();
63               }
64             }
65             links.push_back( link );
66           }
67         }
68       }
69     }
70   }
71   return links;
72 }
73 
findLink(const std::vector<QgsOAPIFJson::Link> & links,const QString & rel,const QStringList & preferableTypes)74 QString QgsOAPIFJson::findLink( const std::vector<QgsOAPIFJson::Link> &links,
75                                 const QString &rel,
76                                 const QStringList &preferableTypes )
77 {
78   QString resultHref;
79   int resultPriority = std::numeric_limits<int>::max();
80   for ( const auto &link : links )
81   {
82     if ( link.rel == rel )
83     {
84       int priority = -1;
85       if ( !link.type.isEmpty() && !preferableTypes.isEmpty() )
86       {
87         priority = preferableTypes.indexOf( link.type );
88       }
89       if ( priority < 0 )
90       {
91         priority = static_cast<int>( preferableTypes.size() );
92       }
93       if ( priority < resultPriority )
94       {
95         resultHref = link.href;
96         resultPriority = priority;
97       }
98     }
99   }
100   return resultHref;
101 }
102