3. Appframe API Resources


Installation


1. Add dependency

Add this to your pubspec.yaml under dependencies:

   appframe_api: 
        git: 
            url: 'https://981b027dc46f4c36e26ae91eeeb88e61af1e3321@github.com/Omega365/flutter-appframe-api.git'
            ref: 'v2.0.0'


2. Install it

    You can now install the package from the command line: With Flutter: 
flutter pub get

3. Import it

Now in your Dart code, you can use:

import 'package:appframe_api/appframe_api.dart';




Resources

ALWAYS make sure you include a WHERE in your update and delete statements, else you will update or delete ALL other resources, and potentially break the entire Pims solution... no pressure.


R4

Resources in R4 is currently manged by updating the view stbl_WebSiteCMS_DataResources in db-manager or SSMS. There is a GUI available for managing api-resources that you can find at mmdev-test.omega.no/sitesetup under Data Resources.

Create new resource

    INSERT INTO dbo.stbl_WebSiteCMS_DataResources (DBObjectID) VALUES ('view or procedure name'


Update existing resource

    UPDATE dbo.stbl_WebSiteCMS_DataResources SET DBObjectID = 'Updated name' WHERE DBObjectID = 'Old name'


Delete an existing resource

    DELETE FROM dbo.stbl_WebSiteCMS_DataResources WHERE DBObjectID = 'Resource name'


365

Resources in 365 is currently manged by updating the stbv_WebSiteCMS_ApiResources or sviw_WebSiteCMS_DataResources (Last one works similar to R4, but all views/procs are exposed by default). Unfortunately there is no GUI for setting up/editing  365 resources, you can only do this in db-manager or SSMS. 

Create new resource:

    INSERT INTO dbo.stbl_WebsiteCMS_ApiResources([Name], [View], [Procedure], [UniqueTableName], [AlternativeConnection])         
    SELECT 'MyNewResource', 'aviw_Namespace_ViewName', null, null, null 


Update existing resource

    UPDATE dbo.stbl_WebsiteCMS_ApiResources SET [Name] = 'MyNewResourceName' WHERE ID = 10000


Delete an existing resource

     DELETE FROM dbo.stbl_WebsiteCMS_ApiResources WHERE ID = 10000


Download resource:

Flutter/Dart is a object-oriented language, so to make it easier when fetching data the API relies on a Dart Class that reflects the incoming data from the resource.
There are different ways to download these classes depending on the solution you're using. 

See Developer Tools for flutter for more.



Usage

Full working example can be found in the  github repository .

Data resource

In the API we now have a stream available. The stream returns a snapshot of type PagedApiList. When using the stream in a streambuilder you will now a Future instead of simply a object. This does unfortunately mean you have to use FutureBuilder to build Widgets that rely on data from the API. 
It's still possible to use  FutureBuilder by using 
fetchItemPage.

Sample

This sample demonstrates how to declare a new data resources and update your stream

      AuthenticationService _authenticationService; = Provider.of<AuthenticationService>(context);

      resource = DataResource(
        httpClient: AFHttpClient(
             authService: _authenticationService,                          // instance of http client wrapper
             apiCompatibility: ApiCompatibility.Appframe365),              // resource API compatibility
        creator: Person.creator,                                           // resource model creator function
        fields: Person.fields,                                             // resource model field you wish to fetch
        name: Person.resourceName,                                         // resource name in database
        pageSize: 100);                                                    // desired page size for your stream
     ); 

    resource.refreshPagedStream();                                        //Adds the current instance of PagedApiList to the StreamController.


Constructor

DataResource({AFHttpClient httpClient, String name, Function(Map<String, dynamic>) creator, int pageSize, List<String> fields})


Properties 

creator →  Function(Map<String, dynamic>)
Resource model creator function

fields → List<String>
Resource model field you wish to fetch

name → String
resource name in database

pageSize → int
Desired page size for your stream

httpClient → AFHttpClient
instance of http client wrapper


Methods

newItem(Model newItem, {dynamic fetchNewItem}) → Future<Model>
Attempts to Insert the provided newItem to the server and returns it result.

updateItem(Model item, {dynamic fetchUpdatedItem}) → Future<Person>
Attempts to Update the database record with the same uid as the provided item.

deleteItem(Model item) → Future<bool>
Attempts to Delete the database record with the same uid as the provided item, provides a bool to indicate if it succeeded or not.

refreshPagedStream() → void
Adds the current instance of PagedApiList to the StreamController

refreshDatasource() → Future<dynamic>
Clears the entire cache

fetchItemPage({bool distinctRows, int page, int limit}) → Future<List<Person>>
Fetches a List<Model> from the server.

fetchItem(String primKey) → Future<Person>
Fetches a single Model from the server.

sortOrder(List<Map<String, String>> order);
The sorting order that the DB uses to sort the requested data. Example:
resource.sortOrder = [{"Name": "asc"}];

whereClause;
The whereClause string used as where in DB. Example:
resource.whereClause = "Name = 'Bruce Springsteen'"



Proc resource

As of today there is no Dart class generator for procedures. That means the data you return from a proc will be returned as Lists with Maps

Sample

This sample demonstrates how to declare a new proc resources and run the procedure:

    AuthenticationService _authenticationService; = Provider.of<AuthenticationService>(context);
procResource= ProcResource(       httpClient: AFHttpClient(         authService: _authenticationService, // Instance of http client wrapper                                  apiCompatibility: ApiCompatibility.Appframe365), // Resource API compatibility       name: 'procResource_Name'                                       // resource name in database     );       ProcResponse res = await procResource.execute(                   // Run the procedure    procParameters: {          "Parameter1": "Value1",                                     // Procedure parameters          "Parameter2": "Value2"     }, );


Constructor

ProcResource({AFHttpClient httpClient, String name, int timeout})

Parameter

name → String
Resource name in database

httpClient → AFHttpClient
instance of http client wrapper

Methods

execute({Map<String, dynamic> procParameters, int timeout = 30}) → Future<ProcResponse>
Run the procedure



Related articles

Placeholder "LocalizeWeb2016" failed