GithubHelp home page GithubHelp logo

ios-interactive's Introduction

中文介绍:点我

Interactive is a basic network framework
We provide core Protocols and a common implementation

More Imagination ideas are waiting for you to implementation

Features:
Simple: make http process simple
Mock : simple http response mock (not just string ,but object)
Reuseable and Transferable : requests will be wapper as a Api
Plugin : support preProcess and postProcess and transparent to invoker
Expansibility : all Protocol can be implementation by yourself

Interactive have 4 Core Protocols, the relationship like below image:

XHFApi (we call Api below)

@protocol XHFApi <NSObject>
@required
//provide a request base on ASIHTTPRequest  
-(ASIHTTPRequest*) getHttpRequest;
//provide a parser which need to implement XHFResponseParser
-(id<XHFResponseParser>) getResponseParser;

@optional
//mock Api response,not just string ,but business object
-(id)mock;
@end

Api define 2 Object: Request and Parser
Request: Api need to build a Request
Parser: Api need to build a Parser,it will be used to parse http response

XHFExecutor (we call Executor below)

@protocol XHFExecutor <NSObject>
@required
//execute a Api with Sync Mode
-(id)execute:(id<XHFApi>)api exception:(NSException*__autoreleasing*)exception;
//execute a Api with Async Mode and callback in MainThread
-(id<XHFHandler>)execute:(id<XHFApi>)api completeOnMainThread:(CompleteCallback)callback;
@end

Executor have only one operate: execute
Executor can execute a Api,and return a result,pay attention ,this result is not a string,but a bussiness object which is parsered from response

Above 2 Protocols ,we can give a sample with Pseudocode :

SomeApi *api=[[SomeApi alloc]initWithUrl:@"http://www.google.com"];
SimpleExecutor *executor=[[SimpleExecutor alloc]init];
__autoreleasing NSException *exception=nil;
id result=[executor execute:api exception:&exception];
if(exception!=nil){
      //Business Code
}

XHFHandler (we call Handler below)

@protocol XHFHandler <NSObject>

-(BOOL)isCanceled;

-(BOOL)isDone;

-(void)cancel;

@end

When you execute a Api in Async Mode, you can receive a Handler,using Handler,you can cancel this request,or track the status of this request

XHFExecutorPlugin (we call Plugin below)

@protocol XHFExecutorPlugin <NSObject>
@optional
//this will be invoke before the request send
-(id)preProcess:(ASIHTTPRequest *)request context:(XHFExecutorContext *)context api:(id<XHFApi>)api;
//this will be invoke after the response has received
-(id)postProcess:(ASIHTTPRequest *)request context:(XHFExecutorContext *)context api:(id<XHFApi>)api;

@end

Plugin is a very useful Feature
You can do something before or after request send,and this will be transparent for Api invoker
Both preProcess and postProcess ,we give Classic Case
Case1: Http Request Cache
Sometimes we offen need to cache some type of http request
In this case you can write a Plugin and implement preProcess method

Case2: Login Session Invalid
Some Http Request need login session,sometimes session will be out of time
If we send Http Request Direct and return , it will receive a error
The best way to solve this problem is write a Login Plugin and implement postProcess method
When Login Plugin receive session invalid message,call Login UI pop and lead user to login.
After Login success,Login Plugin make Http Request replay
All this flow is transparent to Api invoker,and invoker not need to think about session invalid problems

ios-interactive's People

Contributors

xuhengfei avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.