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");

By default Tweetinvi returns null if a query fails. With the TwitterAccessor, you can use a TryGet to check if a query was successful and if it was get the result.

IUserDTO userDTO;
var success = TwitterAccessor.TryExecuteGETQuery("my_query", out userDTO);

Returns part of a query result

Some queries returns more information than you could consider as useful. For example when performing a user search, you want to get a collection of users for your result.

If you encounter such a requirement, you can use ExecuteGETQueryFromPath to create DTO's object from the json path.

var userDTOs = ExecuteGETQueryWithPath<IEnumerable<IUserDTO>>("my_query", "users");

JSON query

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

Boolean query

What I call a boolean query is a query where you just want to check if the query has been successful.

var success = TwitterAccessor.TryExecuteGETQuery("my_query");

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);

JSON Object from Query

Tweetinvi gives you the possibility to get a JSON Object from a query. With this object, you will be able to access any json fields available in the string returned from the query.

var jsonObject = TwitterAccessor.GetQueryableJsonObjectFromGETQuery("my_query");
var fieldValue = jsonObject["field_name"];
Clone this wiki locally