Skip to content

Custom Queries

linvi edited this page Jun 7, 2015 · 7 revisions

Overview

We are trying hard to make every feature as easy as a single line of code. But obviously there are time when what you want to do is not yet implemented in the library.

Yet, to simplify your life we have created the TwitterAccessor class. This will allow you to perform any action possible in the Twitter library by specifying a simple query!

Let's see how this works!

Let's code

Get objects from query

When performing a query, you can specify a type which should be a DTO (Data Transfer Object). These objects defined in the library have very little to no logic. Their only goal is to convert the result from Twitter into an object.

When you have a DTO object, you can create a "concrete" object by using the associated static class and its GenerateFromDTO method.

// Example to create an IUser from an IUserDTO
var user = User.GenerateUserFromDTO(userDTO);

Simple query

// GET from simple query
var userDTO = TwitterAccessor.ExecuteGETQuery<IUserDTO>("https://api.twitter.com/1.1/users/show.json?screen_name=tweetinviapi");

// POST from simple query
var tweetDTO = TwitterAccessor.ExecutePOSTQuery<ITweetDTO>("https://api.twitter.com/1.1/statuses/update.json?status=hello");

JSON query

var userJson = TwitterAccessor.TwitterAccessor.ExecuteJsonGETQuery("https://api.twitter.com/1.1/users/show.json?screen_name=tweetinviapi");

Cursor query

Cursor queries allow developer to access a high number of results by iterating over cursors. For example if a user has more than 5000 followers you will need to use a cursor query to access all the followers of that user.

The library contains the method ExecuteCursorGETQuery<T1, T2> which takes 2 types :

  • T1 : DTO type that will be available as a result of the query
  • T2 : A type implementing IBaseCursorQueryDTO that will allow Tweetinvi to access the information it needs to automatically iterate over the results sent by Twitter. At the current time the following CursorQueryDTO classes exist : IdsCursorQueryResultDTO, UserCursorQueryResultDTO, TwitterListCursorQueryResultDTO.

Learn how to create my own CursorQueryResultDTO

var userIds = TwitterAccessor.ExecuteCursorGETQuery<long, IIdsCursorQueryResultDTO>("https://api.twitter.com/1.1/followers/ids.json?screen_name=tweetinviapi");

You can otherwise get the results from ExecuteCursorGETCursorQueryResult:

var results = TwitterAccessor.ExecuteCursorGETCursorQueryResult<IIdsCursorQueryResultDTO>("https://api.twitter.com/1.1/followers/ids.json?screen_name=tweetinviapi");
var userIds = results.SelectMany(x => x.Ids);
Clone this wiki locally