HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

C# Async Oauth API Wrapper Class

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
classasyncwrapperoauthapi

Problem

Two quick notes: I'm targeting 4.5, and the server already has newtownsoft's json library. I'd prefer not installing anything additional on the server.

```
using Newtonsoft.Json.Linq;
using System;
using System.Text;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Web;
using System.Web.Script.Serialization;
using System.Collections.Generic;
using System.Configuration;

namespace Sierra
{
public class API
{
private string _uri, _key, _secret, _token;
private DateTime _token_expires;
public bool isInitialized = false;
private static HttpClient Client = new HttpClient();

#region Classes for json serializer

//POCO classes for serializer

#endregion

///
/// initialize the API
///
/// Base uri to API endpoint
/// Key value
/// Secret value
public API(string uri, string key, string secret)
{
_uri = uri;
_key = key;
_secret = secret;
isInitialized = GetBearerTokenAsync().Result;
}

public Patron GetPatronById(string barcode)
{
string uri = String.Format("patrons/find?barcode={0}", barcode);
var Patron = GetAsync(uri).Result;

return Patron;
}

public Boolean PlaceHold(int patronID, string recordType, int recordNumber, string pickupLocation)
{
string uri = String.Format("patrons/{0}/holds/requests", patronID);

Hold hold = new Hold();
hold.recordType = recordType;
hold.recordNumber = recordNumber;
hold.pickupLocation = pickupLocation;
string json = Newtonsoft.Json.JsonConvert.SerializeObject(hold);

HttpResponseMessage response = PostAsync(uri, json).Result;

return response.IsSuccessStatusCode == true;
}

public async Ta

Solution

Do not use a public field; instead, make it a property:

public bool isInitialized = false;


Your code is barely above 100 lines, you don't need regions and you shouldn't use them anyway.

Local variables like var Patron should be camelCase.

Don't use Boolean, etc., instead use the alias (bool,...).

This:

Hold hold = new Hold();
        hold.recordType = recordType;
        hold.recordNumber = recordNumber;
        hold.pickupLocation = pickupLocation;


... can be compacted to:

var hold = new Hold 
        {
           recordType = recordType,
           recordNumber = recordNumber,
           pickupLocation = pickupLocation
        }


You're inconsistent in using var. For instance:

JavaScriptSerializer ser = new JavaScriptSerializer();


If you'd called it javaScriptSerializer instead:

var javaScriptSerializer = new JavaScriptSerializer();


The flow of GetBearerTokenAsync() jumps around a lot. You define request on line 3, yet don't use until line 10. Instead, you continue building client.

Code Snippets

public bool isInitialized = false;
Hold hold = new Hold();
        hold.recordType = recordType;
        hold.recordNumber = recordNumber;
        hold.pickupLocation = pickupLocation;
var hold = new Hold 
        {
           recordType = recordType,
           recordNumber = recordNumber,
           pickupLocation = pickupLocation
        }
JavaScriptSerializer ser = new JavaScriptSerializer();
var javaScriptSerializer = new JavaScriptSerializer();

Context

StackExchange Code Review Q#140211, answer score: 4

Revisions (0)

No revisions yet.