LCOV - code coverage report
Current view: top level - lib/model/form - form_uri.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 61 61 100.0 %
Date: 2021-09-29 11:41:23 Functions: 0 0 -

          Line data    Source code
       1             : part of apptive_grid_model;
       2             : 
       3             : /// A Uri representation used for performing Form based Api Calls
       4             : ///
       5             : /// FormUris can come from different sources.
       6             : ///
       7             : /// Depending on your use case you should use a different constructor
       8             : ///
       9             : /// [FormUri.fromUri] is best used if you have a URI Link to a Form but don't know if it's a redirect Link or a direct Link to a Form
      10             : ///
      11             : /// [RedirectFormUri] should be used if you accessed a Form via a Redirect Link e.g. https://app.apptivegrid.de/api/r/609bd6f89fcca3c4c77e70fa
      12             : /// [DirectFormUri] should be used if accessed
      13             : abstract class FormUri extends ApptiveGridUri {
      14           4 :   FormUri._();
      15             : 
      16             :   /// Creates a FormUri based on a [uri]
      17             :   /// [uri] must match either:
      18             :   /// /api/(r|a)/(\w+)\b for [RedirectFormUri]
      19             :   /// or
      20             :   /// /api/users/(\w+)/spaces/(\w+)/grids/(\w+)/forms/(\w+)\b for [DirectFormUri]
      21             :   ///
      22             :   /// throws an [ArgumentError] if [uri] can't be matched against against the above regexes
      23           2 :   factory FormUri.fromUri(String uri) {
      24           4 :     if (RegExp(DirectFormUri._regex).hasMatch(uri)) {
      25           2 :       return DirectFormUri.fromUri(uri);
      26             :     }
      27           4 :     if (RegExp(RedirectFormUri._regex).hasMatch(uri)) {
      28           2 :       return RedirectFormUri.fromUri(uri);
      29             :     }
      30           2 :     throw ArgumentError('Could not parse FormUri $uri');
      31             :   }
      32             : 
      33             :   /// Returns the uriString used for Api Calls using this FormUri
      34             :   @override
      35             :   String get uriString;
      36             : 
      37             :   /// Indicates whether or not a call to this Form will require Authentication
      38             :   ///
      39             :   /// return [false] for [RedirectFormUri]
      40             :   /// return [true] for [DirectFormUri]
      41             :   bool get needsAuthorization;
      42             : }
      43             : 
      44             : /// A FormUri for a Form represented by a redirect Link
      45             : class RedirectFormUri extends FormUri {
      46             :   /// Create a FormUri accessed via a redirect Link from the ApptiveGrid UI Console
      47             :   /// for https://app.apptivegrid.de/api/r/609bd6f89fcca3c4c77e70fa `609bd6f89fcca3c4c77e70fa` would be [components]
      48           3 :   RedirectFormUri({
      49             :     required this.components,
      50           3 :   }) : super._();
      51             : 
      52             :   /// Creates a FormUri based on a [uri]
      53             :   /// [uri] must match:
      54             :   /// /api/(r|a)/(\w+)\b
      55           3 :   factory RedirectFormUri.fromUri(String uri) {
      56           6 :     final matches = RegExp(_regex).allMatches(uri);
      57          12 :     if (matches.isEmpty || matches.elementAt(0).groupCount < 2) {
      58           2 :       throw ArgumentError('Could not parse FormUri $uri');
      59             :     }
      60           3 :     final match = matches.elementAt(0);
      61           9 :     return RedirectFormUri(components: match.group(2)!.split('/'));
      62             :   }
      63             : 
      64             :   static const _regex = r'/api/(r|a)/((\w+/?)+)';
      65             : 
      66             :   /// Id this is representing
      67             :   /// for https://app.apptivegrid.de/api/r/609bd6f89fcca3c4c77e70fa `609bd6f89fcca3c4c77e70fa` would be [components]
      68             :   final List<String> components;
      69             : 
      70           3 :   @override
      71           9 :   String get uriString => '/api/a/${components.join('/')}';
      72             : 
      73           1 :   @override
      74             :   bool get needsAuthorization => false;
      75             : 
      76           2 :   @override
      77             :   String toString() {
      78           4 :     return 'RedirectFormUri(form: $components)';
      79             :   }
      80             : 
      81           2 :   @override
      82             :   bool operator ==(Object other) {
      83           8 :     return other is RedirectFormUri && f.listEquals(components, other.components);
      84             :   }
      85             : 
      86           2 :   @override
      87           4 :   int get hashCode => toString().hashCode;
      88             : }
      89             : 
      90             : /// A FormUri for a Form represented by a direct Link
      91             : class DirectFormUri extends FormUri {
      92             :   /// Create a FormUri with known attributes for [user], [space], [grid], [form]
      93           3 :   DirectFormUri({
      94             :     required this.user,
      95             :     required this.space,
      96             :     required this.grid,
      97             :     required this.form,
      98             :     this.entity,
      99           3 :   }) : super._();
     100             : 
     101             :   /// Creates a FormUri based on a [uri]
     102             :   /// Creates a FormUri based on a [uri]
     103             :   /// [uri] must match:
     104             :   /// /api/users/(\w+)/spaces/(\w+)/grids/(\w+)/forms/(\w+)\b
     105           3 :   factory DirectFormUri.fromUri(String uri) {
     106           3 :     final parsed = Uri.parse(uri);
     107           9 :     final matches = RegExp(_regex).allMatches(parsed.path);
     108          12 :     if (matches.isEmpty || matches.elementAt(0).groupCount != 4) {
     109           2 :       throw ArgumentError('Could not parse FormUri $uri');
     110             :     }
     111             : 
     112           3 :     final match = matches.elementAt(0);
     113             :     EntityUri? entity;
     114           6 :     if (parsed.queryParameters['uri'] != null) {
     115           3 :       entity = EntityUri.fromUri(parsed.queryParameters['uri']!);
     116             :     }
     117           3 :     return DirectFormUri(
     118           3 :       user: match.group(1)!,
     119           3 :       space: match.group(2)!,
     120           3 :       grid: match.group(3)!,
     121           3 :       form: match.group(4)!,
     122             :       entity: entity,
     123             :     );
     124             :   }
     125             : 
     126             :   static const _regex =
     127             :       r'/api/users/(\w+)/spaces/(\w+)/grids/(\w+)/forms/(\w+)\b';
     128             : 
     129             :   /// Id of the User that owns this Grid
     130             :   final String user;
     131             : 
     132             :   /// Id of the Space this Grid is in
     133             :   final String space;
     134             : 
     135             :   /// Id of the Grid this is in
     136             :   final String grid;
     137             : 
     138             :   /// Id of the Form this [FormUri] is representing
     139             :   final String form;
     140             : 
     141             :   /// Optional [EntityUri] leading to a pre-filled form
     142             :   final EntityUri? entity;
     143             : 
     144           1 :   @override
     145             :   bool get needsAuthorization => true;
     146             : 
     147           3 :   @override
     148             :   String get uriString {
     149             :     var entityAppendix = '';
     150           3 :     if (entity != null) {
     151           3 :       entityAppendix = '?uri=${entity!.uriString}';
     152             :     }
     153          15 :     return '/api/users/$user/spaces/$space/grids/$grid/forms/$form$entityAppendix';
     154             :   }
     155             : 
     156             :   /// Returns a FormUri pointing to a Form prefilled with values for a certain [EntityUri]
     157           1 :   FormUri forEntity({
     158             :     required EntityUri entity,
     159             :   }) {
     160           1 :     return DirectFormUri(
     161           1 :       user: user,
     162           1 :       space: space,
     163           1 :       grid: grid,
     164           1 :       form: form,
     165             :       entity: entity,
     166             :     );
     167             :   }
     168             : 
     169           2 :   @override
     170             :   String toString() {
     171          12 :     return 'DirectFormUri(user: $user, space: $space, grid: $grid, form: $form, entity: $entity)';
     172             :   }
     173             : 
     174           2 :   @override
     175             :   bool operator ==(Object other) {
     176           2 :     return other is DirectFormUri &&
     177           6 :         grid == other.grid &&
     178           6 :         user == other.user &&
     179           6 :         space == other.space &&
     180           6 :         form == other.form &&
     181           6 :         entity == other.entity;
     182             :   }
     183             : 
     184           2 :   @override
     185           4 :   int get hashCode => toString().hashCode;
     186             : }

Generated by: LCOV version 1.15